Conditionals in fastcgi config

Maxim Dounin mdounin at
Tue Oct 30 19:50:43 MSK 2007


On Tue, 30 Oct 2007, Igor Clark wrote:

> Hi Igor and nginx people,
> We use nginx as a front end on various development machines in our studio to 
> route to installations of Apache 1, Apache 2, PHP/FCGI, and Ruby/Mongrel on 
> each machine, as appropriate depending on SERVER_NAME conventions, using our 
> private DNS domain.
> We have some circumstances under which we need to grant external access to 
> machines with this setup, and we do this using Apache reverse proxy on the 
> firewall. (Eventually this will be replaced by nginx, but it's got a lot of 
> legacy stuff which will need some time set aside for converting.) Hence 
> is proxied through to 
> This means that the SERVER_NAME value seen by the web application (PHP script 
> in this case) is
> We often configure web applications on a per-host basis, so that e.g. 
> database configuration information is kept in a hash keyed by SERVER_NAME 
> values. This means we need to have SERVER_NAME contain the
> We've achieved this straightforwardly by setting
> 	fastcgi_param	SERVER_NAME	$http_x_forwarded_host;
> which works nicely, but means that all applications responding to different 
> DNS names (not viewed externally via Apache reverse proxy) fail, because they 
> don't have an X-FORWARDED-HOST header, thus fall back to the local machine 
> hostname, which is not in the configuration hash.
> We can get round this by creating separate server {} configurations for 
> applications which need to be served behind a remote reverse proxy, but that 
> defeats the object of our generic per-host configuration based on hostnames.
> So ideally I'd like to do something like:
> 	fastcgi_param	SERVER_NAME	$server_name;
> 	if ($http_x_forwarded_host) {
> 		fastcgi_param	SERVER_NAME	$http_x_forwarded_host;
> 	}
> but: firstly "fastcgi_param" is not supported inside "if", and secondly I 
> don't know how or if this "overriding" would work.
> What's the best way to do this, please?

You may try something like this:

     set $blah $server_name;
     if ($http_x_forwarded_host) {
         set $blah $http_x_forwarder_host;

     fastcgi_param SERVER_NAME $blah;

Maxim Dounin

More information about the nginx mailing list