[英]Regex for nginx server_name
I would like to use a regex on my nginx server_name
that functions almost like a wildcard.我想在我的 nginx
server_name
上使用一个正则表达式,它的功能几乎就像一个通配符。
*-dev.mydomain.com -> dev server (localhost port 3001) *-dev.mydomain.com -> 开发服务器(本地主机端口 3001)
*-staging.mydomain.com -> staging server (localhost port 3002) *-staging.mydomain.com -> 登台服务器(本地主机端口 3002)
everything else -> prod server (localhost port 3000)其他一切-> prod 服务器(本地主机端口 3000)
However I cannot for the life of me get this to work.但是,我一辈子都无法让它发挥作用。
I seemingly get it working on https://regexr.com/51teh - but I'm not able to apply it correctly to my nginx config.我似乎可以在https://regexr.com/51teh上运行它 - 但我无法将它正确应用于我的 nginx 配置。
Here is my staging config now (not working, not catching requests to *-staging.mydomain.com
):这是我现在的暂存配置(不工作,不捕获对
*-staging.mydomain.com
的请求):
server {
listen 443 ssl;
server_name "~.*-staging\.mydomain\.com";
location / {
proxy_pass http://localhost:3002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header x-forwarded-for $remote_addr;
}
}
Try adding another virtual server block with default_server
to the listen directive.尝试将另一个带有
default_server
的虚拟服务器块添加到 listen 指令。 Something like the following:类似于以下内容:
server {
listen 443 ssl default_server;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 443 ssl;
server_name "~.*-staging\.mydomain\.com";
location / {
proxy_pass http://localhost:3002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header x-forwarded-for $remote_addr;
}
}
This should work.这应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.