Docker nginx 反向代理给出“502 Bad Gateway”

新手上路,请多包涵

我正在尝试使用带有 nginx 的 docker 容器作为其他 docker 容器的反向代理,并且在除基本位置“/”以外的位置上不断收到“错误网关”。

我有以下服务器块:

 server {

  listen 80;

  location / {
    proxy_pass "http://game2048:8080";
  }

  location /game {
    proxy_pass "http://game:9999";
  }

}

它适用于 http://localhost 但不适用于 http://localhost/game 它在浏览器和 nginx 容器中给出“坏网关”:

 [error] 7#7: *6 connect() failed (111: Connection refused)
while connecting to upstream, client: 172.17.0.1, server: ,
request: "GET /game HTTP/1.1", upstream: "http://172.17.0.4:9999/game",
host: "localhost"

我使用官方的 nginx docker 镜像并在上面放上我自己的配置。您可以在此处对其进行测试并查看所有详细信息: https ://github.com/jollege/ngprox1

任何想法出了什么问题?

注意:我在 docker 主机上设置了本地主机名条目以匹配这些名称:

 127.0.1.1       game2048
127.0.1.1       game

原文由 jollege 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
1 个回答

我修好了它!我在 nginx 配置中的不同服务器块中设置了服务器名称。请记住使用 docker 端口,而不是主机端口。

 server {

  listen 80;
  server_name game2048;

  location / {
    proxy_pass "http://game2048:8080";
  }

}

server {

  listen 80;
  server_name game;

  location / {
    # Remember to refer to docker port, not host port
    # which is 9999 in this case:
    proxy_pass "http://game:8080";
  }

}

github repo 已更新以反映修复,旧的自述文件位于 ./README.old01.md 下。

当我仔细地向别人提出问题时,我找到了答案。你知道那种感觉吗?

原文由 jollege 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题