【发布时间】:2020-06-29 07:26:11
【问题描述】:
我知道以前有人问过这个问题,而且我可能已经阅读了大部分这些帖子,但我无法让组合发挥作用。
我正在尝试使用 Nginx 作为 Sonatype Nexus 3 前端的 HTTPS 反向代理。Nexus 和 Nginx 都是在 Linux(Fedora 服务器)docker 主机(192.168.60.204 / svr1.domain.com)上运行的容器。
- 我可以从另一台 PC 导航到 Nginx (https://svr1.domain.com) (192.168.40.10)
- 我可以从另一台 PC 导航到 Nexus (http://svr1.domain.com:8081) (192.168.40.10)
- 我可以从另一个导航到 Nexus (http://nexus.domain.com:8081) 电脑 (192.168.40.10)
但是,当我启用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;