Lesson 6 of 9 · 23m
Rate Limiting and Abuse Shaping
limit_req, limit_conn, burst behavior, multi-layer throttles, and how to protect login and API endpoints without punishing normal users.
Rate limiting is traffic policy, not magic abuse detection. The wrong limit does nothing against a distributed botnet and everything against a customer behind carrier-grade NAT. The right design uses several layers: nginx rejects cheap floods early, application logic applies identity-aware rules, and upstream systems enforce business-specific constraints.
Understand the leaky bucket before tuning it
limit_req_zone declares shared memory and a rate. limit_req applies it.
limit_req_zone $binary_remote_addr zone=login_per_ip:10m rate=5r/m;
limit_req_status 429;
location = /login {
limit_req zone=login_per_ip burst=10;
proxy_pass http://auth_upstream;
}rate=5r/m means one request every 12 seconds on average, not five requests whenever you feel like it. Excess requests are delayed until the bucket has capacity. burst=10 permits a queue of ten excess requests. Add nodelay only if bursts should be admitted immediately rather than smoothed.
Delayed requests can be worse than rejected ones for interactive abuse endpoints because they consume connections while attackers wait. Sometimes immediate rejection is kinder to the system.
Connection limits answer a different question
limit_conn_zone $binary_remote_addr zone=addr_conn:10m;
location /downloads/ {
limit_conn addr_conn 10;
proxy_pass http://files_upstream;
}limit_conn counts concurrent connections, not requests over time. Ten parallel downloads may be reasonable for a browser pulling assets but abusive for large files. Requests and connections are separate dimensions; use both deliberately.
The key is the policy
$binary_remote_addr is compact and simple, but it treats every user behind the same NAT as one person. Keying only on a user ID means unauthenticated attacks bypass the rule. Good designs layer keys:
limit_req_zone $binary_remote_addr zone=edge_ip:20m rate=30r/s;
limit_req_zone $http_x_api_key zone=api_key:10m rate=10r/s;Only use client-supplied identity headers if nginx or trusted infrastructure normalizes them. If you are behind a CDN, $remote_addr may be the CDN node, not the client. Restoring real client addresses requires controlled trusted-proxy handling; blindly reading X-Forwarded-For hands attackers a rate-limit bypass.
Make failure visible and graceful
Return 429, log the zone and key class, and emit a response clients can understand:
limit_req_log_level warn;
limit_req_status 429;
error_page 429 @ratelimited;
location @ratelimited { default_type application/json; return 429 '{"error":"rate_limited","retry":"slow down"}'; }Measure before tightening. A limit should reflect observed legitimate behavior plus headroom. Emergency controls can be stricter, but permanent controls need data.
Field notes
- Rate limiting protects capacity; it does not replace authentication, validation, or bot detection.
- Health checks and internal probes often need exemption, but do not exempt them by making them publicly unauthenticated.
- Distributed attacks need edge/CDN controls too; one nginx host cannot absorb what its network pipe cannot receive.
Was this lesson helpful?