Protection
10 min readUpdatednginx rate limiting with limit_req: burst, nodelay and delay, measured
Quick answer
nginx rate limiting takes two directives. limit_req_zone, in the http block, sets a key such as $binary_remote_addr, a shared memory zone and a rate such as 5r/s; limit_req applies it to a server or location. Requests beyond the rate are refused with 503 unless burst lets them queue. nodelay serves the burst at once, and delay=N serves the first N excess requests at once. Set limit_req_status 429. In a test with 20 simultaneous requests at 5r/s and burst=10, every burst variant served 11 and refused 9; only the timing changed.
01How does nginx rate limiting work?
limit_req_zone defines what is counted and how fast; limit_req switches the limit on and sets how many extra requests may wait (burst) and whether they wait at all (nodelay, delay). Anything beyond the burst is refused, with 503 unless limit_req_status says otherwise.
nginx uses the leaky-bucket method at millisecond resolution, so rate=5r/s means one request every 200 ms, not any five within a second. The concept and the choice of key are in what rate limiting is; timeouts, caching and what nginx cannot stop are in nginx DDoS protection.
| Which situation are you in? | Read next |
|---|---|
| What do burst, nodelay and delay change? | burst, nodelay and delay: 20 requests at once, measured |
| The limit does nothing | Why limit_req does nothing in a location that only returns |
| Clients get 503, or you need the zone size | The directives, defaults and zone size |
| Monitoring or an office must not be limited | Several limits at once, and exempting trusted addresses |
| You limit an API | API rate limiting with nginx |
| Everyone is limited at once behind a CDN | Behind a CDN or load balancer |
| Apache, Caddy, WordPress, Cloudflare or ingress-nginx | The section for each, from their documentation |
# http {} context
limit_req_zone $binary_remote_addr zone=perip:10m rate=5r/s;
limit_req_status 429;
# server {} or location {} context
location / {
limit_req zone=perip burst=10 nodelay;
try_files $uri $uri/ =404; # or your proxy_pass
}02burst, nodelay and delay: 20 requests at once, measured
burst, nodelay and delay are the parts of limit_req most often misread, so they were measured. Five locations, each with its own zone at rate=5r/s keyed on $binary_remote_addr, served the same small static file. Each got 20 requests at the same moment from one address, with 4 seconds between variants so every bucket started empty. Only the limit_req line changed.
| limit_req line | 200 OK | Refused | Served at once | Last 200 after |
|---|---|---|---|---|
| limit_req zone=a; (no burst) | 1 | 19 × 503 | 1 | 0.00 s |
| limit_req zone=b burst=10; | 11 | 9 × 503 | 1 | 2.00 s |
| limit_req zone=c burst=10 nodelay; | 11 | 9 × 503 | 11 | within 0.01 s |
| limit_req zone=d burst=10 delay=4; | 11 | 9 × 503 | 5 | 1.20 s |
| limit_req zone=e burst=10 nodelay; plus limit_req_status 429; | 11 | 9 × 429 | 11 | within 0.01 s |
# http {}: one zone per variant, so each test starts with an empty bucket
limit_req_zone $binary_remote_addr zone=a:1m rate=5r/s;
limit_req_zone $binary_remote_addr zone=b:1m rate=5r/s;
# zones c, d and e are identical
server {
listen 127.0.0.1:18090;
root /srv/rl-test;
location /plain/ { limit_req zone=a; try_files /index.txt =404; }
location /burst/ { limit_req zone=b burst=10; try_files /index.txt =404; }
location /nodelay/ { limit_req zone=c burst=10 nodelay; try_files /index.txt =404; }
location /delay/ { limit_req zone=d burst=10 delay=4; try_files /index.txt =404; }
location /s429/ { limit_req zone=e burst=10 nodelay; limit_req_status 429;
try_files /index.txt =404; }
}03What each result means
No burst. burst defaults to zero, so nothing may wait. The first request was served; the other 19 arrived within milliseconds, before the next 200 ms slot, and got 503 at once. A browser loads a page with parallel requests, so a limit without burst refuses ordinary visitors.
burst=10. Ten requests above the rate are accepted, but queued and released at the rate, one every 200 ms. The tenth waited 10 × 0.2 = 2.00 seconds, holding its connection open the whole time. The other 9 found the queue full and got 503.
burst=10 nodelay. The same 11 requests passed, all within 0.01 seconds. nodelay does not raise the limit: the 10 burst slots count as used and free up at 5 per second, so a client that keeps sending is held to the rate.
burst=10 delay=4. Two-stage limiting, available since nginx 1.15.7. The first request and 4 excess requests went through at once; the other 6 queued, and the last left after 1.20 seconds (6 × 0.2 s). A page's few parallel requests load without waiting, while a client that keeps going is slowed, then refused.
limit_req_status 429. Identical to nodelay, but the 9 refusals said 429 Too Many Requests, which RFC 6585 defines for a client that "has sent too many requests in a given amount of time". A 503 reads as an outage to clients and monitoring, as 503 Service Unavailable explains.
04What limit_req writes to the error log
Refusals are logged at error level, delays one level lower at warn. nginx's error_log defaults to error, so delay lines appear only if the log level is warn or lower, as in the test; limit_req_log_level shifts both. The three lines below are copied unchanged from the test.
excess is how far the client is ahead of the rate, in requests: 1.000 in zone a is one request too many with no burst allowed, and 10.915 in zone c means the burst of 10 was full. The delayed request with excess 0.995 waited about 0.995 ÷ 5 = 0.2 seconds. Delay lines have a comma before "by zone" and refusal lines do not, so search for the zone name rather than the whole phrase.
2026/11/11 23:58:23 [error] 3066489#3066489: *4 limiting requests, excess: 1.000 by zone "a", client: 127.0.0.1, server: , request: "GET /plain/ HTTP/1.1", host: "127.0.0.1:18090"
2026/11/11 23:58:27 [warn] 3066489#3066489: *21 delaying request, excess: 0.995, by zone "b", client: 127.0.0.1, server: , request: "GET /burst/ HTTP/1.1", host: "127.0.0.1:18090"
2026/11/11 23:58:33 [error] 3066489#3066489: *49 limiting requests, excess: 10.915 by zone "c", client: 127.0.0.1, server: , request: "GET /nodelay/ HTTP/1.1", host: "127.0.0.1:18090"
# refusals per zone
grep 'limiting requests' /var/log/nginx/error.log | grep -o 'by zone "[^"]*"' | sort | uniq -c
# addresses refused most often
grep 'limiting requests' /var/log/nginx/error.log | grep -o 'client: [^,]*' | sort | uniq -c | sort -rn | head05Why limit_req does nothing in a location that only returns
The most common reason a limit seems to do nothing is a test against a location that answers with return. In the same setup, a location with limit_req and only return 200 answered 20 of 20 simultaneous requests with 200.
nginx handles a request in phases. return belongs to the rewrite module and runs in the rewrite phase; limit_req and limit_conn run in the later preaccess phase, as the nginx development guide lists. return ends processing first, inside if or at server level too. Locations that serve files, use try_files or hand the request to proxy_pass or fastcgi_pass reach the preaccess phase, so their limits apply.
A second trap looks the same from outside: limit_req is inherited from the level above only if the current level has none. A location with its own limit_req line drops the server-level limit, so repeat that line there if both should apply.
# Not limited: return answers before limit_req runs
location = /ping {
limit_req zone=perip;
return 200 "pong\n";
}
# Limited: serve a file or proxy the request instead
location = /ping {
limit_req zone=perip;
default_type text/plain;
try_files /ping.txt =404;
}06The directives, defaults and zone size
Values from the nginx documentation. limit_req_zone belongs in the http block; the others work in http, server and location.
Zone size: $binary_remote_addr takes 4 bytes for IPv4 and 16 for IPv6, and a stored state takes 64 bytes on 32-bit and 128 bytes on 64-bit platforms. So 1 MB holds about 16,000 states on 32-bit systems but about 8,000 on 64-bit servers, and 10m tracks roughly 80,000 addresses. A full zone evicts the least recently used state; if that is not enough, the request is refused. Longer keys, such as API tokens, can need more room per state.
Before enforcing a limit, run it with limit_req_dry_run and log $limit_req_status: nothing is refused, the zone counts as usual, and the access log shows who would have been.
| Directive or variable | Default | Since | What it does |
|---|---|---|---|
| limit_req_zone key zone=name:size rate=rate | none | 0.7.21 (module) | Key, shared memory zone and rate, in r/s or r/m |
| limit_req zone=name [burst=n] [nodelay | delay=n] | burst=0 | delay: 1.15.7 | Applies a zone; several are allowed |
| limit_req_status code | 503 | 1.3.15 | Status for refused requests |
| limit_req_log_level level | error | 0.8.18 | Refusals at this level, delays one lower |
| limit_req_dry_run on | off | off | 1.17.1 | Counts without refusing or delaying |
| $limit_req_status | none | 1.17.6 | PASSED, DELAYED, REJECTED, DELAYED_DRY_RUN or REJECTED_DRY_RUN |
# http {}
log_format ratelimit '$remote_addr [$time_local] "$request" $status $limit_req_status';
# server {} or location {}
limit_req zone=perip burst=10 nodelay;
limit_req_dry_run on; # count only, refuse nothing
access_log /var/log/nginx/ratelimit.log ratelimit;
# addresses that would have been refused
awk '$NF == "REJECTED_DRY_RUN" {print $1}' /var/log/nginx/ratelimit.log | sort | uniq -c | sort -rn | head07Several limits at once, and exempting trusted addresses
A level can carry several limit_req lines; the nginx documentation's example limits each address and the whole virtual server at once. The NGINX blog adds that all matching limits apply, so the most restrictive one wins, and of several delays the longest is used.
To exempt monitoring or an office, give it an empty key: nginx does not count requests whose key is empty. A geo block marks the trusted addresses, and a map turns the mark into either an empty string or the client address. Exempt only ranges you control.
# http {}
geo $limited {
default 1;
10.0.0.0/8 0; # internal services
192.0.2.0/24 0; # office or monitoring (documentation range)
}
map $limited $limit_key {
0 "";
1 $binary_remote_addr;
}
limit_req_zone $limit_key zone=perip:10m rate=5r/s;
limit_req_zone $server_name zone=perserver:10m rate=50r/s;
# server {}
limit_req zone=perip burst=10 nodelay;
limit_req zone=perserver burst=100;08API rate limiting with nginx
For an API, the address is often the wrong key: customers share NAT and cloud addresses, and one customer may call from many. The key can be any variable, so a zone can count per API key. Requests without the header have an empty key and escape that zone, and every made-up key starts with an empty bucket, so keep a looser per-address zone beside it.
Answer with 429 and Retry-After. nginx does not add Retry-After by itself, but a named error location can, as 429 Too Many Requests shows.
# http {}
limit_req_zone $http_x_api_key zone=perkey:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=apiip:10m rate=50r/s;
limit_req_status 429;
# server {}
location /api/ {
limit_req zone=perkey burst=20 nodelay;
limit_req zone=apiip burst=100 nodelay;
proxy_pass http://127.0.0.1:9000;
}09Behind a CDN or load balancer: limit visitors, not the proxy
Behind a CDN, a load balancer or any other reverse proxy, every connection comes from the proxy, so $binary_remote_addr is the proxy's address and all visitors share one bucket. Restore the client address with the realip module first, trusting the header only from the proxy's ranges; limit_req then keys on the restored address. The setup is in nginx real IP, and why headers from anyone else must be ignored in the X-Forwarded-For header.
# http {}
set_real_ip_from 192.0.2.0/24; # your CDN or load balancer
set_real_ip_from 2001:db8::/32;
real_ip_header X-Forwarded-For;
real_ip_recursive on;10limit_conn: cap parallel requests per client
limit_conn caps how many requests one key has in progress at once, which suits downloads and slow endpoints. A connection counts only while a request is being processed and its header has been read completely; under HTTP/2 and HTTP/3, each concurrent request counts.
A state takes 64 bytes on 64-bit platforms, about 16,000 per MB. Unlike limit_req, a full zone does not evict: nginx returns the error to every further request, so size it generously. limit_conn_status also defaults to 503, and limit_conn_dry_run exists since 1.17.6.
# http {}
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn_zone $server_name zone=vhost:10m;
limit_conn_status 429;
# server {}
limit_conn addr 10;
limit_conn vhost 100;11Apache and Caddy: per-client limits need a module
Not reproduced; from each project's documentation. The Apache httpd 2.4 module index lists no module that limits requests per client over time. mod_ratelimit limits bandwidth per response, "not aggregated at IP/client level", and holds a server thread or process for each throttled transfer. Per-client request limits need a third-party module such as mod_qos.
Caddy's documentation says its rate_limit handler "does not come with Caddy". It is the caddy-ratelimit plugin, not an official Caddy repository, added with xcaddy or Caddy's download page. It counts events per key in a sliding window, answers 429 and sets Retry-After itself.
# Apache + mod_qos (third-party): 10 requests to wp-login.php per address per hour
SetEnvIf Request_URI ^/wp-login.php LimitLogin
QS_ClientEventLimitCount 10 3600 LimitLogin
# Caddy, built with: xcaddy build --with github.com/mholt/caddy-ratelimit
example.com {
rate_limit {
zone login {
match {
path /login
}
key {remote_host}
events 5
window 1m
}
}
reverse_proxy 127.0.0.1:8080
}12WordPress: limit wp-login.php and xmlrpc.php before PHP
Not reproduced here; from the WordPress documentation. Its brute-force guide advises rate-limiting logins "at the edge (WAF/CDN) or the web server", because plugins "still execute within PHP". It names xmlrpc.php, especially system.multicall, as a frequent brute-force target and says to disable XML-RPC if you do not use it. Each exact location needs its own PHP handler lines, as below. A variant that counts only POSTs, so the login form always loads, was tested in WordPress WAF.
# http {}
limit_req_zone $binary_remote_addr zone=logins:10m rate=10r/m;
limit_req_status 429;
# server {}
location = /wp-login.php {
limit_req zone=logins burst=20 nodelay;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
location = /xmlrpc.php {
deny all; # or the same limit and PHP lines if an app needs XML-RPC
}13Cloudflare: rate limiting rules
Not reproduced; from Cloudflare's documentation, plans checked on 12 November 2026. Rate limiting rules count requests that match an expression. The Free plan gets one rule, counting by IP over 10 seconds with a 10-second mitigation timeout; Pro gets two rules, periods up to a minute and timeouts up to an hour. Block answers 429 by default, and the visitor side is described in error 1015. nginx behind Cloudflare sees Cloudflare's addresses, so restore the visitor's address before adding limits there too.
| Dashboard field | Example for a login route |
|---|---|
| When incoming requests match | http.request.uri.path eq "/login" |
| With the same characteristics | IP |
| When rate exceeds | 5 requests per 10 seconds |
| Then take action | Block |
| For duration | 10 seconds |
14Kubernetes ingress-nginx: rate-limit annotations
Not reproduced; from the ingress-nginx documentation. limit-rps and limit-rpm set a per-address rate, the burst is that rate times limit-burst-multiplier (default 5), limit-connections caps concurrent connections, and limit-whitelist exempts a comma-separated list of CIDRs. Refusals get 503 unless the controller's ConfigMap sets limit-req-status-code, or limit-conn-status-code for limit-connections. Annotation values must be quoted strings.
Limits apply per controller replica, so with three replicas a client can get up to three times the configured rate. Kubernetes announced in November 2025 that best-effort maintenance of ingress-nginx would end in March 2026, with no further releases, bug fixes or security updates after that; the project's GitHub repository was archived on 24 March 2026. Existing deployments keep working, and Kubernetes recommends moving to Gateway API or another Ingress controller.
metadata:
annotations:
nginx.ingress.kubernetes.io/limit-rps: "5"
nginx.ingress.kubernetes.io/limit-burst-multiplier: "2" # burst of 10
nginx.ingress.kubernetes.io/limit-connections: "20"
nginx.ingress.kubernetes.io/limit-whitelist: "10.0.0.0/8,192.0.2.0/24"
# controller ConfigMap
data:
limit-req-status-code: "429"
limit-conn-status-code: "429"15What per-IP rate limiting cannot stop
A per-address limit assumes the abuse comes from a few addresses. A flood from thousands of addresses can keep each one under 5r/s and still overwhelm the server, and every refused request has already cost a connection and, over HTTPS, a TLS handshake. Keep limits for logins, APIs and single-source abuse, and have distributed floods filtered before they reach the server; see nginx DDoS protection and HTTP floods.
Questions
Frequently asked questions
- 01What is the difference between burst and nodelay in nginx?
- burst sets how many requests above the rate nginx accepts; nodelay sets whether they wait. At 5r/s with burst=10, 20 simultaneous requests gave 11 successes either way: spread over 2 seconds without nodelay, within 0.01 seconds with it.
- 02What does delay do in limit_req?
- delay=N (nginx 1.15.7 and later) serves the first N excess requests at once and queues the rest of the burst. With burst=10 delay=4 at 5r/s, 5 of 20 simultaneous requests passed at once, 6 more within 1.2 seconds, and 9 were refused.
- 03How do I make nginx rate limiting return 429 instead of 503?
- Set limit_req_status 429; and, for connection limits, limit_conn_status 429; in the http, server or location block. Both default to 503, which looks like an outage. nginx adds no Retry-After header itself; send it from a named error location.
- 04How much memory does a limit_req zone need?
- With $binary_remote_addr, a state takes 128 bytes on 64-bit platforms: about 8,000 addresses per MB, or 80,000 in 10m. The often-quoted 16,000 per MB is the figure for 32-bit platforms.
- 05How do I exclude an IP address from nginx rate limiting?
- Give it an empty key. A geo block marks trusted addresses with 0, a map turns 0 into an empty string and 1 into $binary_remote_addr, and limit_req_zone keys on the map's result. Requests with an empty key are not counted.
- 06Why is my nginx rate limit not working?
- Usually the location answers with return, which runs before limit_req; or it has its own limit_req line and no longer inherits the server-level one; or limit_req_dry_run is still on. Test with parallel requests against a path that serves real content.
Sources
Sources and verification
limit_req behaviour measured with nginx 1.22.1 on Debian 12: 20 concurrent requests per variant against a loopback test server, November 2026. Apache, Caddy, WordPress, Cloudflare and ingress-nginx settings are taken from their official documentation.
- 01Module ngx_http_limit_req_module · nginx.org
- 02Module ngx_http_limit_conn_module · nginx.org
- 03nginx development guide: HTTP phases · nginx.org
- 04Rate Limiting with NGINX · NGINX blog
- 05RFC 6585: Additional HTTP Status Codes, section 4 (429 Too Many Requests) · RFC Editor
- 06Apache Module mod_ratelimit · Apache HTTP Server documentation
- 07Module Index, Apache HTTP Server 2.4 · Apache HTTP Server documentation
- 08mod_qos: QS_ClientEventLimitCount · mod_qos project
- 09Module http.handlers.rate_limit · Caddy documentation
- 10caddy-ratelimit README · GitHub
- 11Brute Force Attacks · WordPress Developer Resources
- 12Rate limiting rules: availability · Cloudflare docs
- 13Rate limiting parameters · Cloudflare docs
- 14Ingress-Nginx annotations: rate limiting · Kubernetes ingress-nginx docs
- 15Ingress NGINX Retirement: What You Need to Know (11 November 2025) · Kubernetes blog
- 16Ingress-Nginx ConfigMap: limit-req-status-code and limit-conn-status-code · Kubernetes ingress-nginx docs
- 17kubernetes/ingress-nginx repository (archived 24 March 2026) · GitHub
Last updated