limit_req and limit_conn in rewrite modules if statement

Nam nginx-forum at
Mon Nov 18 19:49:27 UTC 2013

Hello, I would like to see if it's possible to get limit_conn and limit_req
working with the rewrite modules if statement. I have seen some discussion
about this in the mailing list already saying to use stuff like throwing a
410 code and having that 410 code handled by a @named location that handles
requests that should not be limited, such as ...

location / {
error_page 410 = @nolimit;

if ($http_user_agent ~ Googlebot) {
return 410;

limit_req zone=one burst=4;


location @nolimit {

I also know about how if statements are considered evil and should be
avoided where possible, but I am working with dynamically generating config
files which support multiple upstreams, with different upstream options per
location directive with various features involved. I would like to be able
to set a variable that I can than use in an if statement to determine if
limit_con or limit_req should be used. For example...

set $Whitelisted "No";
if ($http_user_agent ~ (googlebot|bingbot)) {
    set $Whitelisted "Yes";
if ($Whitelisted ~* "No") {
    limit_conn conlimit-one 5;
    limit_req zone=limit-half burst=9;

Is there any possibility of this functionality being unlocked in the nginx
code? I really need this functionality, and I hope it's not a big deal to
Is there are particular reason why this module does not work with the
rewrite if statement?

Posted at Nginx Forum:,244781,244781#msg-244781

More information about the nginx mailing list