nginx跳转配置如何做到只跳转首页

2025-04-15 05:23:28
推荐回答(1个)
回答1:

确保安装 nginx 时 ./configure 有加入 --with-http_perl_module 这一项,
然后在 nginx.conf 配置文件的http{}内、server{}前面加入:(加入内容在结构上与 server{} 并列)
perl_set $urldecode 'sub {my $inf = shift;
my $url = $inf-args;
my $inx = index($url, "redirect=",);
my $redirect = "";
if ($inx < 0) {
$redirect = $url;}else {my $red = substr($url, $inx+9,);
my $len = index($red, "\x26",);
if ($len < 0) {
$redirect = substr($red, 0,);}else{$redirect = substr($red, 0, $len);}}$redirect =~ s/\%([A-Fa-f0-9]{2})/pack("C", hex($1))/seg;
return $redirect;}';
在 server 的 location 内加入:(加入内容在 location 之内,location 在 server 之内)
if ($arg_redirect) {
最后,重新启动 nginxPS: 以上的$urldecode是变量名,可以更改