Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a situation with a number of websites sharing a single IP address. I have nginx accepting requests and passing them on to Apache, which actually serves the sites. I know that Apache isn't really needed here, but it's set up this way for historical reasons and I'd rather not change it if I don't have to.

The way things are set up, nginx accepts a request for and passes it on to Apache like so:

server {
    listen       80;

    access_log  /var/log/nginx/example.log;
    error_log  /var/log/nginx/example.log;

    location / {
        proxy_read_timeout 120;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_pass http://localhost:8100;

In httpd.conf, we have

<VirtualHost localhost:8100>
    Options Indexes
    DocumentRoot /export/sites/example/live

Everything has worked fine up to now, but I've added a PHP script (not my own) to the site and it is not able to get the correct hostname. Either $_SERVER["HTTP_HOST"] and/or $_SERVER['SERVER_NAME'] are returning localhost:8100 instead of

Is it possible to set this up so that PHP will get the right hostname?

share|improve this question
Something like proxy_set_header Host $http_host; should do it. – Mark Apr 14 '11 at 17:42
up vote 6 down vote accepted

By default, it sends the host spec from the proxy_pass line. You can override this by throwing this config in there, forcing the Host: header to contain the same as sent by the client:

proxy_set_header Host $host;
share|improve this answer
This worked - thanks!!! – Janine Ohmer Apr 14 '11 at 20:28

A workaround would be to change nginx thus:

   location / {
        proxy_read_timeout 120;
        proxy_set_header  X-Real-IP  $remote_addr;

Then modify /etc/hosts so and both point to (internally). But Shane's answer is better, of course.

share|improve this answer
Why make it complicated when the problem can be solved with Shane's one liner. Keep it simple. – Sameer Apr 15 '11 at 18:10
Of course, but I posted this anyway because this approach works for any proxy and Janine hadn't tested the other answer yet. – Eduardo Ivanec Apr 15 '11 at 18:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.