我有运行2个容器的docker stack,第一个是Nginx,第二个是应用程序.
这是Nginx conf:
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 stack:
docker stack deploy -c docker-compose.yml mystack
因此,我可以通过localhost:9000从主机的浏览器访问应用程序-正常.
另外,从Nginx容器中,我可以ping mystack_app1.
但是当访问localhost:81时,Nginx显示502 Bad Gateway
请帮忙.
最佳答案
看起来您的上游定义不正确.它正在尝试连接到端口80而不是端口9000.
尝试
upstream example {
server mystack_app1:9000;
# Also tried with just 'app1'
# server mystack_app2;
keepalive 32;
}
顺便说一句,我建议您在docker-compose文件中使用container_name.