[英]Docker Compose and Nginx reverse proxy: I can't access the backend through the proxy
我有一個包含 3 個容器的項目:反向代理容器(jwilder-nginx-proxy 圖像)、fontend 容器(nginx 容器服務於由 Vue js 開發和捆綁的應用程序)和一個后端容器(一個 node6 容器服務於 NodeJs+ExpressJs 應用程序)。 后端和前端都在反向代理之后。 以下是它在我的本地主機中的工作方式:
除了后端容器外,一切似乎都運行良好。 當我嘗試訪問后端時,出現“502 錯誤網關”錯誤。 這是 nginx 記錄的內容:
2017/12/19 06:47:28 [error] 6#6: *3 connect() failed (111: Connection
refused) while connecting to upstream, client: 172.22.0.1, server: ,
request: "GET /favicon.ico HTTP/1.1", upstream:
"http://172.22.0.3:3000/favicon.ico", host: "localhost:3500", referrer:
"http://localhost:3500/"
在我的后端 Dockerfile 中,我使用了這個:
EXPOSE 3000
這是我的 docker-compose.yml 文件:
version: '3'
services:
api:
image: myapp/api
restart: always
networks:
- myapp_network
gui:
image: myapp/gui
restart: always
networks:
- myapp_network
reverse:
image: nginx-reverse
depends_on:
- api
- gui
ports:
- 80:8080
- 3500:3500
restart: always
volumes:
- /var/run/docker.sock:/tmp/docker.sock
networks:
- myapp_network
networks:
myapp_network:
driver: bridge
gui 也是如此,它是一個 nginx 服務器,它偵聽容器內部的 8080 並映射到容器外部的端口 80,我在 Dockerfile 中使用了它:
EXPOSE 8080
我相信我的 nginx.conf 文件有問題(我用來配置反向代理的文件不是 GUI 應用程序):
nginx.conf(反向代理配置):
http
{
sendfile on;
upstream myapp-api
{
server api:3000;
}
server
{
listen 3500;
add_header Access-Control-Allow-Origin * always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
gzip_types text/plain text/css application/json application/x-javascript
text/xml application/xml application/xml+rss text/javascript;
location /
{
proxy_pass http://myapp-api/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
upstream myapp-gui
{
server gui:8080;
}
server
{
listen 8080;
add_header Access-Control-Allow-Origin * always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
gzip_types text/plain text/css application/json application/x-javascript
text/xml application/xml application/xml+rss text/javascript;
location /
{
proxy_pass http://myapp-gui;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
我的 docker-compose 文件有問題嗎? 還是 nginx 配置? 我對前端和后端使用了相同的邏輯。 只有后端不工作。 前端正在工作。
我沒有在 nginx-reverse-proxy 的 Dockerfile 中使用暴露指令,因為我正在映射撰寫文件中的端口。
希望有人可以幫助我。
謝謝
編輯:似乎一切都配置得很好。 問題是節點js應用程序。 似乎 Nginx 無法處理對 nodejs 應用程序的請求......有人可以提供幫助嗎?
對於遇到相同問題的每個人,這里是解決方案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.