GrepBoard/Manuals
605 manuals ·

Change Nginx limit_req from 503 to 429 Too Many Requests

·Tested on Ubuntu 24.04 · Nginx 1.24.0 (tested with curl)·Test scope ↓·8 steps

limit_req rejects requests with 503 by default, like an outage. Confirm it in error.log, switch to 429 with limit_req_status, compare burst/nodelay.

If clients get 503 Service Temporarily Unavailable after you add limit_req, the server may not be down at all — 503 is simply the default rejection code. The output below comes from Nginx on port 18801 with a series of curl requests.

[1] Check error.log to see whether the 503 came from the limiter. If this line is there, limit_req rejected the request. excess is how far the request went over the allowed rate, and the zone name tells you which limit fired.

console
# grep 'limiting requests' /var/log/nginx/error.log | tail -n 32026/10/19 14:49:43 [error] …: *2 limiting requests, excess: 0.978 by zone "perip", client: 203.0.113.7, server: example.com, request: "GET /a/ HTTP/1.1", host: "example.com"

[2] Find the current limit settings. limit_req_zone belongs in http; limit_req goes in http, server or location.

console
# nginx -T 2>/dev/null | grep -nE 'limit_req'

[3] Reproduce it. With a limit of 2 requests per second (rate=2r/s), six back-to-back requests let only the first one through.

/etc/nginx/conf.d/example.com.conf
limit_req_zone $binary_remote_addr zone=perip:1m rate=2r/s;server {    listen 18801;    root /srv/www;    location /a/ { limit_req zone=perip; }}
console
$ for i in 1 2 3 4 5 6; do curl -s -o /dev/null -w '%{http_code} ' http://127.0.0.1:18801/a/; done; echo200 503 503 503 503 503

[4] Change the rejection code with limit_req_status. It works in http, server and location.

conf
        location /d/ { limit_req zone=perip; limit_req_status 429; }
console
$ for i in 1 2 3 4; do curl -s -o /dev/null -w '%{http_code} ' http://127.0.0.1:18801/d/; done; echo200 429 429 429$ curl -si http://127.0.0.1:18801/d/ | head -1HTTP/1.1 429 Too Many Requests

If several locations share the limit, set it once in http instead.

conf
http {    limit_req_zone $binary_remote_addr zone=perip:1m rate=2r/s;    limit_req_status 429;

[5] Test the config and reload. A reload does not drop open connections.

console
# nginx -t && systemctl reload nginx

[6] With burst, excess requests queue instead of being rejected. At rate=2r/s and burst=5, eight requests all returned 200, and every one after the first took about 0.49 seconds. Nginx releases them at the allowed rate (one per 0.5 s).

conf
        location /b/ { limit_req zone=perip burst=5; }
console
$ for i in $(seq 8); do curl -s -o /dev/null -w '%{http_code} %{time_total}\n' http://127.0.0.1:18801/b/; done200 0.000353200 0.493341200 0.488397200 0.489918200 0.489037200 0.489441200 0.489050200 0.492385

error.log records these as delaying request at [warn]. A delay is logged one level below the configured limit_req_log_level (default error).

[7] Add nodelay and requests within the burst pass immediately; everything after is rejected. In the same eight-request run, the first six returned 200 at once and the 7th and 8th were refused.

conf
        location /c/ { limit_req zone=perip burst=5 nodelay; }
console
$ for i in $(seq 8); do curl -s -o /dev/null -w '%{http_code} %{time_total}\n' http://127.0.0.1:18801/c/; done200 0.000530200 0.000493200 0.000510200 0.000484200 0.000480200 0.000438503 0.000445503 0.000467
SettingEight requests in a row
limit_req zone=perip;First is 200, the rest rejected at once
burst=5All 200, each after the first delayed about 0.49 s
burst=5 nodelayFirst six 200 at once, then rejected at once

nodelay and limit_req_status 429 are independent directives and can be used together (that exact combination was not run).

[8] To log rejections at [warn] instead of [error], add limit_req_log_level warn;. In the test the rejection lines came out as [warn].

console
2026/10/19 14:50:00 [warn] …: *2 limiting requests, excess: 0.978 by zone "perip", client: 203.0.113.7, …

Source: nginx.org — ngx_http_limit_req_module

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

Check it with the HTTP status codes tool ↗ — calculated in your browser.

#nginx#rate-limit#429#503