Nginx子域配置 [英] Nginx subdomain configuration
问题描述
我让nginx充当apache的反向代理.我现在需要添加一个新的子域 它将提供来自另一个目录的文件,但同时我希望将默认主机拥有的所有location和proxy_pass指令也应用于子域.
I have nginx acting as a reverse proxy to apache. I now need to add a new subdomain that will serve files from another directory, but at the same time I want all location and proxy_pass directives that I have for the default host to apply to the subdomain also.
我知道,如果我将规则从默认主机复制到新的子域中,它将起作用,但是子域是否可以继承规则? 下面是一个示例配置
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;
}
}
谢谢
推荐答案
您可以将公共部分从两个服务器上下文中移至另一个配置文件和include
中.这应该起作用:
You could 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;
}
这是一个示例,该示例实际上是从运行中的服务器复制而来的.我在/etc/nginx/sites-enabled
中配置我的基本服务器设置(在Ubuntu/Debian上,nginx的常规设置).例如,我的主服务器bunkus.org
的配置文件是/etc/nginx/sites-enabled
,它看起来像这样:
Here's an example that's actually copied from my running server. I configure my basic server settings in /etc/nginx/sites-enabled
(normal stuff for nginx on Ubuntu/Debian). For example, my main server bunkus.org
's configuration file is /etc/nginx/sites-enabled
and it 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;
}
作为示例,这是两个server
上下文中包含的/etc/nginx/include.d/all-common
文件:
As an example here's the /etc/nginx/include.d/all-common
file that's included from 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;
}
这篇关于Nginx子域配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!