Enzo
Enzo

Reputation: 141

Nginx Host header for upstream

I am using nginx and I have problem setting a reverse proxy.

My nginx.conf is default (didn't make any change to it) and my site-available config is:

upstream backend_hosts {
    server server1.example.com
    server server2.example.com
}

server {
    listen 80;

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

and it's not working, it doesn't pass the host header. When i do something like this, it works:

...

proxy_set_header Host server1.exampple.com;

...

I would like to do something like this:

proxy_set_header Host $current_upstream_server_name;

Upvotes: 8

Views: 10497

Answers (1)

Dan Gardner
Dan Gardner

Reputation: 1279

We've also bee struggling with this problem, and whilst not a complete answer I believe this might help future users landing on this page.

It is possible to write the IP address of the selected upstream server by access the upstream variable $upstream_http_name.

http {
    upstream backend {
        server server1.example.com;
        server server2.example.com;
    }
    
    server {
        location / {
            proxy_set_header Host $upstream_http_name;
            proxy_pass http://backend;
        }
    }
}

If, as in our application, receiving the IP acceptable then the above configuration will pass that information into the Host header of the proxied request.

A full list of the variable provided by the upstream module can be found here

Upvotes: 2

Related Questions