Setting Up a Prometheus Exporter for Nginx Metrics
📖 Scenario: You are managing a web server running Nginx. You want to monitor its performance using Prometheus, a popular monitoring tool. To do this, you need to set up the Prometheus exporter for Nginx, which exposes Nginx metrics in a format Prometheus can collect.
🎯 Goal: Learn how to configure Nginx to expose metrics for Prometheus by enabling the Prometheus exporter module and setting up the correct endpoint.
📋 What You'll Learn
Create a basic Nginx configuration file with a server block
Add a location block to expose metrics at /metrics
Configure the Prometheus exporter module settings
Verify the metrics endpoint is accessible and outputs metrics
💡 Why This Matters
🌍 Real World
Monitoring web servers is essential to keep websites fast and reliable. Prometheus collects metrics from Nginx to help detect issues early.
💼 Career
DevOps engineers often set up monitoring tools like Prometheus with exporters to maintain system health and performance.
Progress0 / 4 steps
1
Create basic Nginx server configuration
Create an Nginx configuration file with a server block listening on port 8080 and serving requests for localhost.
Nginx
Hint
Use server { ... } block with listen 8080; and server_name localhost;.
2
Add metrics location for Prometheus exporter
Inside the existing server block, add a location /metrics block that will serve the Prometheus metrics.
Nginx
Hint
Use location /metrics { stub_status; allow 127.0.0.1; deny all; } to expose metrics only locally.
3
Enable stub_status module for metrics
Ensure the stub_status directive is enabled inside the location /metrics block to expose Nginx status metrics.
Nginx
Hint
The stub_status; directive is required to expose metrics.
4
Test the metrics endpoint
Run the command curl http://127.0.0.1:8080/metrics to display the Nginx metrics exposed by the Prometheus exporter.
Nginx
Hint
The output should include lines like Active connections: showing Nginx status.
Practice
(1/5)
1. What is the main purpose of the stub_status module in nginx when used with Prometheus?
easy
A. To expose basic nginx metrics like active connections and requests
B. To serve static web pages faster
C. To block unauthorized IP addresses
D. To cache dynamic content for faster delivery
Solution
Step 1: Understand stub_status role
The stub_status module provides a simple status page with metrics like active connections, accepts, handled, and requests.
Step 2: Connect stub_status to Prometheus
Prometheus scrapes these metrics from the URL where stub_status is enabled to monitor nginx performance.
Final Answer:
To expose basic nginx metrics like active connections and requests -> Option A
Quick Check:
stub_status exposes metrics = C [OK]
Hint: stub_status shows nginx metrics, not content or blocking [OK]
Common Mistakes:
Thinking stub_status caches content
Confusing stub_status with IP blocking
Assuming stub_status serves web pages
2. Which nginx configuration snippet correctly enables stub_status at the URL /metrics and restricts access to 127.0.0.1?
easy
A. location /metrics { stub_status on; allow all; }
B. location /metrics { stub_status; }
C. location /metrics { stub_status; allow 127.0.0.1; deny all; }
D. location /metrics { stub_status on; }
Solution
Step 1: Check stub_status syntax
The correct directive is stub_status; without 'on'. It must be inside a location block.
Step 2: Verify access control
To protect metrics, use allow 127.0.0.1; and deny all; inside the location.