I have an /etc/nginx/nginx.conf
with this basic structure:
http {
server {
listen 80;
server_name example.com;
root /var/html;
location / {
rewrite ...;
}
}
server {
listen 443;
server_name example.com;
root /var/html;
location / {
rewrite ...;
}
}
}
There are many rewrite
directives, all identical between the http and https server
blocks, so I tried to extract them into an external file:
http {
server {
listen 80;
include /etc/nginx/redirects.conf;
}
server {
listen 443;
include /etc/nginx/redirects.conf;
}
}
The redirects.conf
file just contains:
server_name example.com;
root /var/html;
location / {
rewrite ...;
}
nginx -t -c /etc/nginx.conf
tells me the syntax is fine, but the rewrite rules aren't taking effect. I know the file is being evaluated because if I introduce a syntax error, nginxs barfs. But the rewrites are ignored unless I keep them in the main nginx.conf
file.
Workaround I will probably use: make use of the "map" module (sample answer showing how this is done here). Another thing I could use is Ansible to template out the config file, at least making the duplication machine-generated, but I'd still like to know why this is happening to better understand the limitations of the include
directive.