Reuse configuration statements for domains in nginx.conf
nginx
Solution
you can do:
server_name one.example.org two.example.org;
if both are exactly identical except for the domainname
if you have just similar locationblocks you can move those locations to a separate file and then do an
include /etc/nginx/your-filename;
to easily use it in each serverblock
Problem
Let's say I have a nginx configuration set up for a domain like this: ``` server { root /path/to/one; server_name one.example.org; location ~ \.php$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` Now, if I want to add another domain with different content, is there a way I can re-use equivalent statements from the previous domain, or do I have to duplicate everything for every new domain I want to support? ``` server { root /path/to/two; # different server_name two.example.org; # different location ~ \.php$ { try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } ``` I tried moving the `location` directive outside the `server` closure, but obviously things don't work like that because I got an error "location directive is not allowed here" when restarting nginx.