Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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:

geo $slow {
    default 0;
    include /etc/nginx/conf.d/slowlist;
}

The slowlist file is like this:

192.168.0.0/24  1;
192.168.10.0/24  1;

and so on (IP addresses are just examples, of course). Then we define speed limits:

limit_req_zone $binary_remote_addr zone=fast:1m rate=1000r/s;
limit_req_zone $binary_remote_addr zone=slow:10m rate=20r/m;

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):

recursive_error_pages on;
error_page 555 = @slow;
if ($slow = 1) {
    return 555;
}
limit_req zone=fast burst=10000 nodelay;

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:

location @slow {
    limit_req zone=slow burst=5 nodelay;

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.

share|improve this answer

You need to add a

location /somewhere/special {
...
}

where you configure your limits with HttpLimitReqModule or HttpLimitZoneModule modules.

share|improve this answer
    
What if the location is in the middle of the URL like foo.com/filter/somewhere?special=overtherainbow. I want to filter on "filter", so would I put /filter/ in the location? –  QWade Dec 15 '11 at 3:31
    
You may define required location with regex –  ghloogh Dec 15 '11 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.