limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;
What does this directive do in NGINX?
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;
The limit_req_zone directive defines a shared memory zone to track request rates per key. Here, $binary_remote_addr means the client IP is used as the key. The zone named 'mylimit' uses 10MB memory to store states. The rate=5r/s limits each client IP to 5 requests per second.
limit_req zone=mylimit burst=10 nodelay;
What is the behavior of NGINX when requests exceed the rate defined in the zone?
limit_req zone=mylimit burst=10 nodelay;The burst=10 allows up to 10 requests to exceed the rate limit temporarily by queuing them. The nodelay option means these burst requests are processed immediately without delay. Requests beyond this burst are rejected with a 503 error.
limit_req_zone $remote_addr zone=one:10m rate=1r/s limit_req zone=one burst=5;
limit_req_zone $remote_addr zone=one:10m rate=1r/s; limit_req zone=one burst=5;
NGINX requires each directive to end with a semicolon. The first line is missing a semicolon, causing a syntax error.
Setting a burst with no nodelay queues excess requests and delays them instead of rejecting immediately. This helps avoid blocking users who send occasional bursts.
First, define the shared memory zone with limit_req_zone. Then apply limit_req in server or location block. Reload NGINX to apply changes. Finally, test the setup.