Nginx proxy_pass到docker容器不起作用

我在CentOS7服务器的不同端口上运行了两个相同的Docker容器。较旧的版本在端口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
songxueli405 回答:Nginx proxy_pass到docker容器不起作用

这与SElinux相关:

setsebool -P httpd_can_network_connect true

根据this thread

  

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

本文链接:https://www.f2er.com/3137200.html

大家都在问