I find myself needing to limit the number of requests a particular IP can send for a particular URI (e.g. www.foo.com/somewhere/special
) while leaving the rest of the site unregulated.
How would I configure that using the HttpLimitReqModule
built into Nginx?
You need to add a
where you configure your limits with HttpLimitReqModule or HttpLimitZoneModule modules.
Here's how it is done on one of my sites. First, we use
geo
module to define a variable that be used to identify limited addresses:The slowlist file is like this:
and so on (IP addresses are just examples, of course). Then we define speed limits:
Now we have to assign req_zones to every IP address. Inside the necessary location, we check the variable $slow, and if equals 1, we redirect to location @slow (using fictitious error 555):
Then normal rules follow, including root, index, fastcgi*, whatever. Limit zone 'fast' will be used here. And, finally, we define location @slow, where limit zone 'slow' will be used:
Then normal rules follow, including root, index, fastcgi*, whatever.
So, usual requests are processed in the normal location, but all others are redirected to the location @slow.