Beyond simply checking error logs, advanced monitoring involves tracking key performance indicators (KPIs) and metrics that provide deep insights into Nginx's behavior and your application's health. This proactive approach allows you to identify bottlenecks, anticipate issues, and ensure optimal performance.
Nginx exposes a wealth of information through its stub_status module. When enabled, this module provides a simple text-based overview of your server's current operational state. To enable it, you'll need to add the ngx_http_stub_status_module to your Nginx build (it's usually compiled by default) and then configure a location in your nginx.conf file.
http {
# ... other http configurations ...
server {
# ... other server configurations ...
location /nginx_status {
stub_status;
# Optional: restrict access for security
# allow 127.0.0.1;
# deny all;
}
}
}Once configured and reloaded, accessing /nginx_status in your browser or via curl will yield output similar to this:
Active connections: 1
server accepts handled requests
100 100 100
Reading: 0 Writing: 1% 0% Waiting: 1Let's break down these key metrics:
- Active connections: The current number of open connections being handled by Nginx. A consistently high number might indicate a need for more worker processes or that clients are not closing connections properly.
server accepts: The total number of accepted client connections since Nginx started. This is a cumulative count.
server handled: The total number of connections that have been handed over to Nginx's worker processes. In most cases, this should be equal toserver accepts.
server requests: The total number of client requests processed by Nginx. This is also a cumulative count.
- Reading: The number of connections where Nginx is currently reading the client request header.
- Writing: The number of connections where Nginx is currently writing the response to the client.
- Waiting: The number of idle keep-alive connections. These are connections that are open but not actively being used for a request. A high number of waiting connections is generally good, indicating efficient connection reuse.
While stub_status is excellent for a quick overview, for more robust and historical monitoring, you'll want to integrate Nginx with external monitoring tools. These tools can scrape the stub_status endpoint (or use Nginx's official metrics endpoint if available with specific modules) and store the data for analysis and alerting.
Popular choices include Prometheus with the nginx-exporter, Grafana for visualization, and Datadog. These platforms allow you to create dashboards that track these metrics over time, identify trends, and set up alerts for when certain thresholds are breached.
graph TD
Nginx[Nginx Server]
StubStatus(stub_status Module)
MetricsScraper(Prometheus Exporter)
MonitoringSystem(Monitoring Platform - e.g., Prometheus)
Visualization(Visualization - e.g., Grafana)
Alerting(Alerting System)
Nginx --> StubStatus
StubStatus --> MetricsScraper
MetricsScraper --> MonitoringSystem
MonitoringSystem --> Visualization
MonitoringSystem --> Alerting
Alerting -- Notify --> Admin(Administrator)
When setting up dashboards, consider tracking the following derived metrics for a comprehensive view:
- Requests per Second (RPS): Calculated by
server requests/ time interval. This is a fundamental measure of traffic volume.
- Connection Rate: The rate at which new connections are being accepted. High rates can indicate successful marketing campaigns or potential DoS attacks.
- Error Rate: While
stub_statusdoesn't directly provide error counts, you can derive this by comparing successful requests (2xx, 3xx) to error responses (4xx, 5xx) from your access logs. Many monitoring solutions can parse access logs for this.
- Response Time: Crucial for user experience. This is typically measured by your application or an external synthetic monitoring tool, but Nginx's ability to efficiently serve requests directly impacts it.
- Worker Connections: The number of connections handled by each worker process. If this becomes too high, it might suggest a need to increase
worker_processes.
By diligently monitoring these metrics, you transform Nginx from a simple web server into a highly observable component of your infrastructure, empowering you to maintain peak performance and reliability.