I have seen similar questions but not specifically this, i don't want to stop logging urls with a parameter, i want to log them without it.
example initial url:
example.com/dir/file.php?important=1&ignore=random&others=yes
How it needs to be logged as:
example.com/dir/file.php?important=1&others=yes
Basically i want to filter out the 'ignore' parameter while keeping the rest intact, if present.
you will need to create a custom log format, including a custom variable, and not including the $request variable. For example:
log_format main '$remote_addr - $remote_user [$time_local] "$custom_request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
I'm adding the variable "$custom_request", that doesn't exist. So you need to create it first. And you can create it based on the $request, using regex to remove what you don't want.
set $custom_request $request;
if ($request_uri ~ "([^\?]*)\?(.*)ignore=([^&]*)&?(.*)") {
set $original_path $1;
set $args_before $2;
set $unwanted $3;
set $args_after $4;
set $args "";
set $custom_request "${original_path}?${args_before}${args_after}";
}
Update to Carlos's answer: Same:
log_format main '$remote_addr - $remote_user [$time_local] "$custom_request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
set $custom_request "";
if ($request_uri ~ "([^\?]*)\?(.*)auth=([^&]*)&?(.*)") {
set $original_path $1;
set $before $2;
set $unwanted $3;
set $after $4;
set $custom_request "${request_method} ${original_path}?${before}auth=*****${after} $server_protocol";
}
Note that the set and if directives must be put in the server block. I took out the line set $args ""; because it wiped the $args variable clean for no reason and impacted more than the logs.