排除在 Nginx 中記錄多個查詢參數?

排除在 Nginx 中記錄多個查詢參數?

這個問題類似於排除在 Nginx 中記錄的特定查詢參數?但對於多個參數。我想做的是混淆全部我指定的查詢參數出現在請求 URI 中。例如,假設我有以下請求:

GET /index.html?latitude=55.70&longitude=32.2341&otherkey=value HTTP/1.1

那我兩個都想要latitude longitude在日誌中進行混淆:

GET /index.html?latitude=***&longitude=***&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=|longitude=)([^&]*)(.*)$   "$1$2***$4";
        default                 $request;
}

那麼只考慮正規表示式中的最後一個參數,結果將是:

GET /index.html?latitude=55.70&longitude=***&otherkey=value HTTP/1.1

這是不是我想要的是。

所以問題是,如何設定 Nginx 進行混淆全部我定義的給定(查詢/uri)參數?

我正在使用 Nginx 1.19.5。

答案1

您可以級聯map語句。它可能不是很有效,但很容易擴展。此外,您還需要使用命名捕獲,因為數位捕獲將被覆蓋。

例如:

map $request $custom1 {
    ~^(?<prefix1>.*[\?&]latitude=)([^&]*)(?<suffix1>.*)$  "${prefix1}***$suffix1";
    default                                               $request;
}
map $custom1 $customrequest {
    ~^(?<prefix2>.*[\?&]longitude=)([^&]*)(?<suffix2>.*)$ "${prefix2}***$suffix2";
    default                                               $custom1;
}

相關內容