Rate Limiting
limit_req, burst and nodelay, limit_conn, and choosing keys
Rate limits protect backends from floods, slow down password guessing, and keep one client from using everything. Nginx implements them with the leaky bucket algorithm, with state in shared memory so all workers count together.
How the bucket works
rate=20r/s means one request every 50 ms, not "20 per clock second."
A request arriving sooner than that is excess. burst=40 lets up to 40
excess requests be accepted; beyond that, 429. Then:
- Without
nodelay, excess requests wait in the queue and are released at the rate: smooth for the backend, slow for the client. - With
nodelay, accepted excess requests are processed at once; only the slots in the bucket drain at the rate.
Measured
docker compose -f compose_prod.yaml run --rm labs python rate_limits.py
100 requests at once to /api/whoami (20 r/s, burst 40, nodelay):
200: 41, 429: 59
steady 15 r/s for 4 s (under the rate):
200: 60
8 logins at once (1 r/s, burst 3, no nodelay):
200 after 0.01s
429 after 0.01s
429 after 0.01s
429 after 0.01s
429 after 0.01s
200 after 2.02s
200 after 2.02s
200 after 3.02s- 200: 41
- 429: 59
- served at once: 1
- queued, served at 2.0, 2.0, 3.0 s: 3
- 429 at once: 4
- API: 1 request plus a burst of 40 got through immediately; the other 59 were rejected at once. A steady 15 r/s was never limited.
- Login: 1 served at once, 3 queued and served over the next seconds, 4 rejected immediately. A password-guessing script gets one try per second at most, and a real user who double-clicks still gets in.
The failover lab tripped this limit by accident: it sent 30 requests as
fast as possible, and its third round came back 29 × 429. The lab now
paces itself at 15 r/s. The same happens to real clients: a batch job,
a retry loop, a single-page app firing 50 API calls on load. Size burst
for legitimate bursts and tell clients the limit
(Retry-After, documentation).
Choosing the key
| Key | Limits | Watch out |
|---|---|---|
$binary_remote_addr | Each client IP | Offices, mobile carriers, CGNAT: thousands of users share an IP |
$http_authorization or an API-key header | Each API key | Unauthenticated requests share the empty key; limit those by IP |
$server_name | A whole site | One abuser blocks everyone |
Behind a CDN or load balancer, $remote_addr is the CDN. Set up realip
first (see the reverse proxy lesson),
or every client shares one bucket.
limit_conn
limit_conn perip 50 caps concurrent connections per IP. It defends
against slow clients holding many connections open (Slowloris-style
attacks) more than against request floods. Over HTTP/2, a browser uses one
connection for everything, so per-request limits matter more.
Lab
- Run
rate_limits.py. Then removenodelayfrom/api/, reload, and run it again. How long does the flood take, and how many 200s? - Add
limit_req_dry_run on;and watch the error log: the limit is logged but not enforced. Why is that useful before turning a limit on? - Send requests from two different containers. Do they share a bucket?