3
votes

I have a global rate limiting in my nginx.conf for both host and IP. However, there is one specific location where I want to ignore those limits, something like this.

limit_req_zone $binary_remote_addr zone=limitip:10m rate=10r/s;
limit_req_zone $host zone=limithost:10m rate=10r/s;

server {
    limit_req zone=limitip burst=5 nodelay;
    limit_req zone=limithost burst=5 nodelay;

    location /whitelisted_location {
        /* ignore the server limits */
    }
}

Is the best approach to create two new zones with really high values and use them inside the location?

1

1 Answers

0
votes

@agentshowers, you can probably find an idea how to do that in this topic.

Actually, we do this in the way like:

# Defining which limit zone to use (per URL)
map $request_uri $pb_limit_req_zone {
    "~^/files(/.*)?"    "files";
    "~^/secret/health-check$"    "healthchecks";

     default                    "common";
}

# Define the key per zone name
map $pb_limit_req_zone $limit_req_key_files {
    default             "";
    "files"    $binary_remote_addr;
}
map $pb_limit_req_zone $limit_req_key_common {
    default             "";
    "common"    $binary_remote_addr;
}
map $pb_limit_req_zone $limit_req_key_healthchecks {
    default             "";
    "healthchecks"    $binary_remote_addr;
}

# Defining the zones
limit_req_zone $limit_req_key_files zone=pb-frontend-files:20m rate=10r/s;
limit_req_zone $limit_req_key_common zone=pb-frontend-common:20m rate=25r/s;
limit_req_zone $limit_req_key_healthchecks zone=pb-frontend-healthchecks:20m rate=100r/s;

...
...
server {
...
    location / {
        # Rate Limit settings
        limit_req_dry_run off;
        limit_req zone=pb-frontend-files burst=1 delay=10;
        limit_req zone=pb-frontend-common  delay=10;
        limit_req zone=pb-frontend-healthchecks burst=50 delay=10;
        limit_req_status 429;

        try_files $uri $uri/ /index.php?$query_string;
    }
...
...
    # Pass all .php files to a php-fpm/php-fcgi server.
    location ~ [^/]\.php(/|$) {
        # Rate Limit settings
        limit_req_dry_run off;
        limit_req zone=pb-frontend-files burst=1 delay=10;
        limit_req zone=pb-frontend-common  delay=10;
        limit_req zone=pb-frontend-healthchecks burst=50 delay=10;
        limit_req_status 429;
    ...
    ...
    }
}