Skip to content

Instantly share code, notes, and snippets.

@dirkjonker
Last active December 5, 2022 11:10
Show Gist options
  • Save dirkjonker/679622b9e6fc713165d35aa3b79a882f to your computer and use it in GitHub Desktop.
Save dirkjonker/679622b9e6fc713165d35aa3b79a882f to your computer and use it in GitHub Desktop.
NGINX JSON log configuration for Google Cloud / StackDriver log format
# this outputs JSON formatted logs which are easy to parse for any logging agent (such as fluentd)
# the format conforms to Google Cloud Logging so logs are nicely structured in the logs viewer
# see also: https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#httprequest
# put the following lines in your nginx config
# /etc/nginx/nginx.conf
log_format json_combined escape=json
'{'
'"time":"$msec",'
'"httpRequest":{'
'"requestMethod":"$request_method",'
'"requestUrl":"$scheme://$host$request_uri",'
'"requestSize":$request_length,'
'"status":"$status",'
'"responseSize":$bytes_sent,'
'"userAgent":"$http_user_agent",'
'"remoteIp":"$remote_addr",'
'"serverIp":"$server_addr",'
'"referer":"$http_referer",'
'"latency":"${request_time}s",'
'"protocol":"$server_protocol"'
'}'
'}';
access_log /var/log/nginx/access.log json_combined;
# example configuration for fluentd
# /etc/google-fluentd/config.d/nginx.conf
<source>
@type tail
format none
path /var/log/nginx/access.log
pos_file /var/lib/google-fluentd/pos/nginx-access.pos
read_from_head true
tag nginx-access
<parse>
@type json
</parse>
</source>
@MaxWinterstein
Copy link

Nice start of my wish to have better/custom nginx logs at google stackdriver site.

Maybe worth mentioning that the value types are importat, and e.g. status must be int, not string like your example. See https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#HttpRequest

Working for me at google k8s behind ambassador gateway:

log_format json_combined
  '{'
    '"time":"$msec",'
    '"httpRequest":{'
      '"requestMethod":"$request_method",'
      '"requestUrl":"$scheme://$host$request_uri",'
      '"requestSize":$request_length,'
      '"status":$status,'
      '"responseSize":$bytes_sent,'
      '"userAgent":"$http_user_agent",'
      '"remoteIp":"$http_x_forwarded_for",'
      '"serverIp":"$server_addr",'
      '"referer":"$http_referer",'
      '"latency":"${request_time}s",'
      '"protocol":"$server_protocol"'
    '}'
  '}';

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