繁体   English   中英

Nginx proxy_pass 到 docker 容器不起作用

[英]Nginx proxy_pass to docker container doesn't work

我有两个相同的 docker 容器在 CentOS7 服务器的不同端口上运行。 旧版本在 81 端口上运行,新版本在 8080 端口上运行(也检查了 82,83)。

当我尝试代理第二个容器并将端口从 81 更改为 8080 时,我收到 nginx 错误消息(HTTP/1.1 502 Bad Gateway)。

Nginx 不在容器中。 我只是将它安装在服务器上。

这是我的 proxy_pass 设置:

location / {
         proxy_pass http://0.0.0.0:8080/;
        }

还有一些附加信息:

nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果我尝试通过它们的端口直接访问容器,一切正常。

curl  0.0.0.0:81
{"msg":"Phone Masks service"}
curl  0.0.0.0:8080
{"msg":"Phone Masks service"}

nginx 版本:nginx/1.16.1

Docker 版本 19.03.4,构建 9013bf583a

完整的服务器配置非常标准,除了 proxy_pass 设置我没有更改任何内容

server {
       listen       80 default_server;
       listen       [::]:80 default_server;
       server_name  _;
       root         /usr/share/nginx/html;

       # Load configuration files for the default server block.
       include /etc/nginx/default.d/*.conf;

       location / {
        proxy_pass http://0.0.0.0:8080/;
       }

       error_page 404 /404.html;
           location = /40x.html {
       }

       error_page 500 502 503 504 /50x.html;
           location = /50x.html {
       }
   }

我用来启动容器的命令:

sudo docker run --rm -it -p 8080:8080 -e PORT="8080" api
sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                            NAMES
47ef127e3e49        api                 "/start.sh"         26 minutes ago      Up 26 minutes       80/tcp, 0.0.0.0:8080->8080/tcp   infallible_borg
5d5fe891ba30        api                 "/start.sh"         7 hours ago         Up 7 hours          80/tcp, 0.0.0.0:81->81/tcp       hopeful_cerf

这是与 SElinux 相关的:

setsebool -P httpd_can_network_connect true

根据这个线程

第二个 [httpd_can_network_connect] 允许 httpd 模块和脚本与与 httpd 服务关联的端口建立传出连接。 要查看这些端口的列表,请运行 semanage port -l | grep -w http_port_t

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM