gpt4 book ai didi

nginx - 如何在NGINX中获取URL的子域

转载 作者:行者123 更新时间:2023-12-04 17:44:42 26 4
gpt4 key购买 nike

我想根据用户输入的子域进行重定向。

例如:
<subdomain>.example.com/admin -> <subdomain>.myurl.com
理想情况下,我想将<subdomain>作为参数传递给我的重定向URL。

我在看一些类似的东西:

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
}

基于此处的帖子: Manipulate or split string(我认为该变量集的语法在此示例中是错误的,但您可以理解要点)。

最佳答案

URL的域名部分未经location指令测试。您将需要在server_name指令中使用命名捕获。有关详细信息,请参见this document

例如:

server {
server_name ~^(?<name>\w+)\.example\.com$;

location /admin {
return 301 $scheme://$name.myurl.com/;
}
}

关于nginx - 如何在NGINX中获取URL的子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39563071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com