我正在尝试配置 nginx 以支持两种服务器名称方案。
当前的:https://custid.hosted-environment.com/test/app-name/ 建议的:https://app-name.test.custid.hosted-environment.com
当前方案运行良好,但尝试同时支持当前方案和提议方案并让 nginx 根据主机直接指向正确的服务器似乎不起作用。
这是我正在使用的配置的缩写版本,我试图将其精简为仅相关的部分:
http {
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ~^(?<app_custid>[^.]+)(?<app_domain>.hosted-environment.com)$;
root /opt/nginx/apps/;
location ~ ^/(?<app_env>dev|test|prod)/(?<app_name>app-name)/server(?<request_path>.*) {
include /opt/nginx/config/nginx/proxy.conf;
proxy_pass https://backend_server;
}
location ~ ^/(?<app_env>dev|test|prod)/(?<app_name>app-name)(?<request_path>.*) {
ssi on;
ssi_types *;
root /opt/nginx/;
try_files
/apps/$app_env/$app_name/dist/client/$request_path
/apps/$app_env/$app_name/dist/client/index.html
=404;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name ~^(?<app_name>[^.]+)\.(?<app_env>dev|test|prod)\.(?<app_custid>[^.]+)(?<app_domain>.hosted-environment.com)$;
root /opt/nginx/;
location ~ ^/server(?<request_path>.*) {
include /opt/nginx/config/nginx/proxy.conf;
proxy_pass https://backend_server;
}
location ~ ^/(?<request_path>.*) {
root /opt/nginx/;
try_files
/apps/$origin_env/$origin_app_name/dist/client/$request_path
/apps/$origin_env/$origin_app_name/dist/client/index.html
=404;
}
}
}
使用此配置,无论server
我先放置哪个块,它都会处理请求。如何让 nginx 根据主机使用正确的服务器块?