Structured request logging
Choose a small structured schema
A structured log records named fields, typically one JSON object per completed request. Start with status, elapsed time, an internal request identifier and a fixed route label. This makes aggregation easier without logging credentials or raw URLs. In the http context define a format, then choose its destination with access_log. JSON escaping protects the syntax of variable values; it does not redact sensitive data. Quote variable values because absent upstream data and multi-attempt values are not necessarily valid JSON numbers. The following partial configuration uses an isolated lab log path.
log_format observation escape=json '{"id":"$request_id","status":"$status",' '"duration":"$request_time","upstream":"$upstream_status",' '"upstream_s":"$upstream_response_time"}';access_log logs/access.jsonl observation;Read the sequence of upstream attempts
The final status is the response visible at the proxy, not a count of all backend outcomes. Upstream variables can contain lists separated by commas for several attempts and colons between upstream groups after internal redirects. Align values by attempt; preserve missing markers instead of converting them into zero. Connect time includes upstream TLS handshake where applicable, header time is time until the response header, and response time covers upstream response reception. Request time spans the client-facing processing interval; it is not a sum of independent backend CPU durations. In this synthetic retry, one backend failed but the final request succeeded.
{ "status": "200", "duration": "0.150", "upstream": "502, 200", "upstream_s": "0.010, 0.110"}Preserve failure visibility when sampling
Sampling trades detail for storage. Use a separate unsampled counter or retain all failure records for your defined SLI population. This partial http-context example keeps all 5xx responses and roughly one percent of other requests, keyed by the locally generated request ID. A sample is not a full denominator: estimate only with the known sampling policy and account for missing ingestion. Do not feed the resulting mixed sample directly into an error ratio.
split_clients "$request_id" $sampled { 1% 1; * 0;}map $status $record { ~^5 1; default $sampled;}access_log logs/sample.jsonl observation if=$record;Correlate and retain safely
At a direct trusted edge, overwrite an untrusted incoming request ID with one generated locally before forwarding. In a multi-proxy system define which trusted hop creates the identifier and how downstream services preserve it. Do not log authorization, cookies, raw bodies or unrestricted query strings. Error logs may still contain sensitive request details: restrict access and retention. Rotate by renaming the lab-owned file then asking only the verified lab master to reopen logs; verify new entries and that old files stop growing before archival. Rotation does not erase previously leaked data.
# Partial proxy location; a fixed local backend is requiredproxy_set_header X-Request-ID $request_id;proxy_pass http://127.0.0.1:19447;Practice and recap
Given final status 200, duration 0.150 and upstream status 502, 200, describe client outcome and attempts. Identify two fields to exclude from logs. Use the staged manual practice and quiz. Next: Metrics and health indicators.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Structured request logging
Design privacy-conscious structured Nginx access logs and interpret multiple upstream attempts without confusing them with client-visible errors.
Why quote upstream time values in a JSON log?
A request ends with 200 but upstream_status is 502, 200. What happened?
What is the effect of escape=json?
Can a mixed sample retaining every 5xx and 1% of other requests directly yield the real error ratio?
What is the appropriate edge correlation policy?