How to Monitor Nginx with Prometheus [2 Steps]

In this article, we will explore How to Monitor Nginx with Prometheus. By integrating Prometheus with Nginx, administrators can gain valuable insights into their web server’s performance and health, enabling proactive management and optimization.

What is Prometheus?

How to add Prometheus Data Source in Grafana 2

Prometheus is an open-source monitoring and alerting toolkit designed for reliability and scalability in modern, dynamic environments. Developed by the Cloud Native Computing Foundation, Prometheus excels at collecting and storing time-series data, allowing users to gain valuable insights into the performance and health of their applications and infrastructure.

With its powerful query language and support for multi-dimensional data, Prometheus has become a popular choice for monitoring systems within cloud-native ecosystem

What is Nginx ?

How to Monitor Nginx with Prometheus [2 Steps] 1

Nginx is a high-performance, open-source web server and reverse proxy server. It is renowned for its efficiency in handling concurrent connections and serving static content quickly. Nginx is often used as a front-end web server to distribute incoming web traffic across multiple backend servers or to serve static content efficiently.

Example: Let’s say you have a website hosted on multiple backend servers running different applications. Nginx can act as a reverse proxy to receive requests from clients and distribute them to the appropriate backend server based on predefined rules, such as load balancing algorithms or URL patterns. This setup helps distribute incoming traffic evenly across backend servers, improving overall performance and reliability of the website.

What is Nginx-Prometheus-Exporter?

The nginx-prometheus-exporter is a tool designed to collect NGINX web server metrics and expose them in a format that Prometheus, a monitoring and alerting system, can scrape and analyze. It allows users to monitor NGINX performance metrics such as request rates, response times, and error rates in real-time.

Here’s a brief explanation of how nginx-prometheus-exporter works:

  1. Data Collection: The exporter gathers NGINX metrics by accessing the NGINX status page, which is enabled by configuring NGINX to expose status information.
  2. Exposition: It formats the collected metrics into a Prometheus-compatible format, usually in a text-based format called exposition format.
  3. Scraping: Prometheus scrapes the metrics exposed by the nginx-prometheus-exporter at regular intervals, typically via HTTP requests.
  4. Storage and Analysis: Prometheus stores the scraped metrics in its time-series database and allows users to query, visualize, and create alerts based on these metrics using PromQL (Prometheus Query Language).

Example:

Let’s say you have NGINX serving web traffic to your application. With nginx-prometheus-exporter, you can monitor various NGINX metrics such as:

  • Total number of requests
  • Request duration
  • HTTP status codes (2xx, 3xx, 4xx, 5xx)
  • Active connections
  • Response sizes

Prerequisites

To follow this tutorial, you will need:

How to Monitor Nginx with Prometheus

Step#1:Install Ngnix on Ubuntu Server

Ensure that NGINX is installed on your system by running the following commands:

sudo apt update 
sudo apt install nginx -y

Step#2:Configure Nginx Status Module

Edit the NGINX configuration to enable the status module by creating a configuration file:

sudo nano /etc/nginx/conf.d/status.conf

Optionally you can restrict this plugin to emit metrics to only the local host. It may be useful if you have a single Nginx instance and you install prometheus exporter on it as well. In case you have multiple Nginx servers, it’s better to deploy the prometheus exporter on a separate instance and scrape all of them from a single exporter.

Add the following configuration:

server {
    listen 80;
    # Optionally: allow access only from localhost
    # listen localhost:80;

    server_name _;

    location /status {
        stub_status;
    }
}
How to Monitor Nginx with Prometheus [2 Steps] 2

Save the file and exit the editor.

Comment out the default configuration:

sudo nano /etc/nginx/nginx.conf
How to Monitor Nginx with Prometheus [2 Steps] 3
How to Monitor Nginx with Prometheus [2 Steps] 4

Open a web browser and navigate to http://your_server_ip:80/status.

How to Monitor Nginx with Prometheus [2 Steps] 5
Active connections: 3 
server accepts handled requests
 5 5 42 
Reading: 0 Writing: 1 Waiting: 2

This output is from the Nginx stub_status module, which provides some basic statistics about Nginx’s current status. Let’s break down each part:

  • Active connections: This indicates the current number of active client connections to the Nginx server. In this case, there are 3 active connections.
  • server accepts handled requests: These numbers represent various counts related to client connections handled by the server:
    • 5 – The total number of accepted client connections.
    • 5 – The total number of handled connections. This is typically the same as the accepted connections unless connection processing is aborted.
    • 42 – The total number of requests handled by the server since it started.
  • Reading: This indicates the current number of connections where Nginx is reading the request header.
  • Writing: This indicates the current number of connections where Nginx is writing the response back to the client.
  • Waiting: This indicates the current number of idle client connections waiting for Nginx to process a request.

Step#3:Download and Install nginx-prometheus-exporter

Download the nginx-prometheus-exporter and install it using the following commands:

sudo wget https://github.com/nginxinc/nginx-prometheus-exporter/releases/download/v1.1.0/nginx-prometheus-exporter_1.1.0_linux_amd64.tar.gz
How to Monitor Nginx with Prometheus [2 Steps] 6

Extract the files:

