Learn Labs
Caching & Limits

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.

http {}: a zone per policy, keyed by client IP (binary form: 4 bytes, not 15)limit_req_zone $binary_remote_addr zone=api:10m rate=20r/s;limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;limit_conn_zone $binary_remote_addr zone=perip:10m;limit_req_status 429;default is 503, which looks like an outagelimit_conn_status 429;location /api/ { limit_req zone=api burst=40 nodelay; ... }location = /api/login { limit_req zone=login burst=3; ... }server { limit_conn perip 50; ... }concurrent connections per IP

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
/api/: 100 requests at once, rate=20r/s burst=40 nodelay100
  • 200: 41
  • 429: 59
/api/login: 8 at once, rate=1r/s burst=3 (no nodelay)8
  • 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.

Choosing the key

KeyLimitsWatch out
$binary_remote_addrEach client IPOffices, mobile carriers, CGNAT: thousands of users share an IP
$http_authorization or an API-key headerEach API keyUnauthenticated requests share the empty key; limit those by IP
$server_nameA whole siteOne 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

  1. Run rate_limits.py. Then remove nodelay from /api/, reload, and run it again. How long does the flood take, and how many 200s?
  2. 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?
  3. Send requests from two different containers. Do they share a bucket?

On this page