GrepBoard/매뉴얼
605 편 ·

nginx internal redirection cycle 500 해결

·시험 환경 nginx 1.30.6 (Docker) · 2026-10·시험 범위 ↓·5 단계

500 과 rewrite or internal redirection cycle 은 try_files 마지막 대상이 root 아래에 없다는 뜻입니다. 파일을 두거나 =404 로 끝냅니다.

output
2026/10/27 00:59:13 [error] 14#14: *2 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 127.0.0.1, server: , request: "GET /no-such-page HTTP/1.1", host: "127.0.0.1:8080"

없는 주소로 들어온 요청을 try_files 가 마지막 대상(/index.html)으로 넘겼는데, 그 대상도 root 아래에 없어서 같은 location 안에서 계속 맴돌다 멈춘 것입니다. 손님 화면에는 500 이 나갑니다. 끝에 붙은 while internally redirecting to "…" 가 범인인 대상 파일 이름이므로, 그 파일이 실제로 있는지부터 봅니다.

문서상 동작은 이렇습니다. try_files 는 앞의 파일들을 차례로 찾고, 하나도 없으면 마지막 인자로 내부 리다이렉트합니다. 리다이렉트된 /index.html 은 다시 location / 에 걸리고, 다시 try_files 를 돕니다. nginx 는 이 반복을 10번까지만 허용하고 오류로 끊습니다.

[1] 오류 줄에서 대상과 요청을 읽습니다. 위 줄은 GET /no-such-page 가 /index.html 로 넘어가다 끊긴 것입니다. 같은 시각 access.log 에는 500 이 남습니다.

output
127.0.0.1 - - [27/Oct/2026:00:59:13 +0000] "GET /no-such-page HTTP/1.1" 500 177 "-" "curl/8.14.1" "-"

[2] 실제로 적용된 root 와 try_files 를 봅니다. nginx -T 는 포함된 파일까지 펼쳐 보여 줍니다.

console
# nginx -T 2>/dev/null | grep -E '^\s*(root|try_files)'# ls -l /srv/www/public/index.html
output
    root /srv/www/public;        try_files $uri $uri/ /index.html;ls: cannot access '/srv/www/public/index.html': No such file or directory

마지막 인자가 /index.php?$args 처럼 다르면 ls 로 볼 파일도 그 이름(/srv/www/public/index.php)으로 바꿉니다.

root 와 마지막 대상을 이어 붙인 경로가 없으면 원인이 확정됩니다. 흔한 경우는 셋입니다.

상황확인
SPA 빌드 결과(index.html)를 다른 폴더에 둠root 가 빌드 폴더(dist·build)를 가리키는지
/index.php 로 넘기는데 location ~ \.php$ 가 없음PHP 를 넘길 블록이 있는지. 없으면 /index.php 도 location / 에 걸려 같은 고리가 됩니다(아래 재현)
root 오타 · 배포 중 폴더 교체배포 직후에만 나는지
output
2026/10/27 00:59:16 [error] 27#27: *2 rewrite or internal redirection cycle while internally redirecting to "/index.php", client: 127.0.0.1, server: , request: "GET /no-such-page HTTP/1.1", host: "127.0.0.1:8080"

[3] 정적 사이트라면 마지막 인자를 코드로 바꿉니다. 코드로 끝나면 리다이렉트가 없어 고리가 생기지 않습니다.

/etc/nginx/conf.d/site.conf
location / {   try_files $uri $uri/ =404;}

같은 요청이 500 대신 404 로 바뀌었습니다.

output
127.0.0.1 - - [27/Oct/2026:00:59:18 +0000] "GET /no-such-page HTTP/1.1" 404 153 "-" "curl/8.14.1" "-"

[4] SPA 처럼 모든 주소를 index.html 로 받아야 한다면 그 파일을 root 에 둡니다. 파일을 둔 뒤 같은 설정에서 없는 주소가 200 으로 index.html 을 받았습니다.

output
127.0.0.1 - - [27/Oct/2026:00:59:20 +0000] "GET /no-such-page HTTP/1.1" 200 4 "-" "curl/8.14.1" "-"

PHP 앱이라면 location ~ \.php$ { … fastcgi_pass …; } 블록을 같은 server 에 두어 /index.php 가 그쪽으로 가게 합니다. PHP-FPM 쪽 오류는 Primary script unknown 같은 다음 단계에서 갈립니다.

[5] 바꾼 뒤 검사하고 다시 읽힙니다. 없는 주소 하나로 상태 코드가 500 이 아닌지 확인합니다.

console
# nginx -t && systemctl reload nginx# curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/no-such-page

Apache 에서 같은 고리는 AH00124 로 나옵니다. AH00124 Request exceeded the limit of 10 internal redirects를 봅니다.

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

#nginx#try_files#500#error-log