In the lines below, I might have a site-specific configuration file that contains additional fastcgi_params unique to that site. If this file exists I want to load it.
server {
listen 80 default;
server_name _;
root /path/www/$host;
# Pass PHP scripts to php-fastcgi listening on port 9000
location ~ \.php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
if (-f /path/www/$host/nginx.conf) {
include /path/www/$host/nginx.conf;
}
}
}
However, this doesn't work and the error I get is:
nginx: [emerg] "include" directive is not allowed here in ...
Update
I thought that rather than checking separatly, I could let include check for me.
server {
listen 80 default;
server_name _;
root /path/www/$host;
# Pass PHP scripts to php-fastcgi listening on port 9000
location ~ \.php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
include /path/www/$host/*.nginx;
}
}
However, this doesn't seem to be working.
Well, this is rather old, but anyway, I found a work-around.
I have a setup with vhosts configured in this style:
Instead of checking if the file exists (which isn't possible), I simply do:
This way I can simply create a file in the
sites-customizations
-folder, which by my convention, is named the same as the main configuration. The*
works pretty much like the if, as it doesn't break if there is no extra configuration files. If you like, this also enables you to add multiple extra configurations in separate files.include
does its thing during server start - runtime variables like$host
can't be used, nor is it able to be used in anif
context, as you found out.You'd need to split up the
server
blocks for different hosts. Sorry!I take advantage of the
glob()
function pattern expansion, which is used by nginx in theinclude
directive, and works well on my Debian servers.In your case, try to replace this line:
with this one:
it's a file mask that matches only one file, and won't make nginx complain if the file does not exist. However variables are not allowed in include directives, so you must supply a fixed value in
<hostname>
. We had to create a script that generates individual .conf files (one per vhost).Edit
As Gerald Schneider pointed out, I was suggesting to keep
$host
in the replacement, which is not allowed. I've changed the suggestion above.I know this is old but solution might help someone looking for an answer like I did.
I had a bunch of redirects that I needed to include for a specific host only and while
include
is not allowed in theif
directive, I ended up adding theif
inside the file I was including...So I have an
include /etc/nginx/conf.d/redirects
and inside that file I have: