【问题标题】:Nginx container 502 Bad GatewayNginx 容器 502 错误网关
【发布时间】:2020-06-29 07:26:11
【问题描述】:

我知道以前有人问过这个问题,而且我可能已经阅读了大部分这些帖子,但我无法让组合发挥作用。

我正在尝试使用 Nginx 作为 Sonatype Nexus 3 前端的 HTTPS 反向代理。Nexus 和 Nginx 都是在 Linux(Fedora 服务器)docker 主机(192.168.60.204 / svr1.domain.com)上运行的容器。

但是,当我启用proxy_pass 时,我得到502 Bad Gateway 从 docker 主机运行docker logs --tail 50 --follow --timestamps nginx-nexus,我得到:

[error] 6#6: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: 192.168.60.1, server: nexus.domain.com, request: "GET / HTTP/1.1", upstream: "http://192.168.60.204:8081/", host: "nexus.domain.com"

192.168.60.1 是 Docker 主机所在网络的默认网关,所以我不知道为什么它似乎试图连接到这个。

nexus.domain.com 是指向 Docker 主机的 CNAME。

我可以使用docker exec -it nginx-nexus sh(感谢@arik)连接到 Nginx 容器并成功 ping nexus.domain.com

我已经尝试了nginx.conf 的许多排列,正如您从注释掉的代码中看到的那样:

client_max_body_size      4G;

server {
  listen *:80;
  location /  {
    return 301 https://$host$request_uri;
  }
}

upstream foo{
  #insert your hosts ip here
  server nexus.domain.com:8081;
}

server {
  listen                  443 ssl;
  server_name             nexus.domain.com;

  ssl_certificate         /etc/nginx/certs/nexus.crt.pem;
  ssl_certificate_key     /etc/nginx/certs/nexus.key.pem;
  ssl_protocols           TLSv1.2;
  ssl_ciphers             HIGH:!aNULL:!MD5;

  location / {
    #resolver              127.0.0.11 valid=5s;
    proxy_pass            http://nexus.domain.com:8081/;
    #proxy_redirect        off;
    #proxy_set_header      Host $http_host;
    #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_set_header      X-Forwarded-Host $server_name;
    proxy_set_header      X-Forwarded-Proto $scheme;
  }
}

我认为 Docker 网络是正确的,因为我有另一个使用 nginx 的工作容器是类似的方式。

如果有人能阐明我的错误之处,我将不胜感激。 T.I.A

更新 1

根据 cmets 中 @Arix 的建议,我在 location / { 下添加了:

resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;

出现以下错误:

$ docker logs --tail 50 --follow --timestamps nginx-nexus
2020-06-26T13:14:52.105017039Z 2020/06/26 13:14:52 [error] 6#6: *1 connect() failed (113: Host is unreachable) while connecting to upstream, client: 192.168.60.1, server: nexus.domain.com, request: "GET / HTTP/1.1", upstream: "http://192.168.60.204:8081/", host: "nexus.domain.com"
2020-06-26T13:14:52.105371984Z 192.168.60.1 - - [26/Jun/2020:13:14:52 +0000] "GET / HTTP/1.1" 502 560 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36" "-"

【问题讨论】:

  • 在你的 nginx 容器中打开一个 shell 并 ping nexus.domain.com - 结果是什么?
  • 嗨@Arik,用docker exec -t -i mycontainer /bin/bash 尝试过。 nginx:alpine图片好像没有壳:OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
  • 试试docker exec -it mycontainer sh
  • @Arik。那行得通。我可以从 nginx 容器 ping nexus.domain.com。问题已更新。谢谢。
  • 尝试将以下内容添加到server 块中:resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;

标签: docker ssl nginx nexus


【解决方案1】:

这似乎是一个 DNS 问题,我不完全理解,但很想知道原因是什么。

另外,我在刷新按钮上太快了。 Nginx 和 Nexus 完全启动并运行大约需要 15 - 30 秒。

TL;DR:

删除域,将:proxy_pass http://nexus.domain.com:8081 更改为 proxy_pass http://nexus:8081

全文:

在 Nginx 容器中打开一个 shell 并 ping nexus.domain.com 解析 Docker 主机的 IP - 这是我想要的。

ping nexus 解析到此容器的 Docker 内部 IP 地址。

它似乎在没有resolver 部分的情况下工作。为了完整起见,这是我的 nginx.conf:

client_max_body_size      4G;

server {
  listen                  80;
  server_name             nexus.domain.com;
  location /  {
    return 301 https://$host$request_uri;
  }
}

#resolver              1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;

server {
  listen                  443 ssl;
  server_name             nexus.domain.com;

  ssl_certificate         /etc/nginx/certs/nexus.crt.pem;
  ssl_certificate_key     /etc/nginx/certs/nexus.key.pem;
  ssl_protocols           TLSv1.2;
  ssl_ciphers             HIGH:!aNULL:!MD5;

  location / {
    proxy_pass            http://nexus:8081/;
    proxy_redirect        off;
    proxy_set_header      Host $http_host;
    #proxy_set_header      X-Real-IP $remote_addr;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for; #Gets CSS working
    #proxy_set_header      X-Forwarded-Host $server_name;
    proxy_set_header      X-Forwarded-Proto $scheme;
  }
}

如果有人有时间解释一下,我很想知道 DNS 发生了什么...

HTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-14
    • 2019-06-05
    • 2015-08-10
    • 2021-11-19
    • 2012-09-25
    • 2014-12-07
    • 2020-09-29
    • 2012-07-16
    相关资源
    最近更新 更多