Skip to content

Instantly share code, notes, and snippets.

@aaronfeng
Created July 30, 2012 14:15
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save aaronfeng/3207188 to your computer and use it in GitHub Desktop.
Save aaronfeng/3207188 to your computer and use it in GitHub Desktop.
nginx json log format
# /etc/nginx/nginx.conf
log_format main '{'
'"remote_addr": "$remote_addr",'
'"remote_user": "$remote_user",'
'"time_local": "$time_local",'
'"request": "$request",'
'"status": "$status",'
'"body_bytes_sent": "$body_bytes_sent",'
'"http_referer": "$http_referer",'
'"http_user_agent": "$http_user_agent"'
'}';
@runningman84
Copy link

This isn't safe if the user agent or other user generated content contains unescaped content.

@sandstrom
Copy link

I think it's fine in recent versions of nginx:

Changes with nginx 1.1.6 17 Oct 2011

*) Change: now the 0x7F-0x1F characters are escaped as \xXX in an
   access_log.

Changes with nginx 0.7.0 19 May 2008

*) Change: now the 0x00-0x1F, '"' and '\' characters are escaped as \xXX
   in an access_log.
   Thanks to Maxim Dounin.

@jolleon
Copy link

jolleon commented Jul 31, 2014

\xXX is not valid in a json string (http://json.org/)

@loveshell
Copy link

so ,it's not a good idead

@EdRowe
Copy link

EdRowe commented Nov 21, 2015

Yeah this is definitely not correct JSON escaping

@ayiis
Copy link

ayiis commented May 6, 2016

so, does an available solution come out?
oh please...

@xilin
Copy link

xilin commented Dec 22, 2016

@hzbd
Copy link

hzbd commented Dec 11, 2020

Nginx logging json format(http(s)&stream):
https://gist.github.com/hzbd/9dd768087f65d6026e14642bcc859e37

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