GrepBoard/매뉴얼
605 편 ·

nginx location cannot be inside the exact location 해결

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

location cannot be inside the exact location 은 = 로 시작하는 location 안에 다른 location 이 들어 있다는 뜻입니다. 두 블록을 형제로 옮겨 다시 검사합니다.

output
2026/11/27 23:28:46 [emerg] 12#12: location "/demo/child" cannot be inside the exact location "/demo" in /lab/before.conf:6nginx: [emerg] location "/demo/child" cannot be inside the exact location "/demo" in /lab/before.conf:6nginx: configuration file /lab/before.conf test failed

location = /demo { … } 처럼 = 가 붙은 location 은 안쪽에 다른 location 을 둘 수 없습니다. 오류의 첫 번째 따옴표는 안쪽(자식), 두 번째는 바깥쪽(부모)입니다. 자식을 부모 밖으로 꺼내 같은 server 안의 형제로 두면 해결됩니다.

[1] 오류 끝의 파일과 줄 번호(before.conf:6)를 엽니다. 이 번호는 안쪽 location 이 시작하는 줄입니다. 시험에서는 컨테이너 안에서 nginx -t -c /lab/before.conf 를 실행했습니다. include 로 들어온 조각이면 오류에 찍힌 파일을 엽니다.

/lab/before.conf
events {}http {    server {        listen 127.0.0.1:18080;        location = /demo {            location /demo/child {                return 200 "child\n";            }        }    }}

[2] 바깥 location 의 수정자를 봅니다. 위에서는 6번째 줄 바로 위의 location = /demo 입니다. = 는 요청 경로가 정확히 /demo 일 때만 맞는 정확 일치입니다. nginx 1.28.0 소스는 중첩 location 을 읽을 때 부모가 정확 일치이면 이 오류를 내고 설정 읽기를 멈춥니다. 같은 자리에서 부모가 @ 로 시작하는 이름 붙은 location 일 때는 cannot be inside the named location 이라는 별도 문구가 나옵니다.

[3] 안쪽 location 을 바깥으로 꺼내 같은 server 안에 나란히 둡니다. 정확 일치 블록에는 정확히 그 경로에 줄 응답만 남깁니다. 자식은 자기 경로(/demo/child)를 그대로 가지고 있어서 경로 이름은 바꿀 필요가 없습니다.

/lab/after.conf
events {}http {    server {        listen 127.0.0.1:18080;        location = /demo {            return 200 "demo\n";        }        location /demo/child {            return 200 "child\n";        }    }}

바깥 블록에 proxy_pass 나 root 같은 지시어가 있었다면, 자식이 형제가 되면서 그 지시어 밖으로 나온 것이므로 자식에도 필요한지 확인합니다.

[4] 고친 파일을 다시 검사합니다. 비교용으로 부모의 = 만 뺀 /lab/prefix.conf(location /demo { location /demo/child { … } })도 같이 돌렸습니다.

console
$ nginx -t -c /lab/after.conf
output
nginx: the configuration file /lab/after.conf syntax is oknginx: configuration file /lab/after.conf test is successful
console
$ nginx -t -c /lab/prefix.conf
output
nginx: the configuration file /lab/prefix.conf syntax is oknginx: configuration file /lab/prefix.conf test is successful

종료 코드는 수정 전 1, 수정 후 둘 다 0 이었습니다. 부모의 = 를 빼는 방법은 검사는 통과하지만 뜻이 바뀝니다. 접두어 /demo 는 /demo 로 시작하는 모든 경로(/demo2 포함)와 맞습니다. 정확히 한 경로만 받으려던 설정이라면 형제로 나누는 쪽을 씁니다.

[5] 검사를 통과하면 운영 방식대로 설정을 다시 읽히고(nginx -s reload) 두 경로를 요청해 응답을 확인합니다. nginx -t 는 문법 검사까지입니다. 어느 location 이 요청을 받을지는 요청을 보내 확인합니다.

형제로 옮긴 뒤 duplicate location 이 나오면 같은 경로의 location 이 둘이라는 뜻이니 nginx duplicate location 해결에서 겹친 두 파일을 찾습니다. 이름 붙은 @ location 쪽 오류는 nginx alias 를 이름 붙은 location 에서 쓸 때에서 다룹니다.

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

#nginx#location#nginx -t#exact-match