我需要大师的建议。
在 Nginx 上conf
文件中,我想将子域作为变量,以便按如下方式重定向访问。
- ACCESS:
http://userX.example.com/?hoo=bar
- 重定向:
http://example.com/userX/?hoo=bar
但我得到
- 重定向:
http://example.com/userX.example.com/?hoo=bar
我现在的_http.conf
设置如下。显然它是这样工作的。
## default HTTP
server {
listen 80;
server_name default_server;
return 301 http://example.com/$host$request_uri;
}
有没有像下面这样的变量或方法?
## default HTTP
server {
listen 80;
server_name default_server;
return 301 http://example.com/$subdomain$request_uri;
}
我知道如果子域部分受到限制,它可以被重定向,但我每次都必须添加它们,并且我希望它保持尽可能简单。
有什么简单的方法可以做到这一点吗?
[ENV]: CentOS:7.3.1611, nginx: nginx/1.13.3, *.example.com 在 NS 设置中定位到同一服务器。
结论 (2017/12/13)
使用正则表达式:
server {
listen 80;
server_name ~^(?<subdomain>.+)\.example\.com$;
return 301 http://example.com/$subdomain$request_uri;
}
Ref: Comment https://stackoverflow.com/questions/45318758/nginx-variable-for-subdomain/45321927#45321927, Document http://nginx.org/en/docs/http/server_names.html#regex_names
您可以在中使用正则表达式server_name
将您需要的部分提取为命名捕获。例如:
server {
listen 80;
server_name ~^(?<name>.+)\.example\.com$;
return 301 http://example.com/$name$request_uri;
}
See 这个文件 http://nginx.org/en/docs/http/server_names.html#regex_names了解更多。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)