Skip to main content
In this lesson we’ll cover practical techniques to monitor and troubleshoot NGINX. Topics include access and error logs, the lightweight stub_status endpoint, integrating with observability platforms, collecting and querying logs, and safe configuration reloads. These are the core practices for maintaining uptime, diagnosing issues, and feeding monitoring dashboards (Prometheus, Grafana, Datadog, etc.).

Logging (Access logs)

NGINX writes request details to access logs via its HTTP logging module. Access logs are essential for troubleshooting traffic patterns, identifying client errors (4xx), server errors (5xx), and measuring performance. Define a custom log format with log_format inside the http block. For example:
Enable access logging with the access_log directive (default locations are typically /var/log/nginx/access.log). Common built-in variables used in log formats:
A slide titled "Log Format Options" listing common web server log fields (e.g., remote_addr, remote_user, time_local, request, status, http_referer, $http_user_agent) with brief descriptions. A small © KodeKloud credit appears at the bottom.
Example: enable access logs in a server block and tail them in real time. Configuration file:
Tailing the access log:
Sample access log entry produced by the custom format above:
How to use access logs when troubleshooting:
  • Reproduce the issue while tailing logs (tail -f) to ensure requests reach this host.
  • If no entry appears, verify DNS, load balancer, or upstream/proxy routing.
  • Add fields (for example \$host, \$request_time, \$upstream_addr) to your log format to capture additional context for later analysis.
Reading a log entry (quick guide)
  • First token: remote IP (e.g., 192.168.1.1).
  • Next token: remote user (- when unauthenticated).
  • Timestamp: in square brackets.
  • Request line: double-quoted (method, URI, HTTP version).
  • Status code: e.g., 200, 404.
  • Bytes sent: numeric value.
  • Referer and User-Agent follow, possibly - if not present.

Error logs

NGINX error logs record problems NGINX encounters (configuration errors, file access errors, upstream timeouts). High volumes or repeated patterns indicate configuration or runtime issues. Look in /var/log/nginx/error.log (path may vary by distribution).

NGINX stub_status (basic runtime metrics)

The stub_status module exposes lightweight runtime metrics: active connections, accepts, handled, total requests, and the number of connections in reading/writing/waiting states. Keep this endpoint restricted and avoid logging access requests to it.
Do not expose stub_status publicly. Restrict access (e.g., allow 127.0.0.1; deny all;) and disable access logging for the endpoint to avoid polluting logs.
Example stub_status configuration:
Notes:
  • Using a nonstandard port like 81 reduces accidental exposure.
  • access_log off; prevents internal monitoring calls from generating log noise.
  • allow/deny restricts who can query the endpoint.
Query locally:
Sample output:
This shows active connections plus cumulative accepts/handled/requests and current connection states.

Monitoring platforms

Observability platforms can collect metrics and logs for richer dashboards and alerting. Common solutions:
  • Prometheus + Grafana (popular open-source stack)
  • Datadog (SaaS monitoring + logs)
  • New Relic, Dynatrace (APM + infrastructure)
  • ELK/EFK stacks for centralized logs
A slide titled "Monitoring Tools" showing a boxed area with logos for common observability platforms. Visible logos include Prometheus, Grafana, Datadog, Dynatrace, and New Relic.
Examples:
  • Prometheus uses an exporter (e.g., nginx-vts-exporter or the official Prometheus nginx exporter) to scrape metrics, and Grafana visualizes them.
  • Datadog installs an agent that collects system metrics (CPU, memory, disk, network) and NGINX-specific metrics (connections, requests, upstream stats).
A Datadog system metrics dashboard for an nginx host showing multiple widgets and time-series charts. It displays CPU usage, a processes memory treemap, load averages, disk latency, network traffic, and memory/disk breakdowns.
NGINX Plus (commercial) exposes additional metrics such as per-upstream and per-status code metrics, cache stats, and health check details which many agents can ingest out of the box.

Collecting and searching logs

Centralized logging (ELK/EFK, Splunk, Datadog Logs) makes it easy to query and correlate large volumes of logs with metrics and traces. Example application-level errors you might aggregate:
Correlate these with access logs and metrics to identify root causes (missing files, application routes failing, 502/504 upstream errors).

Configuration syntax validation and safe reloads

Validate configuration changes before applying them:
Successful test:
If the test fails, nginx -t shows the file and line number with an error message:
After a successful test, reload without restarting the master process to avoid dropping connections:
Always run nginx -t before reloading. Use nginx -s reload to apply changes without dropping active connections — restarting can cause a brief interruption.

Quick troubleshooting commands

Checking service status

Check the systemd service to verify NGINX is running:
Sample active output:
If the service is inactive (dead), try starting it:
If it fails to start, examine the journal and NGINX error logs. Common causes include configuration mistakes and port conflicts (e.g., Apache also listening on port 80).

Verifying NGINX is serving requests

Quick local check:
Example response:
A 200 OK from localhost indicates NGINX serves requests; if remote clients cannot connect, check firewall rules, load balancer, DNS, or network routing.

Firewall and network considerations

Ensure ports 80 (HTTP) and 443 (HTTPS) are open at both the cloud/provider level and the host firewall. Ubuntu (ufw):
RHEL / CentOS / Fedora (firewalld):
If either cloud or host firewall blocks these ports, external traffic will not reach your site.

Closing

This article covered NGINX access and error logging, the stub_status endpoint for lightweight runtime metrics, integrating with observability platforms (Prometheus/Grafana, Datadog, etc.), centralized log collection, and safe procedures for validating and reloading configuration. For production systems, centralize logs, export NGINX metrics to a monitoring system, and alert on key signals (status code spikes, latency increases, resource exhaustion) to respond proactively. Further reading and references:

Watch Video