Every public web server gets visited by bots. Some are useful, such as search engine crawlers that index your pages. Others scrape content, probe for vulnerable paths, hammer login forms, or copy your whole site to resell it. This guide shows how to block bad bots and scrapers with Nginx rules, using the tools Nginx already provides, without paying for a separate service.
The goal is not to block every bot. Search engines and legitimate monitoring tools should keep working. The goal is to stop the abusive ones: clients that ignore robots.txt, request hundreds of pages per minute, fake common browser user agents, or probe paths your site never had.
Start by Identifying the Bad Bots in Your Logs
Before writing any rule, look at the access log and find the actual offenders. Rank client IPs and user agents by request count, and look for patterns: one IP requesting a page every second, a user agent that appears thousands of times but never loads images or CSS, or a flood of 404s for admin panels and backup files. Rules written from real log evidence block real traffic. Rules copied from a generic list often block nothing on your server while breaking something useful.
A quick way to spot the worst clients is to count requests per IP over the last day and look at the top of the list. A legitimate search crawler spreads requests across many pages at a moderate pace. A scraper often hits the same site from one IP at a pace no human reader could match. Once you have a short list of IPs, user agents, or paths, you can write rules aimed at exactly those patterns.
Block Bad Bots and Scrapers by User Agent
These nginx bot blocking rules work best when they target agents you have actually seen in your logs. An nginx user agent block is the simplest layer to start with. The simplest Nginx rule matches the user agent string. Inside a server block or a location block, an if test on $http_user_agent can return 403 for known scraper agents, empty user agents, and agents that claim to be a browser but behave like a script. Keep the list short and specific to agents you have actually seen, and test after each addition.
User agent blocking has a hard limit: any client can change its user agent in one line. It stops lazy scrapers and off-the-shelf tools, but it will not stop a determined scraper that rotates agents. Treat it as one layer, not the whole defense. For the same reason, do not rely on robots.txt alone. Polite crawlers obey it, but scrapers ignore it by definition, so robots.txt is a request, not a control.
When a bot ignores your rules and keeps hammering, rate limiting is the next layer. Our Nginx rate limiting guide shows how limit_req slows abusive clients without blocking legitimate visitors, and the companion guide on rate limiting against brute force and scraping applies the same idea to login and scraping paths specifically.
Use Rate Limits and Connection Limits Together
Rate limiting and connection limiting solve different problems. Rate limiting caps how many requests a client can make per second, which stops a scraper from copying a site quickly. Connection limiting caps how many simultaneous connections one IP can hold, which stops a client that opens many parallel connections to grab pages faster. Used together, they force a scraper to slow down to a pace closer to a human reader, which usually makes scraping your site not worth the effort.
To stop scrapers nginx rate limits are more reliable than user agent rules alone, because limits act on behavior, not identity. Set limits per location, not just globally. A strict global limit can break legitimate bursts, such as a page that loads many assets at once. A tighter limit on an API endpoint, a search page, or a login form targets the paths scrapers and attackers actually abuse, while the rest of the site keeps normal limits. Return 503 or 429 for excess requests, and watch the error log after deploying a new limit to confirm real visitors are not being caught.
Block Scraping Paths and Empty Referrers Carefully
Some rules target behavior instead of identity. Blocking requests for common probe paths, such as known admin panels and backup file extensions, cuts log noise and stops a class of automated attacks before they reach the application. Blocking requests with an empty user agent also catches many scripts, since real browsers always send one.
Be careful with referrer rules. Blocking empty referrers sounds tidy, but direct visits, bookmarks, and some privacy tools also send no referrer, so a broad rule blocks real people. Apply behavior rules to sensitive paths only, and test with a normal browser session before deploying to production. The same caution applies to geo blocking: it can cut a large share of scanner traffic, but it also blocks legitimate visitors who travel or use a VPN, so treat it as a last resort for sites with a genuinely local audience.
When to Add a Firewall or a WAF in Front of Nginx
Nginx rules handle most bot traffic well, but they still cost CPU on every request, and a large distributed attack can overwhelm a small server before Nginx rules matter. At that point, move the blocking upstream: a firewall rule that drops traffic from a known bad network, or a cloud WAF that filters requests before they reach your server. Our Cloudflare security settings guide covers the WAF and bot fight options on that platform, and the UFW firewall guide shows how to add server-level firewall rules.
A sensible order is: identify offenders in the logs, block the obvious ones by user agent, add rate and connection limits on abused paths, then escalate to a firewall or WAF only if the volume still hurts the server. Each layer is cheap to add and easy to test on its own, which makes it easier to find the layer that broke something when a legitimate visitor reports a problem.
Keep a short note of every rule you add and why, with the log evidence behind it. Bot rules rot fast: scrapers change agents, IPs change hands, and a rule that was correct six months ago can start blocking a new legitimate tool. Review the list a few times a year, remove rules that no longer match any traffic, and keep the defense matched to the bots actually visiting your server.