Nginx가 리디렉션 시 Docker 매핑 포트 대신 브라우저 포트를 사용하도록 강제

Nginx가 리디렉션 시 Docker 매핑 포트 대신 브라우저 포트를 사용하도록 강제

호스트와 게스트의 다른 포트로 Nginx 서버를 매핑하면 위치 리디렉션이 요청된 URI의 포트 대신 게스트 포트를 따릅니다.

재현 단계:

$ cat /tmp/default.conf
server {
    listen       8080;
    server_name  localhost;
    location ~ ^/loveslash$ {
        return  301 $request_uri/;
    }
    location ~ ^/loveslash/$ {
        add_header Content-Type text/plain;
        return 200 'yayy!';
    }
    location / {
        add_header Content-Type text/plain;
        return 200 '¯\_(ツ)_/¯';
    }
}

$ docker run --name some-nginx -v /tmp/default.conf:/etc/nginx/conf.d/default.conf:ro -p 80:8080 --rm nginx


$ curl -i http://localhost/loveslash | grep Location
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   169  100   169    0     0   165k      0 --:--:-- --:--:-- --:--:--  165k
Location: http://localhost:8080/loveslash/
  • 내가 얻는 것:http://localhost/loveslash -> http://localhost:8080/loveslash/
  • 내가 원하는 것:http://localhost/loveslash -> http://localhost/loveslash/

답변1

기본 포트를 사용하고 있기 때문에 Nginx가 외부 리디렉션을 생성할 때 URL에서 포트 번호를 제거하도록 지시할 수 있습니다.

예를 들어:

server {
    listen 8080;
    port_in_redirect off;
    ...
}

보다이 문서자세한 내용은.

관련 정보