NGINX second limit_req not working after rewrite

1.6k views Asked by At

This limit_req configuration works if I hit php pages directly (index.php), but not if it hits /[pretty urls] and rewrites it to index.php?$args.

limit_req_zone $binary_remote_addr zone=dynamic:10M rate=1r/s;
limit_req_zone $binary_remote_addr zone=static:10M rate=60r/s;

location / {
            limit_req zone=static burst=180;
            try_files $uri $uri/ /index.php?$args;
    }

location ~ \.php$ {
            limit_req zone=dynamic burst=5;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;

            set $fsn /index.php;
            if (-f $document_root$fastcgi_script_name){
                    set $fsn $fastcgi_script_name;
            }

            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;
            fastcgi_param  PATH_INFO        $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
    }

It is obviously getting to the right location because it passes to the backend, but the limit_req doesn't appear to be used. I can't find anything discussing this limitation in the docs. Does anyone have any ideas?

Edit: Commenting out the first one with zone=static allows the php processing one to work. To me this looks like a bug in limit_req.

1

There are 1 answers

0
RobC On BEST ANSWER

It looks like this is just the way NGINX and the limit_req module work... perhaps in the future it will get better.

After a lot of digging I found an nginx forum post that talks about this http://forum.nginx.org/read.php?2,223426,223431 and it appears one or both of the following is true (I don't have the interest to dig into this any further).

  • It looks like if limit_req is used in a 'location' using a try_files directive, future limit_req directives are not used.
  • It might be that once a location processes a limit_req directive they will not be processed in another 'location', but multiple limit_reqs in a single 'location' should work fine.

So far the solution to my config is to remove the limit_req from / and setup a new location regex match for static files and put in the limit_req for static data there.