Nginx:502 docker堆栈中的坏网关
我让docker堆栈运行2个容器,第一个是Nginx,第二个是应用程序 问题是nginx显示坏网关错误: 这里是nginx conf:Nginx:502 docker堆栈中的坏网关,docker,nginx,Docker,Nginx,我让docker堆栈运行2个容器,第一个是Nginx,第二个是应用程序 问题是nginx显示坏网关错误: 这里是nginx conf: upstream example { server mystack_app1; # Also tried with just 'app1' # server mystack_app2; keepalive 32; } server { listen 80; server_name example; locatio
upstream example {
server mystack_app1;
# Also tried with just 'app1'
# server mystack_app2;
keepalive 32;
}
server {
listen 80;
server_name example;
location / {
proxy_pass http://example;
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_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
}
}
这是docker-compose.yml
version: "3"
services:
app1:
image: my-app:latest
ports:
- "9000:9000"
networks:
- webnet
web:
image: my-web:latest
ports:
- "81:80"
networks:
- webnet
deploy:
restart_policy:
condition: on-failure
networks:
webnet:
我使用以下命令部署docker堆栈:
docker stack deploy -c docker-compose.yml mystack
所以我可以通过localhost:9000从主机的浏览器访问应用程序-它工作正常
另外,从nginx容器中,我可以ping mystack_app1
但是当访问localhost:81时,nginx显示502坏网关
请帮助。看起来您的上游定义不正确。它正在尝试连接到端口80而不是端口9000 试一试
顺便说一句,我建议您在docker compose文件中使用容器名称。我以前尝试过使用port,但不起作用,但可能是因为其他配置不正确。现在可以了,谢谢!
upstream example {
server mystack_app1:9000;
# Also tried with just 'app1'
# server mystack_app2;
keepalive 32;
}