Nginx service discovery to use with Prometheus and the Blackbox Exporter
Go to file
echo 2e20b5a350 fix dependency versions 2023-04-23 18:13:47 +02:00
nginx_site_discovery fix when ip is not present 2023-03-26 19:13:28 +02:00
.gitignore first commit 2020-06-18 18:56:01 -04:00
LICENSE add basic readme 2020-11-21 13:01:02 -05:00 update readme 2021-10-13 23:13:29 +02:00 fix dependency versions 2023-04-23 18:13:47 +02:00

Nginx site discovery

This repo provides a tool to use as both a service discovery sidecar for Prometheus and a discovery agent. The tool watches Nginx configuration file for changes and either write the service discovery file to the provided path or send it to a server for centralisation.


  • To disable a single website from being scraped append # monitoring:ignore after the listen directive.
    listen; # monitoring:ignore
  • To configure the healthcheck URL, that is used for example with the Blackbox exporter
    server_name; # monitoring:healthcheck_url=/health

Command line usage

usage: nginx-sites-discovery [-h] [-l [LABELS [LABELS ...]]] [-s [LISTEN]] [-o OUTPUT_FILE] [-d OUTPUT_DIR] [-u SERVER_URL] [-e SITES_ENABLED] [-n] [-x EXTENSION]

Watch Nginx configuration to create files for service discovery

optional arguments:
  -h, --help            show this help message and exit
  -l [LABELS [LABELS ...]], --labels [LABELS [LABELS ...]]
                        Optional, extra labels to add to the jobs in the format key=value
  -s [LISTEN], --listen [LISTEN]
                        Optional, start a web server to listen for discovery files, if no listening address is provided the default is
  -o OUTPUT_FILE, --output-file OUTPUT_FILE
                        Optional, output the discovery file to the provided path
  -d OUTPUT_DIR, --output-dir OUTPUT_DIR
                        Only valid with --listen, the directory in wich the files are dumped, default to current working directory
  -u SERVER_URL, --server-url SERVER_URL
                        Optional, post the discovery file to the provided url, should be https://<server>/<service name>/<key>.
  -e SITES_ENABLED, --sites-enabled SITES_ENABLED
                        Path to the Nginx configuration file directory, default to /etc/nginx/sites-enabled
  -n, --no-watch        Disable watching, useful for the server only mode
  -x EXTENSION, --extension EXTENSION
                        Configuration file extension, default to empty string

Prometheus config

  - job_name: 'blackbox-http'
    metrics_path: /probe
      module: [http_ok]
      - files:
        - '/var/lib/prometheus/discovery/nginx-*.json'

      - source_labels: [__address__, healthcheck_url]
        separator: ''
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__