我想出了两个主意
第一个想法:单独的配置部分(不是通常的视图,没有隔离)
Inner-server 和outer-server 指令可以放在另外两个文件中。
这里没有出现隔离问题,即 app2 覆盖来自 app1 的内容,并针对不可覆盖的内容发出错误警报。
注意:以下示例仅用于简单演示,并非实际练习。
文件系统的树
| /etc/nginx
|-- nginx.conf
|-- conf.d
|--|-- some_server_name_and_port_80.conf
|--|-- some_server_name_and_port_80.outer_server.d
|--|--|-- app1.outer_server.conf
|--|--|-- app2.outer_server.conf
|--|-- some_server_name_and_port_80.inner_server.d
|--|--|-- app1.inner_server.conf
|--|--|-- app2.inner_server.conf
文件内容
nginx.conf:
...
http {
...
# If default_type is already defined, just replace its value
default_type text/plain ;
include /etc/nginx/conf.d/*.conf ;
}
some_server_name_and_port_80.conf:
include /etc/nginx/conf.d/some_server_name_and_port_80.outer_server.d/*.conf ;
server {
listen 80 ;
server_name some_server_name ;
include /etc/nginx/conf.d/some_server_name_and_port_80.inner_server.d/*.conf ;
}
app1.outer_server.conf(对于 app2 将 app1 替换为 app2):
map $request_uri $app1_var {
~^/app1(.*) /app$1 ;
default $request_uri ;
}
app1.inner_server.conf(对于 app2 将 app1 替换为 app2):
location /app1/ {
return 200 "app1: Hello, World! The universal uri is $app1_var" ;
}
第二种方式:使用不同的服务器和使用proxy_pass的简单路由(需要注意proxy_pass的特性)
注意:以下示例仅用于简单演示,并非实际练习。
文件系统的树
| /etc/nginx
|-- nginx.conf
|-- conf.d
|--|-- some_server_name_and_port_80.conf
|--|-- some_server_name_and_port_80.d
|--|--|-- app1.router.conf
|--|--|-- app2.router.conf
|--|-- app1.conf
|--|-- app2.conf
文件内容
nginx.conf:
...
http {
...
include /etc/nginx/conf.d/*.conf ;
}
some_server_name_and_port_80.conf:
server {
listen 80 ;
server_name some_server_name ;
# If default_type is already defined, just replace its value
default_type text/plain ;
include /etc/nginx/conf.d/some_server_name_and_port_80.d/*.conf ;
}
app1.router.conf(对于 app2,将 app1 替换为 app2,将 81 替换为 82):
location /app1/ {
proxy_pass http://127.0.0.1:81 ;
}
app1.conf(对于 app2,将 app1 替换为 app2,将 81 替换为 82):
map $request_uri $app1_var {
~^/app1(.*) /app$1 ;
default $request_uri ;
}
server {
listen 81 ;
server_name some_server_name ;
location /app1/ {
return 200 "app1: Hello, World! The universal uri is $app1_var" ;
}
}