Rate-limit key and observability
A limit is only as good as its key
The key groups requests into a common budget. An IP-based limit is useful before login, but a school or office may share one public address through NAT. Conversely, an individual can use multiple addresses. For a signed-in service, a verified account or tenant identifier may better represent fairness. Do not use a raw client-supplied identity header or token as a key: a client can change it, and sensitive values may leak through diagnostics. Nginx limit checks occur before ordinary access-phase authentication, so do not assume an auth_request result is available as a limit key in the same request. Enforce verified-user quotas in the application or in a separately authenticated upstream gateway stage.
set_real_ip_from 192.0.2.10;real_ip_header X-Forwarded-For;real_ip_recursive on;The address is documentation-only; replace it with an actual, narrowly scoped trusted proxy and restrict reachability to that proxy where appropriate. Ensure the proxy sanitizes forwarding headers and defines the forwarding chain. This configuration requires the Real IP module. Direct untrusted peers must not be allowed to assert their own identity. The next chapter covers trust configuration in detail. Never trust every source merely to make a log look correct.
Observe the policy before enforcing it
log_format limits "$status rate=$limit_req_status conn=$limit_conn_status";access_log logs/limits.log limits;# Inside an existing location, use:# limit_req_dry_run on;# limit_conn_dry_run on;Dry run updates accounting but does not impose the corresponding limit. Request dry run requires 1.17.1 or later and connection dry run requires 1.17.6 or later. A request may still fail for unrelated reasons. Log the limiter outcomes beside the final HTTP status; omit raw credentials, cookies and identifying query strings. The request limiter can report PASSED, DELAYED, REJECTED, DELAYED_DRY_RUN or REJECTED_DRY_RUN; the connection limiter has PASSED, REJECTED and REJECTED_DRY_RUN. A missing value does not mean the request was counted successfully.
200 rate=PASSED conn=PASSED200 rate=REJECTED_DRY_RUN conn=PASSED429 rate=REJECTED conn=-The second line means the rate policy would have refused a request, but this request received 200. The third line identifies an enforced rate refusal, not necessarily a failure at the backend. A limiter might not run if an earlier phase already finished the request. Avoid reading a 429 count as a complete picture of traffic abuse; measure legitimate-client impact too.
Size zones and roll out safely
limit_req_zone $server_name zone=site_rate:2m rate=20r/s;limit_conn_zone $server_name zone=site_concurrent:1m;Use a bounded key population. For address-keyed state on a 64-bit build, the documentation estimates around 8,000 rate records or 16,000 connection records per MiB. Treat those figures as approximate, not a guarantee: key sizes and allocation overhead matter. Empty keys are excluded from accounting. When rate-zone memory fills, older state can be discarded; connection-zone exhaustion can cause refusals. Monitor capacity rather than accepting arbitrary key cardinality. A local shared zone coordinates workers, not independent hosts. Four independent replicas with the same limit can collectively admit more traffic than one; uneven routing complicates any simple multiplication. Commercial synchronization for request-rate zones is a separate capability; verify edition and configuration before using it. Newer commercial variable-rate support (1.31.3) is not part of the 1.26.3 lab.
Practice and rollout checklist
Review a fictional dry-run sample: 200 requests, of which 24 would be rejected. Check which legitimate clients share an address, which endpoints are costly and whether application latency improves. Review parent/child inheritance: defining any limit_req at a child level replaces the inherited set for that directive; the same rule applies independently to limit_conn. Preserve aggregate and per-client rules explicitly when overriding. Use a staged rollout, observe errors, adjust and keep a rollback. A local limiter does not prevent bandwidth saturation, slow-header pressure or all distributed abuse; coordinate upstream defenses and application controls. Next: IP access, Real IP and PROXY protocol.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Rate-limit key and observability
Choose a trustworthy key, observe enforcement in dry-run mode, and design a cautious rollout with local and distributed limits.
A public client supplies a different X-User header for each call. Should it define a verified-user quota?
What does a 200 with REJECTED_DRY_RUN mean?
Four independent Nginx replicas each have a local rate zone. What is shared across replicas by default?
Why might a per-IP quota unfairly throttle a school?
A child location defines a new limit_req directive. What happens to inherited limit_req rules?