【问题标题】:NGINX Rewrite is encoding querystring as a pathNGINX Rewrite 将查询字符串编码为路径
【发布时间】:2021-08-21 22:57:47
【问题描述】:

我想在内部将多个位置 /customers/foo?bar=2 重写为我的 nginx 配置中/blah 的现有位置,这样就好像请求是针对/blah/customers/foo?bar=2

location /blah {
  # View in fiddler
  proxy_pass http://127.0.0.1:8888/;
  # Lots of config here I don't want to repeat everywhere else
}
location /customers/ {
  rewrite ^/customers/(.*) /blah/customers/$1$is_args$args;
}
location /other/ {
  rewrite ^/other/(.*) /blah/other/$1$is_args$args;
}
# etc...

Nginx 正在使用编码为路径/blah/customers/foo%34bar=2 的查询字符串重写 URL。

rewrite ^ /blah$request_uri; 也会发生同样的事情。它将? 编码为%3F,有效地混淆了URL。

如果我执行客户端重定向 rewrite ^ /blah$request_uri permanent;,则 URL 是正确的并且包含 ?,但我想要在我的 NGINX 配置中进行内部重定向

【问题讨论】:

    标签: nginx


    【解决方案1】:

    不要使用$is_args$args,因为rewrite directive 会自动附加任何现有的查询字符串。

    例如:

    rewrite ^/customers/(.*) /blah/customers/$1 last;
    

    虽然,我更喜欢:

    rewrite ^(.*)$ /blah$1 last;
    

    甚至:

    rewrite ^ /blah$uri last;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 2011-04-01
      • 2016-11-17
      • 2012-10-11
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      相关资源
      最近更新 更多