Docker + Nginx:让 proxy_pass 开始工作
Docker + Nginx: Getting proxy_pass to work
我在尝试让 Nginx 代理到另一个服务器的路径时遇到问题,该服务器也是 Docker 中的 运行。
为了说明,我以 Nexus 服务器为例。
这是我的第一次尝试...
docker-compose.yml
:-
version: '2'
services:
nexus:
image: "sonatype/nexus3"
ports:
- "8081:8081"
volumes:
- ./nexus:/nexus-data
nginx:
image: "nginx"
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
nginx.conf
:-
worker_processes 4;
events { worker_connections 1024; }
http {
server {
listen 80;
location /nexus/ {
proxy_pass http://localhost:8081/;
}
}
}
当我点击 http://localhost/nexus/
时,我得到 502 Bad Gateway 和以下日志:-
nginx_1 | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://[::1]:8081/", host: "localhost"
nginx_1 | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "localhost"
nginx_1 | 172.18.0.1 - - [29/May/2017:02:20:50 +0000] "GET /nexus/ HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
第二次尝试...,
docker-compose.yml
- 我在 Nginx 配置中添加了 links
:-
version: '2'
services:
nexus:
image: "sonatype/nexus3"
ports:
- "8081:8081"
volumes:
- ./nexus:/nexus-data
nginx:
image: "nginx"
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
links:
- nexus:nexus
nginx.conf
... 而不是使用 http://localhost:8081/
,我使用 http://nexus:8081/
:-
worker_processes 4;
events { worker_connections 1024; }
http {
server {
listen 80;
location /nexus/ {
proxy_pass http://nexus:8081/;
}
}
}
现在,当我点击 http://localhost/nexus/
时,它得到了正确的代理,但 Web 内容被部分呈现。检查该页面的 HTML 源代码时,javascript、样式表和图像链接指向 http://nexus:8081/[path]
... 因此,404.
我应该更改什么才能使其正常工作?
非常感谢。
以下附加选项是我用过的
http {
server {
listen 80;
location /{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
server_name_in_redirect on;
proxy_pass http://nexus:8081;
}
location /nexus/ {
proxy_pass http://nexus:8081/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
server_name_in_redirect on;
}
}
}
我的解决方案是在 nginx 配置中包含“/”路径的重定向。 Nexus 应用程序将向“/”请求它无法工作的资源。
但是,这并不理想,不适用于为多个应用程序提供服务的 Nginx 配置。
docs
覆盖此配置并指示您需要配置 Nexus 以在 /nexus
上提供服务。这将使您能够按以下方式配置 Nginx(来自文档)减去上面的 hack。
location /nexus {
proxy_pass http://localhost:8081/nexus;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
我建议使用该配置。
我在尝试让 Nginx 代理到另一个服务器的路径时遇到问题,该服务器也是 Docker 中的 运行。
为了说明,我以 Nexus 服务器为例。
这是我的第一次尝试...
docker-compose.yml
:-
version: '2'
services:
nexus:
image: "sonatype/nexus3"
ports:
- "8081:8081"
volumes:
- ./nexus:/nexus-data
nginx:
image: "nginx"
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
nginx.conf
:-
worker_processes 4;
events { worker_connections 1024; }
http {
server {
listen 80;
location /nexus/ {
proxy_pass http://localhost:8081/;
}
}
}
当我点击 http://localhost/nexus/
时,我得到 502 Bad Gateway 和以下日志:-
nginx_1 | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://[::1]:8081/", host: "localhost"
nginx_1 | 2017/05/29 02:20:50 [error] 7#7: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /nexus/ HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "localhost"
nginx_1 | 172.18.0.1 - - [29/May/2017:02:20:50 +0000] "GET /nexus/ HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
第二次尝试...,
docker-compose.yml
- 我在 Nginx 配置中添加了 links
:-
version: '2'
services:
nexus:
image: "sonatype/nexus3"
ports:
- "8081:8081"
volumes:
- ./nexus:/nexus-data
nginx:
image: "nginx"
ports:
- "80:80"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
links:
- nexus:nexus
nginx.conf
... 而不是使用 http://localhost:8081/
,我使用 http://nexus:8081/
:-
worker_processes 4;
events { worker_connections 1024; }
http {
server {
listen 80;
location /nexus/ {
proxy_pass http://nexus:8081/;
}
}
}
现在,当我点击 http://localhost/nexus/
时,它得到了正确的代理,但 Web 内容被部分呈现。检查该页面的 HTML 源代码时,javascript、样式表和图像链接指向 http://nexus:8081/[path]
... 因此,404.
我应该更改什么才能使其正常工作?
非常感谢。
以下附加选项是我用过的
http {
server {
listen 80;
location /{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
server_name_in_redirect on;
proxy_pass http://nexus:8081;
}
location /nexus/ {
proxy_pass http://nexus:8081/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
server_name_in_redirect on;
}
}
}
我的解决方案是在 nginx 配置中包含“/”路径的重定向。 Nexus 应用程序将向“/”请求它无法工作的资源。
但是,这并不理想,不适用于为多个应用程序提供服务的 Nginx 配置。
docs
覆盖此配置并指示您需要配置 Nexus 以在 /nexus
上提供服务。这将使您能够按以下方式配置 Nginx(来自文档)减去上面的 hack。
location /nexus {
proxy_pass http://localhost:8081/nexus;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
我建议使用该配置。