【问题标题】:nginx reverse proxy in dockerdocker中的nginx反向代理
【发布时间】:2018-10-10 09:13:10
【问题描述】:

我在使用 nginx 时遇到了一个小问题。首先,我只是将 nginx 和 portainer 作为容器运行。 Portainer 在端口 9000 上运行,并且容器位于同一个 docker 网络上,因此这不是可见性问题。 Nginx 暴露了 80 端口并且工作正常。直接访问9000时portainer也是如此。我在本地映射 nginx 卷 /etc/nginx/nginx.conf:ro 和 /usr/share/nginx/html:ro 并且它们对更改做出反应,所以我应该正确连接。在我映射的 nginx.conf(http 部分)中,我有

server {
    location /portainer {
        proxy_pass http://portainer:9000;
     }
}

portainer 被命名的地方,好吧,portainer。我也尝试过使用上游指令+服务器,但也没有用。

当访问 localhost/porttainer 日志时 nginx 显示

2018/04/30 09:21:32 [错误] 7#7: *1 open() "/usr/share/nginx/html/porttainer" 失败(2:没有这样的文件或目录),客户端: 172.18.0.1,服务器:localhost,请求:“GET /portainer HTTP/1.1”,主机:“localhost”

这表明位置指令甚至没有命中(?)。我在各个地方都尝试过,但无济于事。我猜这是我缺少的一些微不足道的东西。

提前致谢, 尼克

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    我必须在两行中添加一个斜杠:

    server {
        location /portainer/ {
            proxy_pass http://portainer:9000/;
         }
    }
    

    【讨论】:

    • 我已经尝试了所有组合,但现在我开始认为存在更通用的设置问题,因为我在带有 NAT 和端口转发以及 docker 容器的 VirtualBox 内运行我的 Ubuntu Ubuntu。我将不得不看看它在另一台主机上的“真正”虚拟机上运行时的反应......
    【解决方案2】:

    试试这个:

    location ~* ^/portainer/(.*)$ {
      proxy_pass http://portainer:9000/$1$is_args$args;
    }
    

    参考:http://nginx.org/en/docs/http/ngx_http_core_module.html

    【讨论】:

    • 不幸的是,它产生了相同的 404。真的很奇怪,我已经看到了几十个例子,在大多数情况下 proxy_pass container_name:portcontainer 与上游服务器容器服务器 container_name:port 似乎工作。 ..
    • 在这种情况下,如果第一个匹配位置在其他地方定义,您可能需要调试。我不确定 Nginx 是否提供了一种直接的发现方法,也许stackoverflow.com/q/12703702 可以提供帮助。
    • 这是一个全新的 nginx 安装,所以如果有另一个匹配的位置,我会感到非常惊讶。尤其是因为它看起来会命中 no 位置(搬运工刚刚被添加到我的静态内容根目录)
    猜你喜欢
    • 2016-07-22
    • 2021-11-13
    • 2020-03-08
    • 2018-05-31
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多