Nginx: Override host header when using fastcgi_pass

Joe J picture Joe J · Mar 6, 2015 · Viewed 8.1k times · Source

I'm trying to override the http host header that is passed to my php-based application (specifically Phabricator) when using fastcgi_pass.
I've found a lot of examples for doing this when using proxy_pass, but I can't seem to find an example of how to do this with fastcgi_pass. Specifically, I'd like the proxied php application to see the host header as "phabricator.localhost".

(The reason for this is that I want to associate several different domains with the Phabricator webapp, but it only allows one domain to be associated and it rejects any requests not made that that one domain.)

I'm pretty new to configuring Nginx with FastCGI, so I'm not sure how fastcgi works. Any help is appreciated.

Here is my Nginx server configuration:

  server {
    server_name phabricator.localhost  www.example.com example.com;
    root /opt/phabricator/phabricator/webroot;

    location / {
      index index.php;
      rewrite ^/(.*)$ /index.php?__path__=/$1 last;
    }

    location = /favicon.ico {
      try_files $uri =204;
    }

    location /index.php {
    fastcgi_pass   localhost:9000;
    fastcgi_index   index.php;

    #### HERE ARE MY ATTEMPTS #####
    #proxy_set_header HOST phabricator.localhost;
    #fastcgi_param SERVER_NAME phabricator.localhost;
    #fastcgi_pass_header 'Host: phabricator.localhost';
    #fastcgi_pass_header 'Host: phabricator.localhost';
    #add_header Host phabricator.localhost;
    #proxy_set_header Host phabricator.localhost;
    #### END ATTEMPTS ####

    fastcgi_param  REDIRECT_STATUS    200;
    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;
    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
    fastcgi_param  REMOTE_ADDR        $remote_addr;
  }
}

Answer

Cole Tierney picture Cole Tierney · Mar 6, 2015

Have you tried HTTP_HOST? The following works for me:

fastcgi_param HTTP_HOST phabricator.localhost;