排除在 Nginx 中記錄的特定查詢參數?

排除在 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"';

無論請求路徑如何,假設我希望不記錄“緯度”參數(或最好將其混淆)。我知道我可以排除全部透過將“$request”更改為“$request_method $uri”來查詢參數,但後來我輸了全部這不是我想要的參數。

更新:

我想要GET /index.html?latitude=43.4321&otherkey=value HTTP/1.1混淆這樣的事情:GET /index.html?latitude=******&otherkey=value HTTP/1.1

答案1

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;
}

您可以新增多個關鍵字,如下所示:~^(.*)(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;
}

相關內容