NGINX Monitoring with Prometheus

Enable the stub status module

http://nginx.org/en/docs/http/ngx_http_stub_status_module.html

Example Configuration below. This will create a HTTP server on port 81 only available from localhost. This goes inside the http {} section

server { 
		listen localhost:81;
		location /metrics {
			stub_status on;
		}
}

/etc/nginx/nginx.conf

Make sure to restart the NGINX service

service nginx restart

This is what the /metrics endpoint will look like

curl localhost:81/metrics
Active connections: 2 
server accepts handled requests
 13 13 25 
Reading: 0 Writing: 1 Waiting: 1

Install the NGINX Prometheus Exporter from GitHub

nginxinc/nginx-prometheus-exporter

cd /tmp
wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v0.7.0/nginx-prometheus-exporter-0.7.0-linux-amd64.tar.gz

tar -xf nginx-prometheus-exporter-0.7.0-linux-amd64.tar.gz
mv nginx-prometheus-exporter /usr/local/bin

useradd -r nginx_exporter

# Create Systemd Service File
nano /etc/systemd/system/nginx_prometheus_exporter.service
systemctl daemon-reload
service nginx_prometheus_exporter status
service nginx_prometheus_exporter start
[Unit]
Description=NGINX Prometheus Exporter
After=network.target

[Service]
Type=simple
User=nginx_exporter
Group=nginx_exporter
ExecStart=/usr/local/bin/nginx-prometheus-exporter \
    -web.listen-address=private_IP_here:9113 \
    -nginx.scrape-uri http://127.0.0.1:81/metrics

SyslogIdentifier=nginx_prometheus_exporter
Restart=always

[Install]
WantedBy=multi-user.target

Once the exporter has started you should be able to CURL the exporter and the output should look like this.

If you have ufw enabled you have to allow tcp port 9113

ufw allow proto tcp from any to any port 9113

Now just tell prometheus to scrape the exporter!

  - job_name: 'nginx'
    static_configs:
    - targets: ['0.0.0.0:9113']

Replace 0.0.0.0 with the IP of the web.listen-address

Get $200 in cloud credits from DigitalOcean using my link: https://m.do.co/t/909d52b6253a