加入收藏 | 设为首页 | 会员中心 | 我要投稿 阜阳站长网 (https://www.0558zz.com/)- 科技、建站、内容创作、云计算、网络安全!
当前位置: 首页 > 运营中心 > Nginx > 正文

Nginx $request_uri有重复的查询参数

发布时间:2021-02-21 10:04:29 所属栏目:Nginx 来源:互联网
导读:我发现nginx的$request_uri复制了查询参数.我希望实现的目标是将裸域的任何请求重定向到www域.这是一个示例配置. server { listen 8080; server_name localhost; location / { if ($ht

我发现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块进行评估.

(编辑:阜阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读