sudo tar -xzvf nginx-prometheus-exporter_1.1.0_linux_amd64.tar.gz
How to Monitor Nginx with Prometheus [2 Steps] 7

Move the binary file of nginx exporter to /usr/local/bin location.

sudo mv nginx-prometheus-exporter /usr/local/bin/

Create a nginx exporter user to run the nginx exporter service.

sudo useradd -rs /bin/false nginx-prometheus-exporter

Step#4:Configure nginx-prometheus-exporter Service

Create a systemd service unit file for nginx-prometheus-exporter:

sudo nano /etc/systemd/system/nginx-prometheus-exporter.service

Add the following content to the file:

[Unit]
Description=nginx-prometheus-exporter
After=network.target
[Service]
User=nginx-prometheus-exporter
Group=nginx-prometheus-exporter
Type=simple
ExecStart=/usr/local/bin/nginx-prometheus-exporter -nginx.scrape-uri=http://localhost:80/status
[Install]
WantedBy=multi-user.target
How to Monitor Nginx with Prometheus [2 Steps] 8

Step#5:Enable and Start nginx-prometheus-exporter Service

Reload systemd to load the new service file:

sudo systemctl daemon-reload

Enable the nginx-prometheus-exporter service to start on boot:

sudo systemctl enable nginx-prometheus-exporter

Start the nginx-prometheus-exporter service:

sudo systemctl start nginx-prometheus-exporter

Check the status of the nginx-prometheus-exporter service:

sudo systemctl status nginx-prometheus-exporter
How to Monitor Nginx with Prometheus [2 Steps] 9

Step#6:Update Prometheus Configuration

Edit the Prometheus configuration file:

sudo nano /etc/prometheus/prometheus.yml

Add the following scrape configuration for nginx-prometheus-exporter:

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "nginx-prometheus-exporter"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9113"]
How to Monitor Nginx with Prometheus [2 Steps] 10

Restart Prometheus service to apply changes:

sudo systemctl restart prometheus

Check the status of Prometheus:

sudo systemctl status prometheus
How to Monitor Nginx with Prometheus [2 Steps] 11

Step#7:Configure Firewall

Enable the firewall:

sudo ufw enable
How to Monitor Nginx with Prometheus [2 Steps] 12

Allow traffic on ports 80, 9090, and 9113:

sudo ufw allow 80/tcp 
sudo ufw allow 9090/tcp 
sudo ufw allow 9113/tcp
sudo ufw status
How to Monitor Nginx with Prometheus [2 Steps] 13

Step#8:Verify the Host status on Prometheus server

How to Monitor Nginx with Prometheus [2 Steps] 14
How to Monitor Nginx with Prometheus [2 Steps] 15
How to Monitor Nginx with Prometheus [2 Steps] 16

Step#9:Examples to Monitor Nginx Metrics on Prometheus

What is nginx_connections_active?

  • Metric Description: nginx_connections_active is a metric provided by NGINX that indicates the current number of active client connections to the NGINX server. These connections represent clients currently communicating with the server, such as those requesting web pages, downloading files, or accessing APIs.
  • Usage: This metric is crucial for monitoring server health and workload. It provides insights into the server’s current capacity and helps administrators gauge the level of incoming traffic. Monitoring nginx_connections_active enables timely responses to changes in traffic patterns and ensures optimal server performance.
  • Example: Suppose you operate an e-commerce website hosted on an NGINX server. During peak shopping hours, the nginx_connections_active metric shows a significant increase as more users access the website to browse products and make purchases. By monitoring this metric, you can assess the server’s ability to handle incoming requests and scale resources as needed to maintain a seamless shopping experience for customers.
How to Monitor Nginx with Prometheus [2 Steps] 17
How to Monitor Nginx with Prometheus [2 Steps] 18

What is nginx_connections_accepted?

  • Metric Description: nginx_connections_accepted is a metric provided by NGINX that indicates the total number of accepted client connections since the NGINX server started. These connections represent successful connections established between clients and the server.
  • Usage: This metric provides insights into the overall connection activity of the NGINX server since its initialization. It helps administrators understand the historical pattern of client connections and assess server performance over time. Monitoring nginx_connections_accepted assists in capacity planning, resource allocation, and identifying long-term trends in server usage.
  • Example: Consider a content delivery network (CDN) using NGINX to serve multimedia content to users worldwide. By monitoring nginx_connections_accepted over an extended period, the CDN administrators can analyze trends in connection growth, identify peak usage periods, and plan infrastructure upgrades to accommodate increasing demand. This metric serves as a valuable tool for optimizing CDN performance and ensuring reliable content delivery to users across the globe.
How to Monitor Nginx with Prometheus [2 Steps] 19
How to Monitor Nginx with Prometheus [2 Steps] 20

Conclusion:

In this tutorial, you learned how to monitor Nginx with Prometheus on an Ubuntu Server. By collecting and analyzing metrics from Nginx, you can gain valuable insights into the performance and health of your web server. Monitoring Nginx with Prometheus helps you detect issues early and optimize your server for better performance and reliability.

Related Articles:

How to Install Prometheus on Ubuntu 22.04 LTS

Secure Grafana with Nginx, Reverse Proxy and Certbot

Reference:

Nginx Prometheus Exporter Download Page

Akash Bhujbal

Hey, I am Akash Bhujbal, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap