Nginx

Monitor Nginx and ingress-nginx on Linux servers, Docker and Kubernetes with WatchLog: status codes, latency, sites, URLs, upstreams and their servers, clients, cache and causes from the error log, turned into metrics on your machine. No log line leaves it.

Nginx monitoring: what WatchLog shows and explains →

WatchLog finds Nginx, OpenResty, Angie and ingress-nginx from the processes, images and ports its agents already see, on Linux servers, Docker and Kubernetes. Open Applications and press Monitor: Nginx needs no user, no password and no module.

How it works

Open-source Nginx tells almost nothing about its traffic: stub_status has connection and request totals only. Status codes, latency, sites, URLs and upstreams are in its access log; the causes (an upstream refusing, timing out, a rate limit, worker_connections reached, a failed reload) are in its error log.

So the WatchLog agent on the machine reads both logs and turns them into numbers there. Log lines never leave the machine and are never stored. What leaves it is counts, sums, latency histograms and short top lists. This is metrics from logs, not log collection.

Where Nginx runs What the agent reads
A Linux server (package or source) The log files its configuration names, and the configuration itself (read-only)
A Docker container on a server The container's stdout and stderr (Docker's json-file), or a log file inside the container; its configuration
Kubernetes (Nginx and ingress-nginx) The pod's stdout and stderr in /var/log/pods. The configuration is not read: it usually comes from a ConfigMap, which WatchLog never reads

The agent never writes Nginx's configuration, never reloads or signals Nginx and never runs nginx. It never opens a private key: of the TLS files it reads only the certificates named by ssl_certificate, for their expiry.

What never leaves the machine

  • The log lines. Each line is parsed, counted and dropped.
  • Query strings, cookies, headers, bodies. URLs are reduced to patterns inside the agent: the query string is cut first, and ids, UUIDs, hashes, tokens, emails and dates in the path become placeholders (/orders/{id}). A path position that keeps taking new values (user names, slugs) becomes {name}. If your format logs $http_cookie or a header, its value is skipped.
  • User agents. Each becomes one of six classes: browser, mobile app, crawler (named from a fixed list: Googlebot, Bingbot…, from the user agent only), script or library, monitoring, empty.
  • The error log's lines. A line becomes a cause (from a fixed list), its site, the upstream server's address and the rate-limit zone. Its request, query and client are dropped where they are read.

Client addresses

Addresses are personal data. WatchLog keeps the 50 busiest per site, for 24 hours (older hours are deleted every hour), so the Clients tab can name the address that sends 38 % of your traffic or the scanner that only gets 404s. In Settings → General → Client addresses (Nginx) an Owner or Admin chooses:

  • Keep the busiest addresses (default);
  • Shorten them: only their networks leave the agent, /24 for IPv4 and /48 for IPv6;
  • Don't keep any: no address leaves your servers; the Clients tab shows classes and totals only.

The agents pick up a change within a minute.

What it needs from you

Everything below works with Nginx's default combined format: requests, status codes, 499s, sites (when each server has its own access_log), URLs, methods, bytes out, clients, and the error log's causes.

Latency, upstream timings, upstream names and cache shares need variables in log_format. The Connection tab shows which are present, what each missing one costs ("no latency", "no cache numbers"), and this line to paste in the http block:

log_format watchlog '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
                    'rt=$request_time uct=$upstream_connect_time uht=$upstream_header_time '
                    'urt=$upstream_response_time ua="$upstream_addr" us="$upstream_status" '
                    'cs=$upstream_cache_status sn="$server_name" rl=$request_length '
                    'bs=$bytes_sent scheme=$scheme tls=$ssl_protocol uh="$proxy_host"';
access_log /var/log/nginx/access.log watchlog;

then reload Nginx yourself (sudo nginx -t && sudo systemctl reload nginx). It starts with combined, so other tools that read your log keep working. JSON formats work too (log_format … escape=json '{"status":$status,…}') when the keys are Nginx's variable names.

On Kubernetes, send the access log to stdout (access_log /dev/stdout watchlog;, the official image already does) and roll the Deployment out. ingress-nginx's own format is read as it is.

stub_status still adds connections (active, reading, writing, waiting, accepted against handled). Add this to the configuration and reload:

server {
    listen 8080;
    location = /stub_status {
        stub_status;
        allow 10.0.0.0/8;
        allow 172.16.0.0/12;
        allow 192.168.0.0/16;
        allow 127.0.0.1;
        deny all;
    }
}

WatchLog looks for /stub_status and /nginx_status on port 8080 and on the service's own port. That is the only request it makes to Nginx.

ingress-nginx's metrics (controller.metrics.enabled in its Helm chart) stay the source for requests, 5xx, latency, upstreams and certificates when they are on; the log adds URLs, clients and the error log's causes. When they're off the page gives the command.

When something can't be read

