在 Nginx 中排除特定的查询参数?

Joh*_*han 4 nginx

我想知道是否可以将 URI 中的特定查询参数排除在记录到 Nginx 访问日志之外?

这是我们当前的配置:

log_format  main  '$remote_addr - $remote_user [$time_local] $host "$request" '
                      '$status $body_bytes_sent $request_time "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
Run Code Online (Sandbox Code Playgroud)

无论请求路径如何,假设我希望从记录中排除“纬度”参数(或者最好应该对其进行混淆)。我知道我可以通过将“$request”更改为例如“$request_method $uri”来排除所有查询参数,但随后我丢失了所有不是我想要的参数。

更新:

我想GET /index.html?latitude=43.4321&otherkey=value HTTP/1.1被混淆到这样的事情:GET /index.html?latitude=******&otherkey=value HTTP/1.1

unN*_*med 7

GET /index.html?key=latitude&otherkey=value HTTP/1.1
变成 GET /index.html?key=***&otherkey=value HTTP/1.1

这是代码:

log_format  main  '$remote_addr - $remote_user [$time_local] $host "$customrequest" '
                      '$status $body_bytes_sent $request_time "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
map $request $customrequest {
        ~^(.*)(latitude)(.*)$   "$1***$3";
        default                 $request;
}
Run Code Online (Sandbox Code Playgroud)

您可以像这样添加多个关键字: ~^(.*)(latitude|dell|inspiron)(.*)$

编辑:
在注释中指定后,正则表达式需要修改:
GET /index.html?latitude=5570&otherkey=value HTTP/1.1变为
GET /index.html?latitude=***&otherkey=value HTTP/1.1

map $request $customrequest {
        ~^(.*)([\?&]latitude=)([^&]*)(.*)$   "$1$2***$4";
        default                 $request;
}
Run Code Online (Sandbox Code Playgroud)

  • 您事先提供的信息越多,答案就越好。 (3认同)