Prometheus is a time-series metrics collection and alerting system widely used for monitoring cloud infrastructure and web servers. The nginx-prometheus-exporter (nginx-exporter) is a lightweight Go binary that reads Nginx’s built-in stub_status page and exposes the metrics in Prometheus format — active connections, total requests, connection states (reading, writing, waiting), and request rate. Combined with Grafana dashboards and Prometheus alerting rules, this gives you real-time visibility into Nginx performance, the ability to alert on abnormal connection counts, and long-term trend analysis for capacity planning. This guide covers enabling the Nginx stub_status endpoint, installing and configuring nginx-prometheus-exporter on RHEL 9, adding a Prometheus scrape job, and viewing metrics.
Prerequisites
- Nginx installed on RHEL 9
- Prometheus installed (or a remote Prometheus instance)
Step 1 — Enable Nginx stub_status
# Add to /etc/nginx/conf.d/stub_status.conf
server {
listen 127.0.0.1:8080; # Only accessible locally
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}
}
nginx -t && systemctl reload nginx
curl http://127.0.0.1:8080/nginx_status
Expected output:
Active connections: 3
server accepts handled requests
12 12 48
Reading: 0 Writing: 1 Waiting: 2
Step 2 — Install nginx-prometheus-exporter
# Download the latest release
EXPORTER_VERSION=1.3.0
curl -LO https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v${EXPORTER_VERSION}/nginx-prometheus-exporter_${EXPORTER_VERSION}_linux_amd64.tar.gz
tar xzf nginx-prometheus-exporter_${EXPORTER_VERSION}_linux_amd64.tar.gz
install -m 755 nginx-prometheus-exporter /usr/local/bin/
Step 3 — Create a systemd Service
# /etc/systemd/system/nginx-exporter.service
[Unit]
Description=Nginx Prometheus Exporter
After=network.target
[Service]
Type=simple
User=nobody
Group=nobody
ExecStart=/usr/local/bin/nginx-prometheus-exporter
--nginx.scrape-uri=http://127.0.0.1:8080/nginx_status
--web.listen-address=:9113
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now nginx-exporter
systemctl status nginx-exporter
# Verify metrics endpoint
curl http://localhost:9113/metrics | grep nginx_
Step 4 — Configure Prometheus to Scrape the Exporter
# Add to /etc/prometheus/prometheus.yml under scrape_configs:
scrape_configs:
- job_name: 'nginx'
static_configs:
- targets: ['localhost:9113']
labels:
instance: 'web01'
# Reload Prometheus
systemctl reload prometheus
# Or via HTTP API:
curl -X POST http://localhost:9090/-/reload
Step 5 — Key Metrics to Monitor
# Active connections
nginx_connections_active
# Requests per second (rate over last 5 minutes)
rate(nginx_http_requests_total[5m])
# Connection states
nginx_connections_reading
nginx_connections_writing
nginx_connections_waiting
# Total handled connections
nginx_connections_handled_total
Step 6 — Prometheus Alerting Rule Example
# /etc/prometheus/rules/nginx.yml
groups:
- name: nginx
rules:
- alert: NginxHighActiveConnections
expr: nginx_connections_active > 1000
for: 2m
labels:
severity: warning
annotations:
summary: "Nginx active connections too high on {{ $labels.instance }}"
description: "Active connections: {{ $value }}"
- alert: NginxDown
expr: up{job="nginx"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Nginx exporter down on {{ $labels.instance }}"
Conclusion
The nginx-prometheus-exporter on RHEL 9 provides a simple bridge between Nginx’s built-in stub_status page and the Prometheus monitoring ecosystem. With active connections, request rates, and connection state metrics exposed, you can build Grafana dashboards for real-time performance visualisation and configure alerts that fire when connection counts spike or the Nginx process goes down. This completes the core Nginx observability stack.
Next steps: How to Harden Nginx: Security Headers and TLS 1.3 on RHEL 9, How to Configure Nginx Rate Limiting on RHEL 9, and How to Install Prometheus and Grafana on RHEL 9.