Py学习  »  NGINX

Nginx Strip URL参数

gidiwe2427 • 3 年前 • 620 次点击  

我想在将url参数发送到proxy_pass之前删除它

对于访客请求url: => https://example.com/?wanted1=aaa&unwanted1=bbb&unwanted2=ccc&wanted2=ddd

然后将所有不需要的参数剥离到: => https://example.com/?wanted1=aaa&wanted2=ddd

我现在的方式是这样的:

if ($args ~ ^(.*)&(?:unwanted1|unwanted2)=[^&]*(&.*)?$ ) {
    set $args $1$2;
}

但它只删除1个参数,从不进行递归。如何解决这个问题?我想修改$args。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129264
 
620 次点击  
文章 [ 1 ]  |  最新文章 3 年前
Richard Smith
Reply   •   1 楼
Richard Smith    3 年前

如果需要递归,可以使用 rewrite...last 从一年之内 location 块在生成内部服务器错误之前,Nginx只能容忍少量的递归(可能是十次迭代)。

例如:

location / {
    if ($args ~ ^(?<prefix>.*)&(?:unwanted1|unwanted2)=[^&]*(?<suffix>&.*)?$ ) {
        rewrite ^ $uri?$prefix$suffix? last;
    }
    ...
}

请注意,您需要使用命名捕获,因为当 rewrite 语句被评估。

注意 重写 需要拖尾 ? 防止将现有参数附加到重写的URI。看见 this document 详细信息。