prvnbist
prvnbist

Reputation: 451

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. 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

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?

Upvotes: 1

Views: 974

Answers (1)

Elgarni
Elgarni

Reputation: 256

Nginx's location blocks should be ordered such that more specific expressions come first.

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

Upvotes: 2

Related Questions