Redirect 如何使用nginx重定向到自定义url?

Redirect 如何使用nginx重定向到自定义url?,redirect,nginx,config,Redirect,Nginx,Config,我试图用nginx实现一个简单的自定义重定向 传入请求: http://localhost:8182/testredirect/?asd=456&aaa=ddd&trueurl=http://example.com/sdd?djdj=55 我想接收HTTP 302重定向到http://example.com/sdd?djdj=55。即转发到trueurl参数之后的任何内容 我试试这个: location /testredirect/ { rewrite "\&t

我试图用nginx实现一个简单的自定义重定向

传入请求:

http://localhost:8182/testredirect/?asd=456&aaa=ddd&trueurl=http://example.com/sdd?djdj=55
我想接收HTTP 302重定向到
http://example.com/sdd?djdj=55
。即转发到
trueurl
参数之后的任何内容

我试试这个:

location /testredirect/ {
    rewrite "\&trueurl=(.*)$" $1 redirect;
}
但这似乎不起作用。它返回错误404。
我遗漏了什么吗?

rewrite正则表达式不会对URI的查询字符串部分进行操作,因此您的代码永远不会匹配。但是,相关参数已被捕获为
$arg\u trueurl
。有关详细信息,请参阅

例如:

location /testredirect/ {
    return 302 $arg_trueurl;
}

感谢@richard smith提供有关查询字符串的有用说明。最后,我得出以下结论:

location /testredirect/ {
    if ($args ~* "\&trueurl=http(.*)$") {
        return 302 http$1;
    }
}

我会尝试重写“\&trueurl=http://(.*)$”http://$1重定向取而代之<如果
rewrite
的第二个参数以
http://
https://
开头,则code>nginx的行为会有所不同,这是有道理的,但不幸的是
trueurl
也可能包含未替换的参数。我需要用正则表达式解析查询字符串,以提取
&trueurl=
之后的任何内容并重定向到它。