Rate Limit Requests per IP with Nginx

Posted on January 31, 2017 at 11:24 pm

You can limit requests per IP per second with Nginx.

Add this line inside http{} block to limti 1 request per second per IP:

limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

Lets say you want to limit requests only on /events/:

Add this text inside server{} block:

location /events/ {
    limit_req zone=one nodelay;
}

*** Using “nodelay” means the extra requests are closed with error 503 ***

Lets say you want to limit requests only on .php files:

Add this text inside server{} block:

location ~ \.php$ {
    [...]
    limit_req zone=one nodelay;
    # By default it returns error 503 (service unavailable)
    # To return error 429 (too many requests) add this
    limit_req_status 429;
}

Restart Nginx service:

/etc/init.d/nginx restart

More information:

Module ngx_http_limit_req_module

Updated on February 1, 2017 at 11:15 pm

Receive updates via email

Other Posts

Updated Posts