简体   繁体   English

NGINX 反向代理未将请求主机名转发到 docker 容器

[英]NGINX reverse proxy not forwarding the request hostname to docker container

Problem:问题:

We've setup a docker container running on port 3002 and then configured port 3002 to /path/ on my domain www.example.com .我们已经设置了一个在端口3002上运行的 docker 容器,然后将端口3002配置到我的域www.example.com上的/path/ There's an express rest api is running on 3002 port container which outputs the req.hostname and when I make a request from let's say www.abc.com , the consoled value of req.hostname is shown to be www.example.com instead of www.abc.com . There's an express rest api is running on 3002 port container which outputs the req.hostname and when I make a request from let's say www.abc.com , the consoled value of req.hostname is shown to be www.example.com instead of www.abc.com

Nginx Conf 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;
    }
}

What changes do I have to make so I can get the www.abc.com in consoled value?我必须进行哪些更改才能获得控制台值的www.abc.com

Nginx's location blocks should be ordered such that more specific expressions come first . Nginx 的location块应该被排序,以便更具体的表达式排在第一位

In your example, you should have:在您的示例中,您应该具有:

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

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

Make sure your changes take effect by either running nginx -s reload or restarting the container通过运行nginx -s reload或重新启动容器来确保您的更改生效

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

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