variables in "include"

Marcus Clyne maccaday at gmail.com
Mon Jul 20 16:56:27 MSD 2009


Hi Kaspars,

Kaspars Dambis wrote:
>
> 'root' directive works fine, but the 'include' doesn't like the $host 
> variable in it.
The include directive is a server-start-time directive, and as such 
doesn't analyse variables as given with $xxx.  This has probably been 
done for at least two reasons - speed and configuration checking.

If the config files were analysed at runtime (like .htaccess files on 
Apache), that would slow things down.  Also, if the configuration file 
is wrong, what do you do?  Better to control it all at start time.
>
> I would like to avoid using server { ... } for each of the individual 
> hosts, in order to have global php settings.
>
> What would be the best way to approach this?
Depending on your configuration, you might be able to define one (or a 
few) server{} configurations, and use variables in other places than the 
'include' directive.  The server_name can take multiple values and 
wildcards, but many other settings can also take variables. 

If you need specific configurations for each server, then you can't 
really get around having a separate server{} block for each one (though 
you might be able to group them). 

One possible method:

nginx.conf :

# main settings (server-wide configuration)
...
http {
include      sites/all.conf;
}

sites/all.conf :

# just a list of all the servers you want to include (separating this 
from the main nginx.conf file might be useful for maintenance)

include      sites/site1;
include      sites/site2;
...

in each of the sites/site[n] files:

server {

include   global_settings.conf;      # this includes all php and other 
settings

# server-specific configurations
...
}

global_settings.conf :

# php settings could go here


Each time a new server was added, you'd need to add the relative 
'include site/site[n];' to the sites list and re-load the server 
configuration (you'd probably want to use the USR1 signal which just 
re-reads/loads the configuration, rather than fully reloading the 
software binary as a graceful restart would do).  This can be done using 
a shell-script, which you'd need to call somehow when you needed to.

If you have a great many hosts, then you may want to have a wildcard 
default server which would take all domains, then if a few hosts have 
more specific settings, to put them in separate server{} blocks.

Hope this is helpful.

Marcus.





More information about the nginx mailing list