![nginx:当两个服务器具有相同的 TLD 时,如何首先匹配具有较短正则表达式的服务器?](https://linux22.com/image/785665/nginx%EF%BC%9A%E5%BD%93%E4%B8%A4%E4%B8%AA%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%85%B7%E6%9C%89%E7%9B%B8%E5%90%8C%E7%9A%84%20TLD%20%E6%97%B6%EF%BC%8C%E5%A6%82%E4%BD%95%E9%A6%96%E5%85%88%E5%8C%B9%E9%85%8D%E5%85%B7%E6%9C%89%E8%BE%83%E7%9F%AD%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%9F.png)
我正在尝试配置 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 根据主机使用正确的服务器块?