Learn Labs
Operations

Logging & Monitoring

JSON access logs and their timings, stub_status metrics, probes, and alerts

Open-source Nginx exposes very few metrics. Most of what you need to know about its traffic is in the access log, so the log format matters.

A JSON access log

log_format json escape=json '{''"time":"$time_iso8601",''"request_id":"$request_id",'also sent to the app and the client'"remote_addr":"$remote_addr",''"method":"$request_method","uri":"$request_uri","status":$status,''"request_time":$request_time,'whole request, as Nginx saw it'"upstream":"$upstream_addr",'which backend(s)'"upstream_status":"$upstream_status",''"upstream_connect_time":"$upstream_connect_time",''"upstream_header_time":"$upstream_header_time",'time to first byte from the backend'"upstream_response_time":"$upstream_response_time",''"cache":"$upstream_cache_status", ...''}';access_log /var/log/nginx/access.log json;
docker logs nginx | grep 'slow?ms=300' | tail -1
{"time": "2026-10-12T16:33:08+00:00", "request_id": "8f2112de86e994952130e2d02c48edf2",
"remote_addr": "172.30.0.1", "method": "GET", "uri": "/api/slow?ms=300", "status": 200,
"request_time": 0.305, "upstream": "172.30.0.4:8000", "upstream_status": "200",
"upstream_connect_time": "0.000", "upstream_header_time": "0.305",
"upstream_response_time": "0.305", "cache": "", "user_agent": "curl/8.7.1"}

JSON means any log pipeline (Loki, Elasticsearch, a cloud logging service) can parse fields without fragile regexes. And the timings answer the first question of every latency incident: is it us or the backend?

PatternMeans
request_time ≈ upstream_response_timeThe backend is slow
request_time ≫ upstream_response_timeThe client is slow (reading or sending)
High upstream_connect_timeBackend overloaded or unreachable
Several addresses in upstreamRetries happened (backend failures)
status 504, upstream_status "504, 504"Timed out on every attempt

The request_id is passed to the app as X-Request-ID and returned to the client, so one id finds the request in Nginx logs, app logs and a user's bug report.

The official image links access.log to stdout and error.log to stderr, so docker logs shows both, and the Docker logging driver handles rotation (max-size in compose).

stub_status and the exporter

curl -s localhost:9113/metrics | grep ^nginx_
nginx_connections_accepted 847
nginx_connections_active 1
nginx_connections_handled 847
nginx_connections_reading 0
nginx_connections_waiting 0
nginx_connections_writing 1
nginx_http_requests_total 4159
nginx_up 1

That's all stub_status offers: connection counts and a request total. No status codes, no latencies, no per-upstream data. NGINX Plus has an API with all of that; in open source, you derive it from logs (Loki queries, or mtail / a log exporter turning log lines into Prometheus counters), or measure from outside with probes.

MetricWatch for
nginx_up0: the exporter can't read stub_status
rate(nginx_http_requests_total)Traffic; a sudden drop is as alarming as a spike
nginx_connections_waitingIdle keepalive connections: normal, can be large
nginx_connections_writingRequests in progress, including waiting on backends: a rising line is a slow backend
accepted − handledConnections dropped at the limit (worker_connections, file descriptors)

Probes from outside

The Prometheus stack's Blackbox exporter requests /healthz over HTTP and /api/whoami over HTTPS, exactly as a client would, through TLS, routing and the upstream. Its metrics include success, status code, the duration of each phase (DNS, connect, TLS, processing) and the certificate's expiry.

Alerts

NginxDown nginx_up == 0 for 1m criticalTLSCertificateExpiringSoon (probe_ssl_earliest_cert_expiry - time())/86400 < 14 for 10mEndpointProbeFailed probe_success == 0 for 2m

The dashboard

Grafana provisions Nginx Overview from grafana/dashboards/Web/nginx-overview.json: status, requests per second, active connections, HTTPS probe result and days until the certificate expires; then requests over time, connections by state, accepted vs handled, and probe duration by phase.

Lab

  1. Run timeouts.py and find the 7-second request in the access log. Which fields show the retry?
  2. Run rate_limits.py with the dashboard open. Which panels move?
  3. Write a LogQL or jq query over docker logs nginx that computes the share of 5xx responses per minute.

On this page