Skip to content

Instantly share code, notes, and snippets.

@Hendrik44
Last active June 8, 2018 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hendrik44/52b07cb19b983ad6ab574dae49941248 to your computer and use it in GitHub Desktop.
Save Hendrik44/52b07cb19b983ad6ab574dae49941248 to your computer and use it in GitHub Desktop.
anonymized log format configuration for nginx
# Add this to /etc/nginx/nginx.conf in http {} block
map $remote_addr $ip_anonym1 {
default 0.0.0;
"~(?P<ip>(\d+)\.(\d+)\.(\d+))\.\d+" $ip;
"~(?P<ip>[^:]+:[^:]+):" $ip;
}
map $remote_addr $ip_anonym2 {
default .0;
"~(?P<ip>(\d+)\.(\d+)\.(\d+))\.\d+" .0;
"~(?P<ip>[^:]+:[^:]+):" ::;
}
map $ip_anonym1$ip_anonym2 $ip_anonymized {
default 0.0.0.0;
"~(?P<ip>.*)" $ip;
}
log_format anonymized '$ip_anonymized - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
### use it like that ###
# server {
# ......
# access_log /var/log/nginx/access.log anonymized;
# ......
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment