Ok here is the senario:

  1. I have a website built on a server with nginx as the main web server
  2. I use apache as a reverse proxy

The problem:

I tried to use the $_SERVER[‘HTTP_HOST’] command (this gives you the hostname of the url the user requested). For example if the user requested http://subdomain.domain.net/ $_SERVER[‘HTTP_HOST’] will give you subdomain.domain.net when used In apache alone.

But in our case, this will return something like: 127.0.0.1:8165. This is not right of course. This happens because, this is actually what the user(in that case nginx) requests from apache.

My config:

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location ~ \.php$ {
                proxy_pass   http://127.0.0.1:81;
       }

In short: Pass all php requests to Apache. Actually what $_SERVER[‘HTTP_HOST’] returns is this you write there after proxy_pass, without http://, here 127.0.0.1.

One way to fix that:

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location ~ \.php$ {
                proxy_pass   http://yourdomain.com:81;
       }

But this isn’t very nice.

The right way to fix this is, add that line: proxy_set_header Host $http_host;

This will pass the right argument to the host requested and you can use $_SERVER[‘HTTP_HOST’] like you always do.

Thanks for visiting the blog.