How to stop unwanted crawlers and bots: robots.txt versus real server and CDN enforcement
robots.txt manages cooperative crawling. It does not protect private files or stop a client that ignores it. This guide shows enforceable nginx controls, measured rate limits, caching, testing, and rollback.

A crawler rule has two very different jobs. robots.txt asks cooperative bots not to fetch a URL. A CDN, reverse proxy, or application rule actually refuses the request. Use the first to manage reputable search crawlers. Use the second for traffic you do not trust.
What each control can do
• robots.txt: manages crawling by bots that choose to comply. Google warns that it is not a way to protect private information, and a blocked URL can still appear in search results.
• noindex: asks supporting search engines not to index a page, but the crawler must be allowed to fetch the directive.
• Authentication: protects private content. A 401 challenge or a signed-in application is the right boundary for customer data.
• CDN, firewall, nginx, or application rules: enforce blocks and rate limits. Put the cheapest reliable check as close to the edge as you can.
Primary references: Google's robots.txt limitations, RFC 9309, and Google's indexing controls.
1. Block known scan targets without sending them to the app
Match exact paths or carefully bounded patterns. Do not use one broad expression that can catch legitimate downloads.
A normal 404 is easy to test and understand. nginx also supports its non-standard 444, which closes the connection without response headers, but that can make monitoring and client behaviour harder. Use it only when that trade-off is deliberate. See nginx's return directive.
2. Rate-limit expensive routes
This example allows an average of 2 requests a second per source address and an immediate burst of 10 before rejecting excess requests with 429. Those are starting values, not universal safe numbers. Measure real visitors and API clients first. Also restore the real client address from your trusted CDN before keying a limit on $binary_remote_addr; otherwise every visitor may appear to be the CDN. nginx documents the leaky-bucket behaviour in limit_req.
3. Cache public pages without caching private ones
Cache only responses that are truly public and identical for every visitor. A route that varies by cookie, authorization header, tenant, or account must bypass the shared cache. proxy_cache_lock lets one request populate a missing cache entry while matching requests wait; it does not make an unsafe cache policy safe. See nginx's proxy cache documentation.
4. Test and roll back
1. Start rate limiting in dry-run mode with limit_req_dry_run on; and inspect the log.
2. Test the normal page, the blocked path, a signed-in page, and requests through the real CDN.
3. Run nginx -t before every reload.
4. Reload gracefully, then watch 2xx, 401, 403, 404, and 429 counts separately.
5. Keep the last known-good configuration ready and restore it if legitimate traffic changes unexpectedly.
User-Agent rules can reduce obvious noise, but a client can copy any User-Agent string. If you need to admit a named crawler, verify its network identity too. The companion guide shows how to allow verified crawlers without opening private pages.
Need a second pair of eyes on a reverse-proxy or application boundary?
Talk to Biz59