[英]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://nexus:8081/
:-而不是使用http://localhost: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配置。
該文檔介紹了此配置,並指出您需要配置Nexus才能在/nexus
上投放。 這將使您能夠按照以下方式(從docs)配置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;
}
我建議使用該配置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.