14.7. Nginx Prometheus Exporter

14.7.1. Download nginx-prometheus-exporter

  • Download

  • Extract

  • Copy to /usr/local/bin

  • Set ownership and permissions

# mkdir -p /tmp/nginx_exporter_tmp && cd /tmp/nginx_exporter_tmp
# wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v1.2.0/nginx-prometheus-exporter_1.2.0_linux_amd64.tar.gz
# tar axf nginx-prometheus-exporter_1.2.0_linux_amd64.tar.gz
# cd nginx-prometheus-exporter
# cp nginx-prometheus-exporter /usr/local/bin

14.7.2. Create a system user

# useradd --system --no-create-home --shell /bin/false nginx-exporter

14.7.3. Systemd configuration

14.7.3.1. Nginx-prometheus-exporter systemd service file

# Path:/etc/systemd/system/nginx-exporter.service

[Unit]
Description=NGINX Prometheus Exporter
Requires=nginx-exporter.socket

[Service]
User=nginx-exporter
ExecStart=/usr/local/bin/nginx-prometheus-exporter --nginx.scrape-uri="http://127.0.0.1/metrics" --web.systemd-socket

[Install]
WantedBy=multi-user.target

14.7.3.2. Nginx-prometheus-exporter systemd socket file

# Path: /etc/systemd/system/nginx-exporter.socket

[Unit]
Description=NGINX Prometheus Exporter

[Socket]
ListenStream=9113

[Install]
WantedBy=sockets.target

14.7.4. Modify the Prometheus configuration file

# Path: /etc/prometheus/prometheus.yml

# Job, for nginx_exporter
  - job_name: 'nginx_exporter'
    static_configs:
      - targets: ['localhost:9113'] # choose port number

14.7.5. Nginx configuration

server {
   listen localhost;
   root /var/www/html;
   index index.html;
   server_name _;

   location /metrics {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
      }

  }

## Create the Nginx symbolic link to enable the virtualhost file
# ln -s /etc/nginx/sites-available/nginx-exporter.conf /etc/nginx/sites-enabled/nginx-exporter.conf
# nginx -t
# systemctl reload nginx
## Check with curl
# curl http://localhost/metrics

14.7.6. Restart everything

# systemctl daemon-reload # Only necessary if modified
# systemctl enable --now node-exporter.service
# systemctl status node-exporter.service
# systemctl restart prometheus.service

14.7.7. Close the 9113 port with Nginx

  • Change the nginx-exporter.socket file

# Path: /etc/systemd/system/nginx-exporter.socket

[Unit]
Description=NGINX Prometheus Exporter

# Add here the loopback ip address
[Socket]
ListenStream=127.0.0.1:9113

[Install]
WantedBy=sockets.target

14.7.8. Add a new server block to Nginx

server {
 listen 80;
 listen [::]:80;

 server_name ip_address | subdomain.ltd;
     location /nginx-exporter {
       proxy_pass http://localhost:9113;
       }

}

14.7.9. Create a new dashboard in Grafana

You can now go to the Grafana dashboards and easily add the new dashboard for the Nginx Exporter program. Please make sure you choose the right Prometheus data source.