Welcome! Log In Create A New Profile

Advanced

limit_req does not work

Posted by jonathanmueller 
limit_req does not work
February 09, 2015 12:26PM
Hi,

I'm trying to implement a simple rate limiting system with nginx (v1.6.2)
sites-available/mysite.com:

limit_req_zone $binary_remote_addr zone=myzone:10m rate=2r/m;
server {
[..]

location = /test {
limit_req zone=myzone burst=3 nodelay;
rewrite ^(.*)$ /test.txt;
}

location = / {
limit_req zone=myzone burst=3 nodelay;
index lala.html;
}

location ~ /somesite {
limit_req zone=myzone burst=3 nodelay;
rewrite ^(.*)$ /somesite.txt;
}

[..]
}


Rate limiting / works fine. I get an error 503 if I do too many requests to this page.
The problem: Neither /test, /somesite nor /somesite/lala is rate limited, and I don't know why. It must have to do something with rewrite, but what's the problem?

Any ideas? I tried everything!
Re: limit_req does not work
February 11, 2015 03:19PM
The limit_req happens at a later phase of query processing than the rewrite. So when you do the /test and /somesite the rewrite happens before limit. The URL the rewrite is targeting is not exactly '/' so the middle location isn't getting hit by the rewrite either to limit there. I suspect what you want is:

location = /test {
rewrite ^(.*)$ /test.txt;
}

location / {
limit_req zone=myzone burst=3 nodelay;
index lala.html;
}

location ~ /somesite {
rewrite ^(.*)$ /somesite.txt;
}
Re: limit_req does not work
February 11, 2015 03:22PM
But
location / {
limit_req zone=myzone burst=3 nodelay;
index lala.html;
}
matches any rewrite, right? So
location ~ /dont-rate-limit {
rewrite ^(.*)$ /no.txt;
}
will also be rate limited?
I only want to rate limit *some* rewritten pages. :/
Re: limit_req does not work
February 11, 2015 03:25PM
I know this will be a bit of a pain but the way I can think of doing it in that case is something along the lines of:

location = /test {
rewrite ^(.*)$ /test.txt;
}

location = / {
limit_req zone=myzone burst=3 nodelay;
index lala.html;
}

location ~ /somesite {
rewrite ^(.*)$ /somesite.txt;
}

location = /somesite.txt {
limit_req zone=myzone burst=3 nodelay;
}

location = /test.txt {
limit_req zone=myzone burst=3 nodelay;
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 140
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready