I'm able to use limit_req
to rate-limit all requests to my server.
However I'd like to remove the rate restriction for certain IP addresses (i.e. whitelist) and use a different rate restriction for certain others (i.e. certain IPs I'd like as low as 1r/s).
I tried using conditionals (e.g. if ( $remote_addr = "1.2.3.4" ) {}
) but that seems to work only with rewrite rules, not for rate-limit rules.
It is really better to avoid using the "if" directive. When the key in limit_req_zone (and limit_conn_zone) is empty the limits are not applied. You can use this in conjunction with the map and geo modules to create a whitelist of IPs where the throttle limits are not applied.
This example shows how to configure a limit for both concurrent requests and request rate from a single IP.
The zone directives must be placed at the http level, however the other directives can be placed further down, e.g. at the server or the location level to limit their scope or further tailor the limits.
For futher information refer to the Nginx documentation ngx_http_limit_req_module and ngx_http_limit_conn_module
You can safely use named locations, such as "@location" in an if() block.
See: http://wiki.nginx.org/IfIsEvil
Something like this should work:
Fill in "location @slowdown { }" with the same information as "location / { }, such as proxy_pass if you're using nginx as a reverse proxy.