我正在尝试使用重写规则从同一IP /域为多个反向代理应用程序提供服务。最初,当我只有两个站点时,我将所有内容存储在一个配置中,如下所示:
upstream app_server1 {
server 127.0.0.1:8001 fail_timeout=0;
}
upstream app_server2 {
server 127.0.0.1:8002 fail_timeout=0;
}
server {
listen 80 deferred;
client_max_body_size 100M;
server_name $hostname;
keepalive_timeout 100;
location /A {
try_files $uri @proxy_to_app1;
}
location @proxy_to_app1 {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
rewrite ^/A/(.*) /$1 break;
proxy_pass http://app_server1;
}
# -------------------------------
location /B {
try_files $uri @proxy_to_app2;
}
location @proxy_to_app2 {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
rewrite ^/B/(.*) /$1 break;
proxy_pass http://app_server2;
}
}
,这正常工作。但是,如果我尝试将其拆分为两个将都位于sites-enabled
中的配置文件,则它们均无法正常工作,并且如果我尝试访问"/usr/share/nginx/html/A/data" failed (2: No such file or directory)
,则会收到类似http://<my_ip>/A/data
的错误:
site_a.config:
upstream app_server1 {
server 127.0.0.1:8001 fail_timeout=0;
}
server {
listen 80;
client_max_body_size 100M;
server_name $hostname;
keepalive_timeout 100;
location /A {
try_files $uri @proxy_to_app1;
}
location @proxy_to_app1 {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
rewrite ^/A/(.*) /$1 break;
proxy_pass http://app_server1;
}
}
和
site_b.config:
upstream app_server2 {
server 127.0.0.1:8002 fail_timeout=0;
}
server {
listen 80;
client_max_body_size 100M;
server_name $hostname;
keepalive_timeout 100;
location /B {
try_files $uri @proxy_to_app2;
}
location @proxy_to_app2 {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
rewrite ^/B/(.*) /$1 break;
proxy_pass http://app_server2;
}
}
没有其他活动的nginx配置,但是似乎找不到任何一条路由。有什么想法或建议吗?