Skip to content

Monitoring Nginx

James Brucker edited this page Nov 27, 2025 · 1 revision

To monitor the Nginx server using Prometheus, there are two components:

  1. Enable stub_status in the Nginx container.

  2. Run nginx-prometheus-exporter in a separate container to gather Nginx data and expose these metrics in a format usable by Prometheus.

Enable stub_status in Nginx

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;
    }

Create Container for nginx-prometheus-exporter

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-net

Note: to save time at start-up I run docker pull nginx/nginx-prometheus-exporter in advance.

Verification

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-exporter

To verify that metrics scraping is working, temporarily expose port 9113 and browse to http://localhost:9113/metrics. Use curl or a web browser.

Clone this wiki locally