Skip to content

Instantly share code, notes, and snippets.

@iCyLand
Last active April 18, 2022 15:25
Show Gist options
  • Save iCyLand/816497b74c6c9e7d664b3c15f8d3c7ca to your computer and use it in GitHub Desktop.
Save iCyLand/816497b74c6c9e7d664b3c15f8d3c7ca to your computer and use it in GitHub Desktop.
W3C json log format for nginx
## this GeoIP dat file relate on centos environment please change to your OS path
geoip_country /usr/share/GeoIP/GeoIP-initial.dat;
map $request_uri $request_uri_path {
"~^(?P<path>[^?]*)(\?.*)?$" $path;
}
map $upstream_response_time $new_upstream_response_time {
default $upstream_response_time;
'' null;
}
log_format w3cjson escape=json
'{'
'"Date":"$time_iso8601",'
'"Client IP Address":"$remote_addr",'
'"Client Username":"$remote_user",'
'"Server IP Address":"$server_addr",'
'"Request Port":$server_port,'
'"Request Protocol":"$server_protocol",'
'"Request Method":"$request_method",'
'"Request URI":"$request_uri_path",'
'"Query String":"$query_string",'
'"Status":$status,'
'"User Agent":"$http_user_agent",'
'"Cookie":"$http_cookie",'
'"Referer":"$http_referer",'
'"Bytes Received":$request_length,'
'"Bytes Sent":$body_bytes_sent,'
'"Request Time":$request_time,'
'"X-Forwarded-For":"$http_x_forwarded_for",'
'"Upstream":"$upstream_addr",'
'"Upstream Response Time":$new_upstream_response_time,'
'"Upstream Cache Status":"$upstream_cache_status",'
'"Request Country Code":"$geoip_country_code"'
'}';
@iCyLand
Copy link
Author

iCyLand commented Jul 3, 2018

Save this file to /etc/nginx/conf.d/ (for nginx on centos)
So enable GeoIP too for locating a source location.

How to use.
access_log /path/to/log/vhostname.access.log w3cjson;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment