【问题标题】:Wildcard nginx rewrite通配符 nginx 重写
【发布时间】:2023-03-13 17:38:02
【问题描述】:
我怀疑这会很简单,但我似乎无法理解 nginx 重写系统。希望有人能插话并帮助我吗?
很简单,我需要..
http://example.com/?c=foo
要改写成...
http://foo.example.com
如果您对这个 nginx 菜鸟感到厌烦,请提前感谢您 :)
我会继续尝试,万一我遇到了“尤里卡”时刻,我会更新它以帮助任何遇到/将遇到类似“问题”的人。
干杯。
【问题讨论】:
标签:
nginx
subdomain
rewrite
wildcard
【解决方案1】:
实际上,在 Nginx 中无法进行内部重定向到其他域。但是有两种可用的解决方案:
1) 简单:重定向到指定地址。示例:
if ( $arg_c ) {
rewrite ^ http://$arg_c.example.com;
}
2) 复杂:如果你使用 Nginx 作为 php-fpm、apache 或其他东西之间的代理,你可以这样设置 Host 头::
设置 $new_host example.com;
if ( $arg_c) {
set $new_host $arg_c.example.com;
}
并将其作为参数传递:
proxy_set_header Host $new_host;