How to make Nginx forward the original host name

Ibu asked:

I have an nginx server acting as a load balancer for my apache servers. I am using subdomains and my code relies on the HTTP_HOST value to perform the correct task.

When i make a request to say: http://get.example.com once it is forwarded the HTTP_HOST on apache becomes example.com.

My question is how do I make it stay the same as the original request?

Here is my nginx config:

upstream example.com {
  server 192.168.2.1:8909 weight=2;
  server 192.168.2.2:8909 weight=1;
  server 192.168.2.3:8909 weight=1;
}

server {
  listen 80;
  location / {
    proxy_pass  http://example.com;
  }
}

I tried adding multiple proxy_pass, one for each subdomain and it doesn’t seem to work.

My answer:


You need to pass along the HTTP Host header. Add this to the relevant location:

proxy_set_header Host $host;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.