【问题标题】:Nginx proxy_pass to docker container doesn't workNginx proxy_pass 到 docker 容器不起作用
【发布时间】:2019-11-08 14:33:24
【问题描述】:

我有两个相同的 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

【问题讨论】:

  • 既然0.0.0.0 表示它自己,那么这个容器应该如何与另一个容器通信?您应该将流量传递给另一个容器(使用其 ip / 容器名称)。
  • @emix 容器不相互交互,它们只是在不同的端口上运行
  • Nginx 不在容器中。两个容器应该是服务的不同版本,但出于测试原因,我启动了第二个相同的容器,而 nginx 无法在不同的端口上代理新的容器
  • 我明白了,这些容器是否在同一个网络中运行? setsebool -P httpd_can_network_connect true 怎么样?它很可能与 selinux 相关。你检查过日志吗?
  • @emix 错误是 [crit] 9478#0: *5 connect() to 0.0.0.0:8080 failed (13: Permission denied) while connecting to upstreamsetsebool -P httpd_can_network_connect true 帮助了我!十分感谢!现在我很好奇为什么第一个容器很好。您能否将其添加为答案,以便我接受?

标签: python-3.x docker nginx centos fastapi


【解决方案1】:

这是 SElinux 相关的:

setsebool -P httpd_can_network_connect true

根据this thread

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

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 2022-01-22
    • 2015-03-17
    • 2023-02-26
    • 2019-05-03
    • 2017-01-22
    • 2015-05-03
    • 2020-02-22
    • 2017-10-29
    相关资源
    最近更新 更多