Start your free 14-day ContainIQ trial

Using NGINX Prometheus Exporter | Tutorial

In this guide, we explain to readers how to setup and use the NGINX Prometheus exporter to monitor NGINX metrics.

December 4, 2022
Kumar Harsh
Software Developer

Monitoring is a crucial part of software development, as it helps you keep an eye on performance and resources. Without active monitoring, you’re in the dark, and your apps could go down at any time without your knowledge. This is where tools like Prometheus help. Prometheus is an open-source observability solution that tracks key metrics and gives you visibility into your apps.

However, Prometheus can not automatically connect with all types of apps and resources. In some cases, you need an adapter to translate the metrics that a resource generates into a format that Prometheus can easily understand. Prometheus exporters do just this.

This guide will show you how you can use the <terminal inline>prometheus-nginx-exporter<terminal inline> tool to connect an NGINX server to Prometheus.

What Is The NGINX Prometheus Exporter?

NGINX is one of the most popular web servers used today, and the NGINX Promtheus exporter is a tool you can use to export metrics from your NGINX server and capture them in your Prometheus instance. While NGINX exposes a basic set of metrics via its stub_status module, those metrics are not in a form that Prometheus can scrape. This is where <terminal inline>prometheus-nginx-exporter<terminal inline> helps. It listens to the stub_status endpoint and generates a feed of Prometheus-friendly metrics on an endpoint. You can then configure your Prometheus instance to scrape data from this endpoint and use it to track and analyze the performance of your NGINX server.

How to Use The NGINX Prometheus Exporter

To set up NGINX monitoring using Prometheus, you’ll need the following:

To start, update your NGINX server’s config file to enable the <terminal inline>/metrics<terminal inline> endpoint by adding the following to your <terminal inline>nginx.conf<terminal inline> file:


server { 
    location /metrics {
        stub_status on;
    }
}

Visit the <terminal inline>/metrics<terminal inline> route on your NGINX server to see whether metrics have been exposed. You should see a result like this:


Active connections: 1 
server accepts handled requests
 7 7 14 
Reading: 0 Writing: 1 Waiting: 0

Next, install the exporter in a Docker container by running the following command:


docker run \
  -p 9113:9113 \
  nginx/nginx-prometheus-exporter:0.10.0 \
  -nginx.scrape-uri=http://<nginx ip>:8080/metrics \
  -web.telemetry-path=/metrics

Once the exporter is up, you’ll be able to access the following page: <terminal inline>http:<your machine ip>:9113/metrics<terminal inline>, and see a page with detailed metrics from your NGINX server.


# HELP nginx_connections_accepted Accepted client connections
# TYPE nginx_connections_accepted counter
nginx_connections_accepted 10
# HELP nginx_connections_active Active client connections
# TYPE nginx_connections_active gauge
nginx_connections_active 1
# HELP nginx_connections_handled Handled client connections
# TYPE nginx_connections_handled counter
nginx_connections_handled 10
# HELP nginx_connections_reading Connections where NGINX is reading the request header
# TYPE nginx_connections_reading gauge
nginx_connections_reading 0
# HELP nginx_connections_waiting Idle client connections
# TYPE nginx_connections_waiting gauge
nginx_connections_waiting 0
# HELP nginx_connections_writing Connections where NGINX is writing the response back to the client
# TYPE nginx_connections_writing gauge
nginx_connections_writing 1
# HELP nginx_http_requests_total Total http requests
# TYPE nginx_http_requests_total counter
nginx_http_requests_total 21
# HELP nginx_up Status of the last metric scrape
# TYPE nginx_up gauge
nginx_up 1
# HELP nginxexporter_build_info Exporter build information
# TYPE nginxexporter_build_info gauge
nginxexporter_build_info{commit="7a03d0314425793cf4001f0d9b0b2cfd19563433",date="2021-12-21T19:24:34Z",version="0.10.0"} 1

Next, set up your Prometheus instance to scrape data from the exporter. You can do this by updating your <terminal inline>prometheus.yaml<terminal inline> file to include the following:


scrape_configs:
  - job_name: ‘nginx exporter’
    scrape_interval: 5s
    static_configs:
      - targets: ['<your machine ip>:9113']

Once everything is set up correctly, you’ll be able to see the NGINX-related metrics in your PromQL autofill suggestions:

PromQL autofill suggestions
PromQL autofill suggestions

You can now use these metrics in PromQL queries to monitor your NGINX server and draw visualizations using other tools, such as Grafana.

Final Thoughts

Prometheus is an all-around, open-source monitoring solution. However, it needs some extra configuration to work with external tools and services. In this guide, you saw how you can export metrics from your NGINX server and capture them in Prometheus using NGINX Prometheus exporter.

Start your free 14-day ContainIQ trial
Start Free TrialBook a Demo
No card required
Kumar Harsh
Software Developer

Kumar is a software developer and technical author. He has written for a number of software companies including LogRocket and Career Karma. Kumar previously worked for Goldman Sachs as a Summer Analyst. He is currently pursuing a Bachelor of Technology in Computer Science at the National Institute of Technology, Patna.

READ MORE