jsonnginxhex

nginx logging $request_body as hexadecimal


I am trying to log the request body of requests to my api and nginx is turning all quotes (and some other characters like spaces and tabs) into hexadecimal characters.

Here is my log format

log_format postdata '{"ts": "$time_iso8601", "status": $status, "req": "$uri", "meth": "$request_method", "body": "$request_body"}';

Here is what gets logged

{"ts": "2015-05-20T15:31:11-07:00", "status": 400, "req": "/v2/track", "meth": "POST", "body": {\x22id\x22:\x22user id\x22}}

How can I prevent this so that the resulting log line is

{"ts": "2015-05-20T15:31:11-07:00", "status": 400, "req": "/v2/track", "meth": "POST", "body": {"id":"user id"}}

Solution

  • Sinse 1.13 there is an "escape=none" parameter that turns off data escaping.

    http://nginx.org/en/docs/http/ngx_http_log_module.html#log_format

    log_format  api_request_log escape=none '[$time_local] $request \n$request_body';