proxy_pass 의 trailing slash(/) 의미proxy_pass 끝에 / 가 붙는지 여부에 따라, location prefix를 백엔드로 전달할지 말지가 결정된다.
| 설정 | 클라이언트 요청 | 백엔드로 전달되는 경로 |
|---|---|---|
proxy_pass http://localhost:8080; | /detector/api | /detector/api (prefix 유지) |
proxy_pass http://localhost:8080/; | /detector/api | /api (prefix 제거) |
/ 없음 → location prefix를 유지한 채로 전달/ 있음 → location prefix를 제거하고 나머지 경로만 전달# prefix 유지: /detector/api → localhost:8080/detector/api
location /detector/ {
proxy_pass http://localhost:8080;
}
# prefix 제거: /detector/api → localhost:8080/api
location /detector/ {
proxy_pass http://localhost:8080/;
}
location 설정 3가지 방법입력한 prefix로 시작하는 모든 경로를 매칭한다.
location /detector/ {
# /detector/, /detector/api/test 등 모두 매칭
}
⚠️
/detector/로 설정하면/detector(trailing slash 없음) 는 매칭되지 않는다.
두 경우를 모두 처리하려면 아래처럼 리다이렉트를 추가한다.location = /detector { return 301 /detector/; }
입력한 경로와 정확히 일치하는 요청만 매칭한다.
location = /detector {
# /detector 와 정확히 일치하는 요청만 매칭
}
정규식 패턴에 매칭되는 경로를 처리한다.
location ~ ^/detector/\d+ {
# /detector/123, /detector/456 등 매칭
}
⚠️ 정규식 location에서는
proxy_pass에 URI(trailing slash)를 붙일 수 없다.
nginx가 설정 로드 시점에 에러를 발생시키므로, 아예 허용되지 않는다.# ❌ 설정 에러 발생 location ~ ^/detector/\d+ { proxy_pass http://localhost:8080/; } # ✅ URI 없이만 사용 가능 location ~ ^/detector/\d+ { proxy_pass http://localhost:8080; }따라서 정규식 location에서 prefix를 제거하고 싶을 때는
rewrite를 사용한다.
rewrite 란?요청 URI를 내부적으로 변경하는 기능이다.
정규식 location에서 prefix 제거가 필요할 때 주로 활용한다.
location ~ ^/detector/(.*)$ {
rewrite ^/detector/(.*)$ /$1 break;
proxy_pass http://localhost:8080;
# /detector/api/test → localhost:8080/api/test
}