【问题标题】:NGINX reverse proxy not forwarding the request hostname to docker containerNGINX 反向代理未将请求主机名转发到 docker 容器
【发布时间】:2020-05-24 11:24:18
【问题描述】:

问题:

我们已经设置了一个在端口 3002 上运行的 docker 容器,然后在我的域 www.example.com 上将端口 3002 配置为 /path/。在3002 端口容器上运行一个快速休息api,它输出req.hostname,当我从www.abc.com 发出请求时,req.hostname 的控制台值显示为www.example.com 而不是@987654330 @。

Nginx 配置

server {
    listen 443 ssl;
    ssl_certificate /etc/ssl/__abc.crt;
    ssl_certificate_key /etc/ssl/abc.key;

    listen 80 default_server;
    listen [::]:80 default_server;
    location  / {
        proxy_pass http://localhost:3001/;
        proxy_set_header Host $host;
    }
    location /path/ {
        proxy_pass http://localhost:3002/;
        proxy_set_header Host $http_host;
    }
}

我必须进行哪些更改才能获得 www.abc.com 的控制台值?

【问题讨论】:

    标签: docker nginx reverse-proxy hostname


    【解决方案1】:

    Nginx 的location 块应该按照更具体的表达式排在第一位的顺序排列。

    在您的示例中,您应该:

        location /path/ {
            proxy_pass http://localhost:3002/;
            proxy_set_header Host $http_host;
        }
    
        location  / {
            proxy_pass http://localhost:3001/;
            proxy_set_header Host $host;
        }
    
    

    通过运行nginx -s reload 或重新启动容器来确保您的更改生效

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多