GrepBoard/매뉴얼
605 편 ·

Nginx limit_req 가 503 을 줄 때 429 로 바꾸기

·시험 환경 Ubuntu 24.04 · Nginx 1.24.0 (curl 로 시험)·시험 범위 ↓·8 단계

limit_req 가 막은 요청은 기본 503 이라 장애처럼 보입니다. error.log 로 확인하고 limit_req_status 429 로 바꾸며, burst·nodelay 차이도 재현했습니다.

limit_req 로 요청 수를 제한했더니 클라이언트가 503 Service Temporarily Unavailable 을 받는다면, 서버 장애가 아니라 제한에 걸린 것일 수 있습니다. 기본 거절 코드가 503 이기 때문입니다. 아래는 Nginx 를 포트 18801 로 띄워 curl 을 연달아 보낸 실제 결과입니다.

[1] 503 이 제한 때문인지 error.log 로 확인합니다. 아래 줄이 있으면 limit_req 가 거절한 것입니다. excess 는 허용 속도를 넘은 요청 수이고, zone 이름으로 어느 제한인지 알 수 있습니다.

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] 현재 제한 설정을 찾습니다. limit_req_zone 은 http 블록에, limit_req 는 http·server·location 에 둡니다.

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

[3] 재현합니다. 초당 2건(rate=2r/s)으로 제한한 위치에 6번 연달아 요청하면 첫 번째만 통과합니다.

/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] limit_req_status 로 거절 코드를 바꿉니다. 지시어는 http·server·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

여러 location 에 같은 제한이 걸려 있으면 http 블록에 한 번만 두는 편이 낫습니다.

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

[5] 설정을 검사하고 다시 읽힙니다. reload 는 열린 연결을 끊지 않습니다.

console
# nginx -t && systemctl reload nginx

[6] burst 를 주면 초과 요청이 거절되지 않고 줄을 섭니다. rate=2r/s 에 burst=5 로 8번 요청하면 첫 요청 뒤의 모든 요청이 0.49초씩 늦게 응답했고, 모두 200 이었습니다. 요청 간격을 허용 속도(0.5초)에 맞춰 내보내기 때문입니다.

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 에는 delaying request 줄이 [warn] 으로 남습니다. 거절이 아니라 지연이므로 사용 중인 limit_req_log_level(기본 error)보다 한 단계 낮게 기록됩니다.

[7] nodelay 를 붙이면 burst 만큼은 기다리지 않고 바로 통과시키고, 그 뒤부터 거절합니다. 같은 8번 요청에서 앞 6개는 즉시 200 이고 7번째부터 막혔습니다.

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
설정8번 연속 요청 결과
limit_req zone=perip;첫 요청만 200, 나머지 즉시 거절
burst=5모두 200, 둘째부터 약 0.49초씩 지연
burst=5 nodelay앞 6개 즉시 200, 이후 즉시 거절

nodelay 와 limit_req_status 429 는 서로 독립된 지시어라 함께 쓸 수 있습니다(조합은 따로 돌리지 않았습니다).

[8] 거절을 [error] 대신 [warn] 으로 기록하려면 limit_req_log_level warn; 을 둡니다. 시험에서 warn 을 두자 거절 줄이 [warn] 으로 남았습니다.

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

출처: nginx.org — ngx_http_limit_req_module

Nginx 로그를 브라우저에서 바로 분석 ↗ — 아무것도 올라가지 않습니다.

HTTP 상태 코드 도구로 바로 확인 ↗ — 브라우저 안에서 계산합니다.

#nginx#rate-limit#429#503