4

I wonder if it's possible to exclude specific query parameters in the URI from being logged to the Nginx access log?

Here's our current configuration:

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"';

Regardless of the request path, let's say I want the "latitude" parameter to be excluded from being logged (or preferably, it should be obfuscated). I know I can exclude all query parameters by changing "$request" to e.g. "$request_method $uri", but then I lose all parameters which is not what I want.

Update:

I want GET /index.html?latitude=43.4321&otherkey=value HTTP/1.1 to be obfuscated to something like this: GET /index.html?latitude=******&otherkey=value HTTP/1.1

7

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

Here's the code:

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

You can add multiple keywords like this: ~^(.*)(latitude|dell|inspiron)(.*)$

Edit:
After specification in the comment the regex needs to be modified:
GET /index.html?latitude=5570&otherkey=value HTTP/1.1 becomes
GET /index.html?latitude=***&otherkey=value HTTP/1.1

map $request $customrequest {
        ~^(.*)([\?&]latitude=)([^&]*)(.*)$   "$1$2***$4";
        default                 $request;
}
3
  • Thanks, but I want to value to be obfuscated for the specific key. I don't know the "value" of the key beforehand. – Johan Dec 15 '20 at 12:52
  • 3
    The more information you give in the beforehand the better are the answers. – unNamed Dec 15 '20 at 13:05
  • I've asked a similar question for multiple parameters here: serverfault.com/questions/1046302/… – Johan Dec 15 '20 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.