You can move the common parts to another configuration file and include from both server contexts. This should work:
server { listen 80; server_name server1.example; ... include /etc/nginx/include.d/your-common-stuff.conf; } server { listen 80; server_name another-one.example; ... include /etc/nginx/include.d/your-common-stuff.conf; }
Edit: Here is an example that is actually copied from my working server. I configure the basic server settings in /etc/nginx/sites-enabled (normal stuff for nginx on Ubuntu / Debian). For example, the configuration file for my main server is bunkus.org /etc/nginx/sites-enabled and looks like this:
server { listen 80 default_server; listen [2a01:4f8:120:3105::101:1]:80 default_server; include /etc/nginx/include.d/all-common; include /etc/nginx/include.d/bunkus.org-common; include /etc/nginx/include.d/bunkus.org-80; } server { listen 443 default_server; listen [2a01:4f8:120:3105::101:1]:443 default_server; include /etc/nginx/include.d/all-common; include /etc/nginx/include.d/ssl-common; include /etc/nginx/include.d/bunkus.org-common; include /etc/nginx/include.d/bunkus.org-443; }
An example is the /etc/nginx/include.d/all-common file, which is included in both server contexts:
index index.html index.htm index.php .dirindex.php; try_files $uri $uri/ =404; location ~ /\.ht { deny all; } location = /favicon.ico { log_not_found off; access_log off; } location ~ /(README|ChangeLog)$ { types { } default_type text/plain; }
Moritz Bunkus Mar 28 2018-12-12T00: 00Z
source share