Access Log Formats
The proxy's access log is the single best record of what your users experienced: every request, its status, how long it took, which backend answered and why a request failed. The default formats are made for humans reading one line at a time; in production you want structured logs (JSON) with timing and upstream fields, shipped to a log system such as Loki or Elasticsearch (Observability). This lesson configures that in all four servers and reads the results with jq.
Nginx: a JSON format with upstream timing
nginx
log_format json_combined escape=json '{'
'"time":"$time_iso8601","request_id":"$request_id",'
'"remote_addr":"$remote_addr","method":"$request_method","uri":"$request_uri",'
'"status":$status,"bytes":$body_bytes_sent,'
'"request_time":$request_time,'
'"upstream":"$upstream_addr","upstream_status":"$upstream_status",'
'"upstream_connect_time":"$upstream_connect_time","upstream_response_time":"$upstream_response_time",'
'"cache":"$upstream_cache_status","user_agent":"$http_user_agent"}';
access_log /var/log/nginx/access.json json_combined buffer=64k flush=5s;| Variable | Meaning |
|---|---|
$request_time |
seconds from the first byte read from the client to the last byte sent (includes slow clients) |
$upstream_response_time |
time the backend took; several values (0.002, 1.001) when Nginx retried on another server |
$upstream_connect_time |
TCP (+TLS) connect time to the backend: high values mean an overloaded backend or network |
$upstream_addr / $upstream_status |
which backend(s) answered, and with what; also comma-separated after retries |
$request_id |
a random 32-hex id per request (since 1.11.0); send it to the app with proxy_set_header X-Request-ID $request_id; to correlate logs |
escape=json (since 1.11.8) makes Nginx escape quotes and control characters properly. The upstream variables are strings (they can be - or lists), so quote them; $status and $request_time are always numbers.
bash · try it yourself
cat > app.py <<'EOF'
import sys, time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
class H(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/slow": time.sleep(0.3)
self.send_response(500 if sys.argv[2] == "bad" else 200); self.end_headers(); self.wfile.write(b"ok\n")
def log_message(self, *a): pass
ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1])), H).serve_forever()
EOF
python3 app.py 9001 bad & python3 app.py 9002 good &
for p in 9001 9002; do for i in $(seq 50); do curl -s 127.0.0.1:$p/ > /dev/null && break; sleep 0.1; done; done
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http {
log_format json_combined escape=json '{'
'"request_id":"$request_id","method":"$request_method","uri":"$request_uri","status":$status,'
'"request_time":$request_time,"upstream":"$upstream_addr","upstream_status":"$upstream_status",'
'"upstream_response_time":"$upstream_response_time","user_agent":"$http_user_agent"}';
access_log access.json json_combined;
upstream app { server 127.0.0.1:9001 max_fails=0; server 127.0.0.1:9002 max_fails=0; }
server {
listen 127.0.0.1:8080;
location / {
proxy_pass http://app;
proxy_next_upstream error timeout http_500;
proxy_set_header X-Request-ID $request_id;
}
}
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
curl -s -o /dev/null 127.0.0.1:8080/fast
curl -s -o /dev/null 127.0.0.1:8080/slow
curl -s -o /dev/null -A 'evil "quoted" agent' 127.0.0.1:8080/x
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null; sleep 0.3
jq -c '{uri, status, upstream, upstream_status, user_agent}' access.json
echo "--- every line is valid JSON: $(jq -s length access.json) records"
echo "--- request ids are 32 hex chars: $(jq -r .request_id access.json | grep -cE '^[0-9a-f]{32}