The page says exactly what is missing and the one step that fixes it:

  • The log file isn't readable. The server agent's unit has CAP_DAC_READ_SEARCH, so it reads /var/log/nginx/*.log (www-data:adm 0640) without a group change. A docker run install needs --cap-add DAC_READ_SEARCH (the install command has it). On Kubernetes the node agent reads pod logs as root while eBPF is on (the default).
  • access_log off. Sites, URLs, latency, clients and 499s are hidden; stub_status and the error log still work.
  • access_log syslog:…. WatchLog doesn't run a syslog listener. Add a file next to it; both can be on.
  • A format WatchLog doesn't know (Kubernetes). Use combined, JSON or the line above.
  • A log file inside a Kubernetes container (not stdout): the node agent can't reach it; use access_log /dev/stdout.

What you see

Tab What it shows
Overview The finding, cause first; the request flow (requests in → Nginx → upstreams → responses); six tiles against their usual; requests by status class; p50, p95, p99; health checks; upstreams
Sites Each server name: requests, 5xx, 4xx, 499, p95, bandwidth, cache hit share, certificate; requests and 5xx share by site; the busiest URLs
URLs Every URL pattern (top 100, the rest as "other"): requests, 5xx, 499, p95, time spent in the backend, upstream; the slowest, the largest, where clients give up
Upstreams Each upstream: its backend workload, requests, 5xx, p95, servers up, retries, last rollout
Errors 502, 504, 499, rate limited, 404 (and from scanners), dropped connections; errors by code; 499 against latency; why, from the error log; the URLs with the most errors
Latency The request time distribution; p50/p95/p99; where the time goes (connecting, waiting for the backend, sending); the slowest URLs and upstreams
Clients The busiest addresses with their class, errors and what they ask for; scanners and rate-limited addresses; who sends the requests (browsers, crawlers, apps, scripts)
Cache Only when there is one: hit share against the day before, by status, by site and URL pattern, cache zones
Connections and workers stub_status's connections; connections against worker_connections; open files against the limit (servers); workers, CPU, memory, reloads and failed reloads
TLS Protocol versions and ciphers in use, by site; certificates and their expiry (servers); SSL handshake failures (with error_log … info)
Connection What WatchLog reads (each log and its state, the format's variables) and the line that adds what's missing

Every tab filters by site: pick one in the chips above the panels. One site, one URL pattern, one upstream (its servers side by side, each with its requests, 5xx, response time and connect time, or what its errors say) and one client address have pages at their own address.

Each upstream server is named by the workload behind its address (a Kubernetes Deployment, a server's systemd service or process group, a container), the way the service map names connections.

What WatchLog tells you

Findings name the cause first, for example: "6 % of requests answer 5xx: 91 % of them come from upstream checkout-api, rolled out 2 minutes ago"; "502 because server 10.0.3.14:3000 of upstream cart refuses connections"; "504 on /reports/{id}: the backend takes over 60 s"; "clients give up (499) on /search: its p95 is 9 s"; "worker_connections reached: 140 connections dropped a minute"; "one address sends 38 % of all requests"; "the cache hit share fell from 82 % to 11 % after the reload"; "a reload failed: an emerg line, Nginx still runs the old configuration".

Default alerts

Created when you start monitoring; the drawer's ticks decide which start on.

  • More than 5 % of requests answer 5xx for 5 minutes (critical)
  • An upstream with no server answering (critical)
  • p95 over twice its usual for 10 minutes (warning)
  • Nginx drops connections (warning)
  • A reload or start fails (critical)
  • A certificate expires within 14 days (warning)
  • Rate-limit rejections above the usual for 10 minutes (warning)

A site, a URL pattern or an upstream gets its own alerts from its page.

What it costs

Measured with the whole agent (every collector, eBPF on) on one ARM core of a small VM, while a log it follows grew at a fixed rate:

Lines a second CPU, share of one core Memory (RSS)
0 1.8 % 29 MiB
1,000 2.2 % 31 MiB
5,000 3.9 % 31 MiB
20,000 8.0 % 26 MiB

The peak RSS in the whole run was 44 MiB. Following a file costs about 35 ns a line; parsing and counting a line with timings and three upstream tries about 3 µs. The Connection tab shows the reader's own time as a share of one core.

Above 20,000 lines a second per Nginx the agent counts one line in k with weight k, and the Connection tab says how much it sampled. Each Nginx sends at most about 1,500 series for its logs (20 sites, 100 URL patterns, 50 upstream servers, 50 addresses per site); a typical one a few hundred. --nginx-logs=false (WATCHLOG_NGINX_LOGS=false) turns log reading off.

Not available

  • The configuration on Kubernetes: sites from server blocks, upstream definitions, certificate files and worker_connections aren't read there (ConfigMap data). ingress-nginx keeps its own certificate metric.
  • Open files per worker on Kubernetes: another container's /proc/<pid>/fd needs a capability the node agent doesn't have.
  • Crawler verification: crawlers are named from the user agent; WatchLog doesn't look their addresses up.
  • The VTS module, the NGINX Plus API and Angie's API: detected and named on the Connection tab; their numbers aren't read, the log covers them.
  • access_log syslog: isn't read (see above).