gpt4 book ai didi

Nginx $ request_uri具有重复的查询参数

转载 作者:行者123 更新时间:2023-12-04 10:17:41 25 4
gpt4 key购买 nike

我发现nginx的$ request_uri复制了查询参数。

我要实现的目标是将裸域的任何请求重定向到www域。这是一个示例配置。

    server {
listen 8080;
server_name localhost;
location / {
if ($http_host !~* "^www\.") {
rewrite (.*) http://www.$http_host$request_uri permanent;
}
}
}

我得到的结果是这样的:
curl -I http://127.0.0.1:8080/pp/\?a\=b

HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.2
Date: Thu, 22 Jan 2015 04:07:39 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://www.127.0.0.1:8080/pp/?a=b?a=b

查询参数在结果中重复;我在这里想念什么吗?

最佳答案

您看到的查询参数的复制是Nginx的预期行为。要更改此设置,您需要在重写中添加尾随?,如下所示:

   server {
listen 8080;
server_name localhost;
location / {
if ($http_host !~* "^www\.") {
rewrite (.*) http://www.$http_host$request_uri? permanent;
}
}
}

See Documentation Here

If a replacement string includes the new request arguments, the previous request arguments are appended after them. If this is undesired, putting a question mark at the end of a replacement string avoids having them appended, for example:

rewrite ^/users/(.*)$ /show?user=$1? last;



但是,对于所需的重定向类型,Aleksey Deryagin给出的配置是更好,更有效的选择,因为无论是否需要,每个请求都将由原始配置中的 if块评估。

关于Nginx $ request_uri具有重复的查询参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081159/

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