【问题标题】:Nginx: 502 Bad Gateway within docker stackNginx:502 泊坞窗堆栈中的错误网关
【发布时间】:2018-05-05 21:17:42
【问题描述】:

我有运行 2 个容器的 docker stack,第一个是 Nginx,第二个是应用程序。

问题是 nginx 显示 Bad Gateway 错误:

这里是 nginx 配置文件:

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

请帮忙。

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    您的上游定义似乎不正确。它正在尝试连接到端口 80 而不是端口 9000。

    试试

    upstream example {
      server mystack_app1:9000;  
      # Also tried with just 'app1'
      # server mystack_app2;
    
      keepalive 32;
    }
    

    顺便说一句,我建议你在 docker-compose 文件中使用 container_name。

    【讨论】:

    • 我之前用port试过,没用,可能是其他配置不正确。现在可以了,谢谢!
    猜你喜欢
    • 2015-10-08
    • 2021-11-05
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    • 2011-05-14
    • 2019-06-05
    • 2015-08-10
    • 2017-10-12
    相关资源
    最近更新 更多