nginx子域configuration

我有nginx作为一个反向代理到Apache。 我现在需要添加一个新的子域来提供来自另一个目录的文件,但同时我希望所有的位置和proxy_pass指令,我有默认主机也适用于子域。

我知道,如果我从默认主机复制规则到新的子域它将工作,但有没有办法让子域inheritance规则? 以下是一个示例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; } } 

谢谢

您可以将通用部分移动到另一个configuration文件,并include在两个服务器上下文中。 这应该工作:

 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; } 

编辑:这是一个实际上从我运行的服务器复制的例子。 我在/etc/nginx/sites-enabled (Ubuntu / Debian上的nginx的普通东西)中configuration我的基本服务器设置。 例如,我的主服务器bunkus.org的configuration文件是/etc/nginx/sites-enabled ,它看起来像这样:

 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; } 

作为一个例子,这里是包含在两个server上下文中的/etc/nginx/include.d/all-common文件:

 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; }