-
Notifications
You must be signed in to change notification settings - Fork 0
Monitoring Nginx
To monitor the Nginx server using Prometheus, there are two components:
-
Enable
stub_statusin the Nginx container. -
Run
nginx-prometheus-exporterin a separate container to gather Nginx data and expose these metrics in a format usable by Prometheus.
In the nginx.conf file, add a location (the path is arbitrary, typically /stub_status, /nginx_status, or /status):
http {
server {
listen 80;
...
# This location is scraped by Prometheus
location /nginx_status {
stub_status;
allow 127.0.0.1;
allow ::1; # IPv6 localhost
allow nginx-exporter; # metrics scraper and adapter
deny all;
}In allow I originally used a subnet mask of the Docker internal network (172.19.0.0/16)
but that is installation-dependent. Instead I use the container name (nginx-exporter)
which Docker's DNS resolves to the IP address on the bridge network.
Another solution is to use a "network alias" or "host alias" in the network definition (docker-compose).
Using a network alias of "nginx-metrics":
- In docker-compose.yml
networks: app-net: aliases: - nginx-metrics
- In nginx.conf
location /nginx_status { stub_status; allow metrics-collector; deny all; }
In docker-compose.yml
nginx-exporter:
# Export server metrics for monitoring via Prometheus.
image: nginx/nginx-prometheus-exporter
container_name: nginx_exporter
ports:
# no need to expose a port
# - "9113:9113"
command:
- '-nginx.scrape-uri=http://nginx:80/nginx_status'
depends_on:
- nginx
networks:
- app-netNote: to save time at start-up I run docker pull nginx/nginx-prometheus-exporter in advance.
Test that the name "nginx-exporter" is resolvable in the nginx container. On the host where you are running Docker:
# Exec into nginx container and test DNS resolution
docker exec -it nginx nslookup nginx-exporterTo verify that metrics scraping is working, temporarily expose port 9113 and browse to http://localhost:9113/metrics. Use curl or a web browser.