Nginx anonymize access log

From snippet wiki
Jump to navigation Jump to search

To cut of privacy related data like the client IP number, create an nging global definition in conf.d/anon.conf:

map $remote_addr $remote_addr_anon {
    ~(?P<ip>\d+\.\d+\.\d+)\.    $ip.0;
    ~(?P<ip>[^:]+:[^:]+):       $ip::;
    default                     0.0.0.0;
}
log_format anonymized '$remote_addr_anon - $remote_user [$time_local] '
   '"$request" $status $body_bytes_sent '
   '"$http_referer" "$http_user_agent"';

And use that definition within your virtual host log definitions:

access_log /var/log/nginx/snippet/access.log anonymized;