0% found this document useful (0 votes)
125 views

Nginx Monitoring in Prometheus

This document discusses monitoring Nginx web servers using Prometheus and Grafana. It describes how to configure Nginx to expose metrics via a stub status page, run the Nginx Prometheus exporter container to scrape and reformat those metrics, integrate Prometheus to scrape the exporter, and import a Grafana dashboard to visualize the metrics. Key steps include enabling the stub_status module in Nginx, running the exporter container with the stub URL, adding a Prometheus job to scrape the exporter, and importing a pre-made Nginx dashboard into Grafana.

Uploaded by

Aymen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Nginx Monitoring in Prometheus

This document discusses monitoring Nginx web servers using Prometheus and Grafana. It describes how to configure Nginx to expose metrics via a stub status page, run the Nginx Prometheus exporter container to scrape and reformat those metrics, integrate Prometheus to scrape the exporter, and import a Grafana dashboard to visualize the metrics. Key steps include enabling the stub_status module in Nginx, running the exporter container with the stub URL, adding a Prometheus job to scrape the exporter, and importing a pre-made Nginx dashboard into Grafana.

Uploaded by

Aymen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Nginx Monitoring in Prometheus

Olivier Spiesser

Following my two previous blogs about monitoring, I will now focus on Nginx. Nginx Inc provides
an exporter with it’s code: https://github.com/nginxinc/nginx-prometheus-exporter

As Nginx cannot run a web application as WebLogic or WildFly can, exporter will be another
process.

Nginx Configuration

To enable metrics for Nginx, we must use stub_status directive in a server or location context
of the configuration.

Before that, we must check that stub module is enabled. A simple command will do:

1 nginx -V 2>&1 | grep --color "with-http_stub_status_module"

Then, we must extend the existing configuration to enable stub module by adding following lines
inside a server context:

1 location /nginx_status {
2 stub_status on;
allow 127.0.0.1;
3
deny all;
4
}
5

To validate configuration, we can run following command to reload configuration:

A curl command (curl https://localhost:443/nginx_status) will check that metrics are


populated:

1 Active connections: 1
2 server accepts handled requests
14 14 14
3
Reading: 0 Writing: 1 Waiting: 0
4

As the format is not Prometheus compliant, we need the exporter to do the reformatting for us.

Nginx Exporter

We could run exporter as a binary or as a container. I am choosing the second option.


1 docker run\
2 -p 9113:9113\
-e SSL_VERIFY=false\
3
nginx/nginx-prometheus-exporter\
4
-nginx.scrape-uri=https://192.168.33.10:443/nginx_status
5

Here is a quick explanation of the parameters:

Line 2 is the port exposed to get metrics.

Line 3 is to disable SSL verification as my certificate is not signed by a certification authority.

Line 4 is the image name.

Line 5 is the stub URL.

Output of docker command looks like this:

1 NGINX Prometheus Exporter version=0.11.0 commit=e4a6810d4f0b776f7fde37fea1d84e4c7284b72a


date=2022-09-07T21:09:51Z, dirty=false, arch=linux/amd64, go=go1.19
2
2023/03/27 10:41:11 Starting...
3 2023/03/27 10:41:11 Listening on :9113
4 2023/03/27 10:41:11 NGINX Prometheus Exporter has successfully started

Further, I can also test the URL where metrics are now properly formatted for Prometheus
ingestion (i.e. http://localhost:9113/metrics):
1 # HELP nginx_connections_accepted Accepted client connections

2 # TYPE nginx_connections_accepted counter


nginx_connections_accepted 27
3
# HELP nginx_connections_active Active client connections
4
# TYPE nginx_connections_active gauge
5
nginx_connections_active 2
6
# HELP nginx_connections_handled Handled client connections
7 # TYPE nginx_connections_handled counter
8 nginx_connections_handled 27
9 # HELP nginx_connections_reading Connections where NGINX is reading the request header

10 # TYPE nginx_connections_reading gauge


11 nginx_connections_reading 0
12 # HELP nginx_connections_waiting Idle client connections
# TYPE nginx_connections_waiting gauge
13
nginx_connections_waiting 1
14
...
15
16

Prometheus Integration

In scrape_configs section, I am adding the following job:

1 - job_name: "Nginx"
2 metrics_path: /metrics

3 static_configs:

4 - targets: ['172.17.0.1:9113']

After a reload of the configuration, I check if the Target State is up:

Prometheus Targets

Grafana

I will use the dashboard provided in the github repository (grafana/dashboard.json) and point it to
Prometheus datasource:

Grafana Dashboard Import

Result looks simple but nice:

Nginx Dashboard
Nginx Plus provides much more metrics as documented here.

Post Views: 1,760

We use cookies on our website to provide you with the most relevant experience by remembering
your preferences. No personal data is stored. By clicking on "Accept All", you consent to the use of
ALL cookies. However, you can visit "Cookie Settings" to provide controlled consent. Read More

You might also like