GrepBoard/매뉴얼
605 편 ·

nginx proxy_pass cannot have URI part 해결

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

정규식 location·if 안 proxy_pass 에 경로(끝 /도)가 있으면 cannot have URI part 오류입니다. 경로를 빼고 rewrite … break 나 접두사 location 으로 고칩니다.

output
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/conf.d/default.conf:11

정규식 location(~·~*)이나 이름 있는 location(@), if·limit_except 블록 안의 proxy_pass 에 주소 뒤 경로를 붙였다는 뜻입니다. 끝의 / 하나도 경로로 칩니다. 이런 자리에서는 요청 URI 의 어느 부분을 그 경로로 바꿔야 할지 nginx 가 정할 수 없어서 문법 단계에서 막습니다. proxy_pass 에서 경로를 빼고 rewrite … break 로 URI 를 바꾸거나, 정규식이 꼭 필요하지 않으면 접두사 location 으로 바꾸면 됩니다.

[1] nginx -t 가 가리키는 줄을 엽니다. 위 오류는 아래 설정의 11번 줄(proxy_pass)에서 났습니다.

/etc/nginx/conf.d/default.conf
    location ~ ^/api/(.*)$ {        proxy_pass http://127.0.0.1:8080/;    }

nginx 소스의 최신 판에는 이 문구에 or inside predicate location 이 더 붙어 있습니다. 문구가 조금 길어도 같은 검사입니다.

[2] 무엇으로 고칠지 고릅니다. 아래 세 방법은 모두 같은 요청 /api/users/7?fields=name 으로 시험했습니다. 뒤쪽 시험 서버는 받은 URI 를 backend got: … 으로 돌려줍니다.

상황고치는 법/api/users/7?fields=name 이 뒤로 갈 때
앞부분 /api/ 만 떼면 된다접두사 location /api/ + proxy_pass …/;/users/7?fields=name
정규식이 꼭 필요하다경로 없는 proxy_pass + rewrite … break/users/7?fields=name
변수로 경로를 만든다proxy_pass …/$1$is_args$args;/users/7?fields=name(단 인코딩 문제, [5])

고치기 전에 지금 설정을 conf.d 밖으로 복사해 둡니다. location 종류나 rewrite 를 바꾸면 nginx -t 를 통과해도 뒤로 가는 URI 가 달라질 수 있어서, [7] 에서 문제가 보이면 이 사본으로 되돌립니다.

console
$ cp -a /etc/nginx/conf.d/default.conf /root/default.conf.bak

[3] 정규식이 필요 없으면 접두사 location 으로 바꿉니다. 경로가 붙은 proxy_pass 는 접두사 location 에서는 location 과 맞은 앞부분을 그 경로로 바꿉니다.

/etc/nginx/conf.d/default.conf
    location /api/ {        proxy_pass http://127.0.0.1:8080/;    }
console
$ curl -s 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /users/7?fields=name

[4] 정규식을 그대로 두려면 proxy_pass 에서 경로를 빼고 rewrite … break 로 URI 를 바꿉니다. nginx 문서도 이 경우 proxy_pass 를 경로 없이 쓰라고 하고, rewrite … break 로 바뀐 URI 는 그대로 뒤로 넘긴다고 적어 둡니다.

/etc/nginx/conf.d/default.conf
    location ~ ^/api/(.*)$ {        rewrite ^/api/(.*)$ /$1 break;        proxy_pass http://127.0.0.1:8080;    }
console
$ curl -s 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /users/7?fields=name

쿼리 문자열도 따라갔고, %20 이 든 경로(/api/files/a%20b.txt)도 /files/a%20b.txt 로 인코딩된 채 넘어갔습니다.

[5] 변수로 경로를 만드는 방법은 검사를 통과하지만 두 군데서 어긋납니다. 변수가 든 proxy_pass 는 문서대로 그 값을 그대로 보냅니다.

/etc/nginx/conf.d/default.conf
    location ~ ^/api/(.*)$ {        proxy_pass http://127.0.0.1:8080/$1;    }
console
$ curl -s 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /users/7

쿼리 문자열이 사라졌습니다. 아래처럼 $is_args$args 를 붙이면 /users/7?fields=name 으로 돌아옵니다.

/etc/nginx/conf.d/default.conf
        proxy_pass http://127.0.0.1:8080/$1$is_args$args;

그런데 이 설정에서 %20 이 든 경로를 보내면 400 이 돌아왔습니다.

console
$ curl -s 'http://127.0.0.1/api/files/a%20b.txt'
output
<html><head><title>400 Bad Request</title></head>…

nginx 문서에 따르면 location 은 %XX 를 푼 정규화된 URI 로 맞춥니다. 그래서 $1 에는 a b.txt 처럼 풀린 값이 들어가고, 그 값이 다시 인코딩되지 않은 채 요청 줄에 실린 것으로 보입니다. 경로에 공백·한글·%2F 가 올 수 있으면 [4] 의 rewrite 를 씁니다. 변수에 호스트 이름이 들어가면 resolver 도 필요해집니다(no resolver defined 참고).

[6] if 안에서 걸렸으면 조건을 map 으로 빼고 rewrite 로 경로를 고릅니다. 아래는 X-Beta 헤더가 있으면 /beta/ 아래로 보내던 설정을 고친 것입니다.

/etc/nginx/conf.d/default.conf
map $http_x_beta $api_prefix {    default "";    ~.      /beta;} server {    …    location /api/ {        rewrite ^/api/(?<rest>.*)$ $api_prefix/$rest break;        proxy_pass http://127.0.0.1:8080;    }}
console
$ curl -s -H 'X-Beta: 1' 'http://127.0.0.1/api/users/7?fields=name'
output
backend got: /beta/users/7?fields=name

캡처에 이름(?<rest>)을 붙인 데는 이유가 있습니다. 처음에 (.*) 와 $1 로 썼더니 헤더가 있을 때 backend got: /beta/?fields=name 이 나왔습니다. 정규식이 든 map 이 평가되면서 번호 캡처가 덮인 것으로 보이며, 이름 붙인 캡처로 바꾸자 제대로 넘어갔습니다.

[7] nginx -t 를 통과하면 reload 하고, 실제 경로로 한 번 더 요청해 뒤쪽 접근 로그에 찍힌 URI 를 봅니다. nginx -t 는 문법만 보므로, 뒤로 가는 URI 가 맞는지는 이 요청으로만 확인됩니다. reload 뒤 404·400 이 늘었는데 원인을 바로 못 찾으면 먼저 [2] 에서 복사한 설정으로 되돌립니다.

console
$ cp -a /root/default.conf.bak /etc/nginx/conf.d/default.conf$ nginx -t && nginx -s reload

뒤쪽이 404·502 를 내면 nginx 502 Bad Gateway 로그 읽기로 넘어갑니다.

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

정규식 시험기(JS · grep -E · grep -P) 도구로 바로 확인 ↗ — 브라우저 안에서 계산합니다.

#nginx#proxy_pass#location#rewrite#regex