Docker&x2B;Nginx:让代理_通行证发挥作用
我在试图让Nginx代理到另一台也在Docker中运行的服务器的路径时遇到问题 为了说明这一点,我以Nexus服务器为例 这是我第一次尝试Docker&x2B;Nginx:让代理_通行证发挥作用,nginx,docker,docker-compose,nginx-location,Nginx,Docker,Docker Compose,Nginx Location,我在试图让Nginx代理到另一台也在Docker中运行的服务器的路径时遇到问题 为了说明这一点,我以Nexus服务器为例 这是我第一次尝试 docker compose.yml:- version: '2' services: nexus: image: "sonatype/nexus3" ports: - "8081:8081" volumes: - ./nexus:/nexus-data nginx: image: "nginx"
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坏网关:-
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配置中添加了链接
:-
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/[路径]
。。。因此,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应用程序将向“/”请求无法使用的it资源
但是,这并不理想,并且不适用于为多个应用提供服务的Nginx配置
介绍此配置,并指出您需要配置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;
}
我建议使用该配置。以下附加选项是我所使用的
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应用程序将向“/”请求无法使用的it资源
但是,这并不理想,并且不适用于为多个应用提供服务的Nginx配置
介绍此配置,并指出您需要配置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;
}
我建议使用该配置。它不适合我<代码>http://localhost/nexus获取重定向到
http://nexus
,我在该页上看到404。这将发生,因为您没有位置指令匹配所需的尾随“/”。这本身并不能解决问题。我正在用解决方案更新答案。将Nexus配置为具有自定义上下文路径/Nexus
对我来说很有用。然后,我将proxy_pass配置为指向http://nexus:8081/nexus/
。非常感谢你的帮助。这对我不起作用<代码>http://localhost/nexus获取重定向到http://nexus
,我在该页上看到404。这将发生,因为您没有位置指令匹配所需的尾随“/”。这本身并不能解决问题。我正在用解决方案更新答案。将Nexus配置为具有自定义上下文路径/Nexus
对我来说很有用。然后,我将proxy_pass配置为指向http://nexus:8081/nexus/
。非常感谢你的帮助。