페더레이션 인증 서버에서 요청을 수신하여 내 백엔드 앱으로 전달하는 역방향 프록시를 설정 중입니다. 요청에 후행 슬래시가 누락된 경우 nginx는 기본 301 리디렉션을 수행하지만 리디렉션되는 주소에는 위치 블록에서 일치하는 경로가 포함되지 않습니다. 아이디어는 Gunicorn의 upstream.com/myApp
루트 URL 에서 요청을 프록시하는 것입니다. backend.com/
내 구성은 다음과 같습니다
geo $allow {
default 0;
<upstream ip> 1;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
set_real_ip_from <backend ip>;
real_ip_header X-Forwarded-For;
if ($allow = 0) {
return 444;
}
server_name backend.com;
underscores_in_headers on;
include snippets/<ssl-conf>;
include snippets/<ssl-params>;
location /<myApp>/static/ {
root /<path>/<myApp>/static;
}
location /<myApp>/ {
include proxy_params;
proxy_pass_request_headers on;
proxy_pass http://unix:/<path>/<myApp>/<myApp>.sock:/;
}
location = /<myApp> {
include proxy_params;
proxy_pass_request_headers on;
proxy_pass
http://unix:/<path>/<myApp>/<myApp>.sock:/;
}
}
원래는 첫 번째 위치 블록만 포함했지만 upstream.com/myApp
후행 슬래시 없이 요청이 발생하면 nginx는 로 리디렉션되고 backend.com/myApp/
원래 요청의 헤더를 전달하지 않습니다. 리디렉션을 방지하기 위해 두 번째 위치 블록을 추가하면 문제가 해결됩니다.
그러나 이제 (후행 슬래시 없이)와 같은 요청을 받으면 upstream.com/myApp/search
301 리디렉션을 수행합니다 upstream.com/search/
(후행 슬래시를 추가하지만 해당 부분은 사라졌습니다. nginx가 리디렉션을 수행한 후 URL에서 해당 부분을 어떻게 보존할 수 있습니까?) ?
답변1
좀 더 디버깅을 했는데 결국 Nginx 문제가 아니었습니다. 오히려 Django가 nginx 프록시에 의해 필터링된 경로를 리디렉션에 추가하지 않는 문제입니다.
답변2
Nginx 문제가 아니라 Nginx 기능입니다. Nginx가 위치 블록과 일치하는 경로 부분을 제거하지 않도록 하려면 Proxy_pass 지시문에 server:port 또는 소켓 뒤에 지정된 경로가 없어야 합니다.
여기에는 끝에 슬래시만 포함되며, 지정한 모든 경로는 위치 블록의 일치하는 부분을 대체하며, 슬래시만 지정하면 일치하는 부분을 삭제하는 효과가 있습니다.