





- the client IP address,
- the time between requests,
- and the number of requests within a time window.
- Request rate limiting (
limit_req) — caps requests per time unit (token-bucket style). - Connection rate limiting (
limit_conn) — caps concurrent connections per key (commonly per IP).
2r/m) using a token-bucket / leaky-bucket algorithm. Short bursts can be tolerated or smoothed using parameters such as burst and delay. Excess requests are either delayed or rejected based on configuration.

/admin location. It includes:
limit_req_zone(defined in thehttpcontext) — creates an in-memory zone storing client counters and sets the rate,limit_req_status(optional) — sets the response status when the limit is exceeded (default is503),limit_req(in alocation) — applies the zone to requests.
$binary_remote_addr: efficient binary representation of the client IP used as the tracking key. If Nginx is behind a proxy/load balancer, use thereal_ipmodule andX-Forwarded-For/X-Real-IPheaders to obtain the client IP.zone=req_limit_per_ip:10m: shared memory zone name and size (10 MB). The zone stores counters and state required for limiting.rate=2r/m: allows 2 requests per minute per key. User/sfor requests per second (e.g.,1r/s).limit_req_status 429: returns HTTP 429 (Too Many Requests) instead of the default503. 429 is the correct semantic response for throttling.
Tip: Prefer
429 for rate-limited responses so clients and automated clients understand they are being throttled. Also ensure that, if Nginx is behind proxies, you correctly set the real client IP (see the Nginx real_ip module).
limit_conn_zonein thehttpcontext — defines the tracking zone,limit_conn_status— sets the response status when the limit is exceeded,limit_connin thelocation— applies the connection limit.
/admin:
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;— uses the client IP as the key and reserves 10 MB for tracking.limit_conn conn_limit_per_ip 1;— allows 1 concurrent connection per key (increase as appropriate).
Best practices
- Return HTTP
429so clients know they are being throttled. - Set zone sizes based on expected number of unique clients. Each tracked client consumes memory.
- Apply stricter limits to sensitive endpoints (logins, account actions, API write endpoints) and looser limits for public content.
- Combine both
limit_reqandlimit_connto mitigate different attack vectors (bursts vs. connection floods). - Monitor logs and metrics after enabling limits to avoid accidentally blocking legitimate traffic.
Warning: Misconfigured limits can block legitimate users or cause unexpected behavior. Start conservatively, monitor traffic, and adjust thresholds and zone sizes. If Nginx is behind proxies, ensure you use the correct client IP via the
real_ip or proxy_protocol configuration.