Nginx subdomain configuration

I have nginx acting as a reverse proxy for apache. Now I need to add a new subdomain that will serve files from another directory, but at the same time I want all the location and proxy_pass directives that I have for the default host to also apply to this subdomain.

I know that if I copy the rules from the default host to the new subdomain, it will work, but is there a way for the subdomain to inherit the rules? Below is a sample configuration

server { listen 80; server_name www.somesite.com; access_log logs/access.log; error_log logs/error.log error; location /mvc { proxy_pass http://localhost:8080/mvc; } location /assets { alias /var/www/html/assets; expires max; } ... a lot more locations } server { listen 80; server_name subdomain.somesite.com; location / { root /var/www/some_dir; index index.html index.htm; } } 

thank

+61
subdomain nginx
Mar 28 '12 at 10:13
source share
1 answer

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; } 
+74
Mar 28 2018-12-12T00:
source share



All Articles