포트 80에서 수신 대기하는 nginx를 실행하는 여러 도커 컨테이너를 실행하려고하지만 컨테이너 포트 80에 다른 호스트 포트가 매핑되어 있습니다.
후행 슬래시가 없어서 nginx가 경로 재 지정을 수행하는 경우를 제외하고 대부분의 경우에 작동합니다.
server {
listen 80;
root /var/www;
index index.html;
location /docs {}
}
위의 nginx 구성과 컨테이너 포트 80에 매핑 된 호스트 포트 8080으로 실행하는 도커 컨테이너를 고려하면 curl ok를 통해 localhost : 8080 / docs /를 얻을 수 있습니다.
> GET /docs/ HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
* Server nginx/1.9.5 is not blacklisted
< Server: nginx/1.9.5
< Date: Sat, 28 Nov 2015 17:27:05 GMT
< Content-Type: text/html
< Content-Length: 6431
< Last-Modified: Sat, 28 Nov 2015 17:17:06 GMT
< Connection: keep-alive
< ETag: "5659e192-191f"
< Accept-Ranges: bytes
<
... html page ...
그러나 localhost : 8080 / docs를 요청하면 localhost / docs /로 리디렉션됩니다.
> GET /docs HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
* Server nginx/1.9.5 is not blacklisted
< Server: nginx/1.9.5
< Date: Sat, 28 Nov 2015 17:29:40 GMT
< Content-Type: text/html
< Content-Length: 184
< Location: http://localhost/docs/
< Connection: keep-alive
<
... html redirect page ...
리디렉션을 수행 할 때 nginx가 원래 포트를 유지하도록하려면 어떻게해야합니까? port_in_redirect 및 server_name_in_redirect를 살펴 보았지만 도움이되지 않았습니다.
편집하다
를 기반으로 https://forum.nginx.org/read.php?2,261216,261216#msg-261216 이 지금 할 수있는 권리를 보이지 않는다.
답변
가장 간단한 해결책은 index
지시문 을 제거하고 명시 적 또는 암시 적 $uri/
리디렉션 에 의존하지 않는 것 입니다. 예를 들면 다음과 같습니다.
server {
listen 80;
root /var/www;
location /docs {
try_files $uri $uri/index.html =404;
}
}
리디렉션을 완전히 피하기 때문에 동일한 동작이 아닙니다. 인덱스 모듈과 같이 슬래시 리디렉션을 원한다면 더 복잡한 솔루션이 필요합니다. 예를 들면 다음과 같습니다.
server {
listen 80;
root /var/www;
location /docs {
try_files $uri @redirect;
}
location @redirect {
if ($uri ~* ^(.+)/$) { rewrite ^ $uri/index.html last; }
if (-d $document_root$uri) { return $scheme://$host:8080$uri/; }
return 404;
}
}
답변
HTTP 클라이언트는 포트를 호스트 헤더에 넣습니다. 리디렉션을 수행 할 때 호스트 헤더의 원래 값을 사용하면 예상대로 작동합니다. 다음 코드를 테스트했으며 요청한대로 정확하게 수행하고 있습니다.
location ~ ^.*[^/]$ {
try_files $uri @rewrite;
}
location @rewrite {
return 302 $scheme://$http_host$uri/;
}
> GET /bla HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 302 Moved Temporarily
< Server: nginx/1.9.7
< Date: Sun, 29 Nov 2015 06:23:35 GMT
< Content-Type: text/html
< Content-Length: 160
< Connection: keep-alive
< Location: http://localhost:8080/bla/
답변
이 간단한 수정을 따르십시오.
location /app {
alias /usr/share/nginx/html/folder;
if (-d $request_filename) {
rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}
}
답변
흥미 롭습니다. 저는이 문제를 정확히 겪었고 Richard Smith의 답변에서 제안한 대로 해결할 수있었습니다 .
root /var/www;
location = /docs {
try_files $uri $uri/ =404;
}
유일한 차이점은 내가 지정하지 않는다는 것입니다 index.html
.
리디렉션 루프를 피하려면 오류 코드를 지정하십시오.
여전히 nginx의 지원으로부터 피드백을 기다리고 있습니다.