Nginx limiting the redirection

nginxredirection

We use nginx to redirect any http request to https connection, So though the user makes http connection nginx will redirect to https connection.

Here does nginx have any limit that can handle only 'n' re-directions per second. Because our application is accessible by other system and they would make http requests always intentionally. So nginx has to redirect all the http requests to https, but It coulndn't handle many requests at a time.

But If we make https requests, It is able handle many number of requests per second as it doesn't require any redirection. Below is our configuration

server {
    listen   6000;                                          ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on;                  ## listen for ipv6
    server_name     someDomain.com;

    rewrite        ^ https://$server_name$request_uri? ;

    #root /usr/share/nginx/www;
    index index.html index.htm;
}

Best Answer

You need to use the limit_req directive.

http
{
    limit_req_zone $binary_remote_addr zone=zonename:100m rate=25r/s;
    server
    {
        limit_req zone=zonename burst=5;

$binary_remote_addr limits each IP to 25 requests per second rate=25r/s

You can add nodelay to limit_req to make it respond with a 503 as soon as the limit is hit.