【发布时间】:2017-10-29 06:13:19
【问题描述】:
我在尝试让 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/ 时,它会被正确代理,但网页内容会部分呈现。在检查该页面的 HTML 源代码时,javascript、样式表和图像链接指向 http://nexus:8081/[path]... 因此是 404。
我应该改变什么才能让它正常工作?
非常感谢。
【问题讨论】:
标签: nginx docker docker-compose nginx-location