Rate Limit Misconfigured Bots in Nginx

nginxrate-limiting

We have an Nginx / PHP-FPM behind a load balancer which periodically gets scraped. Many of the scrapers hit our application's 404 page quiet hard.

Is there a way in Nginx, possibly with the rate-limiting module or a different module, to block all traffic based on x-forward-for after hitting 404 errors at over 100 requests per minute?

The documentation I found for limit-req-module looks like it is based on resource rather than on page return status.

There is enough traffic, that each node is seeing enough 404 traffic that they shouldn't need to communicate about who to collectively block.

Best Answer

You can certainly do this in nginx with the limit_req_zone module.

In nginx.conf setup a zone:

limit_req_zone $binary_remote_addr zone=one:1000m   rate=100r/m;

You can also use X_Forwarded_For instead of binary_remote_addr too.

In your site config location block just reference the zone:

limit_req zone=one burst=10 nodelay;

doc: http://nginx.org/en/docs/http/ngx_http_limit_req_module.html