Skip to content

How-to guide

Nginx monitoring: status, logs and alerts

Nginx sits in front of almost everything. Its status page and access logs tell you how much traffic arrives, how much fails and how long your upstreams take.

4 min read13 sectionsUpdated

On this page

Key points

  • stub_status gives connections and request counts in one small page.
  • Dropped connections (accepts minus handled) mean Nginx hit a limit.
  • Log upstream_response_time to see backend latency per request.
  • Alert on the 5xx share, not on raw counts.

Enable stubstatus

status.confplaintext
server {
    listen 127.0.0.1:8081;
    location = /nginx_status {
        stub_status;
        allow 127.0.0.1;
        deny all;
    }
}
plaintext
Active connections: 291
server accepts handled requests
 16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106

Reading the numbers

FieldMeaningWatch for
Active connectionsOpen client connectionsSudden jumps (traffic or slow upstreams)
accepts vs handledAccepted vs handled connectionshandled < accepts means dropped (worker_connections limit)
requestsTotal requestsRate over time = requests per second
Reading / Writing / WaitingStates of current connectionsHigh Writing = slow responses or slow clients

Structured access logs with upstream timing

nginx.confplaintext
log_format json_combined escape=json '{'
  '"time":"$time_iso8601","remote":"$remote_addr","method":"$request_method",'
  '"uri":"$uri","status":$status,"bytes":$body_bytes_sent,'
  '"request_time":$request_time,"upstream_time":"$upstream_response_time",'
  '"upstream":"$upstream_addr","ua":"$http_user_agent"}';
access_log /var/log/nginx/access.json json_combined;

With JSON logs you can chart the 5xx share per route, upstream time percentiles, and which upstream instance is slow, and search individual requests during an incident. Ship them with your log aggregation pipeline.

Alerts worth setting

  • 5xx share above 1 to 2% for 5 minutes.
  • Dropped connections greater than zero.
  • Upstream p95 time above your target.
  • Nginx unreachable from outside: an uptime check on the public URL.
  • Error log spikes of upstream timed out or connect() failed, which explain 502 and 504 errors.

In VitalRix

A ready-made Nginx check reads the stub_status page on a schedule and alerts on dropped or saturated connections. Uptime monitoring checks the public URLs, and logs take the JSON access log for search and live tail.

Limits that cause dropped connections

SettingWhat it limitsSymptom when too low
worker_processesNumber of worker processesCPU-bound workers; set to auto
worker_connectionsConnections per workeraccepts > handled in stub_status
worker_rlimit_nofileOpen files per worker"too many open files" in the error log
keepalive_timeoutIdle client connectionsMany Waiting connections holding slots
proxy_read_timeoutTime waiting for the upstream504 errors

Useful log queries

With JSON access logs in a log search tool, these answer most questions during an incident: 5xx responses by route in the last 15 minutes; the slowest requests by upstream_time; requests per upstream address (is one backend misbehaving?); and the top client IPs and user agents (is this a bot or a retry storm?).

The NGINX metrics that matter

stub_status gives connection counts; the access log, with the right fields, gives everything else.

MetricSourceStarting alert
Active connectionsstub_statusNear worker_processes × worker_connections
accepts vs handledstub_statusAny difference: connections were dropped at a limit
5xx rateAccess log statusAbove 1% for 5 minutes
Request time and upstream time$request_time, $upstream_response_timep95 above your budget
Upstream errorsError log: "upstream timed out", "connect() failed"Any burst
plaintext
# a small status endpoint, local only
location = /nginx_status { stub_status; allow 127.0.0.1; deny all; }

# log how long each request and its upstream took
log_format timed '$remote_addr "$request" $status $body_bytes_sent '
                 'rt=$request_time urt=$upstream_response_time ua="$http_user_agent"';
access_log /var/log/nginx/access.log timed;

What the incidents look like

  • 502 Bad Gateway when the upstream (PHP-FPM, Node.js, a container) is down or crashes mid-response: see 502 Bad Gateway.
  • 504 Gateway Timeout when the upstream is alive but slower than proxy_read_timeout: see 504 Gateway Timeout.
  • Connections hit worker_connections under a traffic spike or many slow clients; accepts and handled diverge. Raise the limit together with the file descriptor limit.
  • A reload with a broken config after an edit. Always run nginx -t first, and keep an uptime check on the public URL so a bad reload is caught in a minute.

Frequently asked questions

Is stub_status safe to expose?

Keep it on localhost or an internal port with allow and deny rules. It reveals traffic levels, which you may not want public.

What does upstream_response_time "-" mean?

Nginx did not contact an upstream for that request, such as a static file or a cached response.

  • MySQL monitoring: key metrics and alerts: Monitor MySQL: connections and threads, query throughput, slow queries, InnoDB buffer pool, replication lag and disk, with the SQL for each check.
  • The three pillars of observability: Logs, metrics and traces compared: what each pillar of observability is good at, what it costs, and how to link them so one alert leads to the cause.
  • PostgreSQL monitoring: key metrics and alerts: Monitor PostgreSQL: connections, slow queries, locks, replication lag, cache hit ratio, bloat and disk, with the SQL for each and the alerts to set.
  • Kubernetes observability: Kubernetes observability for application teams: pod resources against limits, restarts and OOM kills, and traces with Kubernetes metadata.
  • 403 Forbidden: what it means and how to fix it: Fix 403 Forbidden errors: file permissions, missing index files, WAF and bot rules, IP allowlists, expired signed URLs and missing API scopes, step by step.
  • How to set a log retention policy: Decide how long to keep logs, traces and metrics: debugging needs, security and compliance, cost, tiered retention and a sample policy to adapt.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.