[nginx] Using User-Agent & IP address to rate limit

lorenanicole nginx-forum at nginx.us
Mon Jul 28 16:43:14 UTC 2014

Thanks for the prompt feedback! Yes, the continuous if directives put my
teeth on edge as well. Using a map block to introduce variables for $zone
and $burst respectively, I tried this already and had continuous errors.
Attempting this again (per your suggestions), I have this error --

nginx: [emerg] invalid burst rate "burst=$burst" in

With line 69 as follows --

limit_req  zone=$zone   burst=$burst;

Reading the limit_req directives
(http://nginx.org/en/docs/http/ngx_http_limit_req_module.html) instructions
there is no information specifying that you can use a variable to set these
values. Likewise I do not believe you can use limit_req inside the context
of an if directive (as you can with proxy_pass for example -
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass). My
attempt to read off the value of $http_user_agent and set the limit_req
inside an if block resulted in many "nginx: [emerg]" errors.

My second thought now is to use nested locations after doing a rewrite on
the $uri and inside these nested locations set the limit_req, log the info,
and proxy_pass along. This way the request doesn't have to start over.

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,252085,252087#msg-252087

More information about the nginx mailing list