GrepBoard/Manuals
605 manuals ·

Fix broken nginx JSON access logs with escape=json

·Tested on nginx 1.30.6 (Docker) · jq 1.7 · Python 3.12 · 2026-10·Test scope ↓·6 steps

An nginx JSON log_format without escape=json breaks on quotes, non-ASCII and control characters. Seven requests compare jq parsing in three escape modes.

output
jq: parse error: Invalid escape at line 1, column 167

If jq or your log shipper drops lines from a JSON-shaped log_format with this error, escape=json is missing. nginx's default escaping turns " into \x22, and \x is not valid JSON, so the whole line fails to parse.

[1] Add escape=json to the format, right after the format name and before the string.

/etc/nginx/nginx.conf
http {    log_format json_escaped escape=json        '{"time":"$time_iso8601","remote_addr":"$remote_addr",'        '"request":"$request","status":$status,'        '"referer":"$http_referer","user_agent":"$http_user_agent"}';    access_log /var/log/nginx/access.log combined;          # keep the existing line    access_log /var/log/nginx/access-json.log json_escaped;  # add a new one}

Do not replace the existing access_log line; add one next to it. Several access_log directives on the same level all log, so collectors and dashboards reading the old log keep working while you validate the new JSON file. Remove the old line only after the new file is ingested correctly. Keep the .log extension: distribution packages usually rotate only /var/log/nginx/*.log, so a name like access.json is never rotated and keeps growing (check with cat /etc/logrotate.d/nginx). To roll back, delete the added line and repeat the test and reload in [2].

Wrap every string value in double quotes. $status is always a three-digit number, so it is safe unquoted, but variables such as $upstream_response_time can be - or 0.012, 0.034 and break the line if left unquoted (response time variables).

[2] Test the syntax and reload.

console
$ sudo nginx -t && sudo systemctl reload nginx

[3] Compare the three modes on real input. The format above was written to three files at once, changing only escape, and seven requests were sent over a raw socket: a plain request, a User-Agent with quotes and a backslash, a Korean path, a header containing a tab and 0x01, a Referer with quotes, invalid UTF-8 bytes \xff\xfe, and a path with é and ï. Each line was run through jq -e .:

console
$ for f in escaped default none; do  ok=0; n=0  while IFS= read -r line; do n=$((n+1)); printf '%s\n' "$line" | jq -e . >/dev/null 2>&1 && ok=$((ok+1)); done < $f.log  echo "$f.log: $ok/$n lines parsed by jq"doneescaped.log: 7/7 lines parsed by jqdefault.log: 1/7 lines parsed by jqnone.log: 4/7 lines parsed by jq
Requestescape=jsondefaultescape=none
Plain requestParsesParsesParses
UA with "quoted" \back\"quoted\" \\back, parses\x22 \x5C, failsRaw quotes, fails
Korean path, é/ï pathRaw UTF-8, parses\xEA\xB2\x80… \xC3\xA9…, failsRaw UTF-8, parses
Tab and 0x01\t \u0001, parses\x09 \x01, failsRaw, fails
Referer with quotesParsesFailsFails
Invalid UTF-8Parses (jq substitutes U+FFFD)FailsParses (jq substitutes U+FFFD)

The default mode, as documented, writes ", \, characters below 32 and above 126 as \xXX. That is safe for humans but nearly every unusual request fails as JSON. escape=none changes nothing, so one quote breaks the line:

console
$ sed -n 4p none.log | jq .jq: parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 1, column 143

[4] Read the escape=json values back with jq and confirm they round-trip.

console
$ sed -n 2p escaped.log | jq -r .user_agentMozilla/5.0 "quoted" \back
console
$ sed -n 7p escaped.log | jq -r .requestGET /café?q=naïve HTTP/1.1

Non-ASCII bytes are written as-is and read back as UTF-8. The default mode writes the same path as /caf\xC3\xA9?q=na\xC3\xAFve, which needs another decoding step even for humans, and jq rejects that line:

console
$ sed -n 7p default.log | jq -r .requestjq: parse error: Invalid escape at line 1, column 115

[5] Check invalid UTF-8 separately. escape=json escapes quotes, backslashes and characters below 32, and leaves bytes above 126 untouched. If a client sends bytes that are not UTF-8, the log line is not valid UTF-8 either. jq 1.7 replaced them with U+FFFD and accepted the line; a strict decoder rejects it.

console
$ sed -n 6p escaped.log | jq -r .request | od -c | head -10000000   G   E   T       / 357 277 275 357 277 275       H   T   T   P
console
$ python3 strict.py escaped.logescaped.log line 6: UnicodeDecodeError: invalid start byteescaped.log: 6/7 lines parsed by json.loads

strict.py is a ten-line test script that runs bytes.decode("utf-8") and then json.loads on each line. If your shipper still drops the odd line, suspect this case and look for a shipper setting that replaces invalid bytes. No nginx option to prevent it was found in the documentation.

[6] Note that empty values change shape. The default mode logs - for a missing value, while escape=json logged an empty string "" (the referer field in the test). If a dashboard filters on referer == "-", make it match "" as well.

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

Check it with the JSON · YAML formatter and converter tool ↗ — calculated in your browser.

#nginx#log-format#json#jq#escape