Nginx Log Formats and Privacy
Choose fields for a purpose
A log format is a template for an access record. Name it with log_format in the http block, then select that name using access_log. A useful small record contains the selected server, method, status, elapsed time and a correlation identifier. Avoid collecting every header merely because it is available.
log_format lab_json escape=json '{"id":"$request_id","site":"$server_name",' '"method":"$request_method","uri":"$uri",' '"status":"$status","seconds":"$request_time",' '"upstream":"$upstream_status"}';access_log logs/access.jsonl lab_json;Each completed request produces one JSON object per line. Quoting every variable keeps a missing upstream value or multi-attempt status list representable as a string. escape=json is supported from 1.11.8; it escapes characters inside variable values but does not repair malformed JSON surrounding those values. This is access logging; newer commercial JSON error logging is a separate capability, not a feature of all open-source builds.
{ "id": "0123456789abcdef0123456789abcdef", "site": "learn.test", "method": "GET", "uri": "/guide.html", "status": "200", "seconds": "0.012", "upstream": "-"}Here 0.012 seconds is 12 milliseconds. Request time includes reading the client request and sending the response, so it is not pure backend CPU time. The request ID is a generated hexadecimal value (available since 1.11.0); it is useful for correlation, not for authentication. To correlate with an application, arrange explicit propagation and logging at each trusted hop. Default error logs need not contain this ID.
Minimize sensitive data
$request_uri original URI including query$args query arguments$http_cookie incoming cookie header$http_authorization incoming authorization header$request_body request body when availableEven $uri can contain a private identifier or change after an internal redirect. For sensitive routes prefer a fixed route category and omit the path entirely. A JSON-escaped token is still a token; escaping is not redaction. Default combined and diagnostic logs may include request lines or other sensitive data. Audit all destinations, restrict file and directory permissions, limit retention, and sanitize excerpts before sharing. Do not send real credentials in a test.
log_format minimal escape=json '{"status":"$status","id":"$request_id"}';# Inside a reviewed server/location: access_log logs/minimal.jsonl minimal;This sacrifices route detail in exchange for less exposure. Use an approved retention policy rather than making logs world-readable. For a live system, choose the required evidence before omitting fields; privacy and incident response must both be considered.
Rotate and reopen safely
mv logs/access.jsonl logs/access.previous.jsonlnginx -p "$PWD/" -c nginx.conf -s reopenOnly run this on the chapter lab you started yourself, with a unique prefix and verified lab PID, and only if the destination does not exist. Renaming a file does not switch an open descriptor to a new file. Reopening directs subsequent writes to the configured path; confirm the new file exists and receives a test request before archiving the previous log. Production rotation should coordinate file ownership, reopen, retention and monitoring. Buffered access logging can delay visibility.
Practice
Design a status-and-latency-only record for a sensitive account route and explain which fields you omitted. Validate JSON shape offline with the supplied fixture checker.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Structured logging and privacy
Choose the best answer based on the lesson and synthetic evidence.
Where is log_format defined?
What does escape=json do?
What does request_time of 0.045 mean?
Which field should be omitted to avoid recording authorization tokens?
After renaming an active file, why reopen logs?