【发布时间】:2022-03-30 20:04:09
【问题描述】:
我在 React 中有一个使用 Nginx 作为服务器的多容器 docker 应用程序。当我尝试在浏览器中打开它时,我收到 502 Bad Gateway 错误消息:
2021/12/30 11:58:44 [error] 31#31: *31 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.23.0.2:3000/favicon.ico", host: "localhost:3050", referrer: "http://localhost:3050/"
这里是 nxing 配置文件:
upstream client {
server client:3000;
}
upstream api {
server api:5000;
}
server {
listen 80;
location / {
proxy_pass http://client;
}
location /sockjs-node {
proxy_pass http://client;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}
这是用于 Nginx 的 docker-compose 的片段:
nginx:
depends_on:
- api
- client
restart: always
build:
dockerfile: Dockerfile.dev
context: ./nginx
ports:
- '3050:80'
我该如何解决这个问题?
【问题讨论】:
-
该错误的意思是:Nginx 尝试连接到
client:3000(将其解析为 Docker 内部 IP 地址)并失败。client容器中正在运行什么?ports:没关系;client容器是否在运行 HTTP 服务器,正在监听0.0.0.0:3000? (您在问题中显示的内容似乎按预期工作。) -
在客户端的 Dockerfile 中添加 EXPOSE 3000 有效,我可以访问该页面
-
当我尝试发布值时,我得到了这个:
2021/12/30 15:48:01 [error] 30#30: *20 connect() failed (113: No route to host) while connecting to upstream, client: 172.18.0.1, server: , request: "POST /api/values HTTP/1.1", upstream: "http://172.18.0.3:5000/values", host: "localhost:3050", referrer: "http://localhost:3050/"它似乎试图去 '/values' 而不是 '/api/values' -
我猜应该是因为 nginx 配置,但是为什么会这样呢?
-
没关系,现在可以使用了