如何在NGINX中获取URL的子域 [英] How to get subdomain of URL in NGINX
问题描述
我想根据用户输入的子域进行重定向.
I am wanting to do a redirect based on what subdomain the user is entering.
例如:
<subdomain>.example.com/admin -> <subdomain>.myurl.com
理想情况下,我想将<subdomain>
作为参数传递给我的重定向URL.
Ideally I want to pass <subdomain>
as a parameter to my redirect URL.
我正在寻找与此类似的东西:
I was looking at something along the lines of this:
location ~ (sub).(somewhere).(com)/(some)(thing)/(something)(else) {
set $var1 = $1; # = sub in above example
set $var2 = $2; # = somewhere in above example
set $var3 = $3; # = com in above example
set $var4 = $4; # = some in above example
set $var5 = $5; # = thing in above example
set $var6 = $6; # = something in above example
set $var7 = $7; # = else in above example
rewrite ^ $1/$2 last; # would be sub/somewhere
}
根据此处的这篇文章:操纵或分割字符串(我认为变量集在此示例中是错误的,但您可以理解要点).
based on this post here: Manipulate or split string (I think the syntax of the variable set is wrong in this example but you get the gist).
推荐答案
URL的域名部分未通过location
指令进行测试.您将需要在server_name
指令中使用命名捕获.有关详细信息,请参见本文档.
The domain name part of the URL is not tested by the location
directive. You will need to use a named capture in the server_name
directive. See this document for details.
例如:
server {
server_name ~^(?<name>\w+)\.example\.com$;
location /admin {
return 301 $scheme://$name.myurl.com/;
}
}
这篇关于如何在NGINX中获取URL的子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!