Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

prometheus-nginxlog-exporter

> DevOps
Open source

Export metrics from Nginx access log files to Prometheus

1.1K stars0 likes0 views
WebsiteGitHub

About

Export metrics from Nginx access log files to Prometheus

= NGINX-to-Prometheus log file exporter :tip-caption: :bulb: :note-caption: :information_source: :important-caption: :heavy_exclamation_mark: :caution-caption: :fire: :warning-caption: :warning: :toc: :toc-placement!: :toc-title:

image:https://img.shields.io/github/workflow/status/martin-helmich/prometheus-nginxlog-exporter/Compile%20&%20Test[GitHub Workflow Status] image:https://quay.io/repository/martinhelmich/prometheus-nginxlog-exporter/status[link="https://quay.io/repository/martinhelmich/prometheus-nginxlog-exporter",Docker Repository on Quay] image:https://goreportcard.com/badge/github.com/martin-helmich/prometheus-nginxlog-exporter[link="https://goreportcard.com/report/github.com/martin-helmich/prometheus-nginxlog-exporter", Go Report Card] image:https://img.shields.io/github/license/martin-helmich/prometheus-nginxlog-exporter[GitHub] image:https://img.shields.io/badge/donate-PayPal-yellow[link="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=SEARYHPVS9U5N&source=url", Donate]

Helper tool that continuously reads an NGINX log file (or any kind of similar log file) and exports metrics to https://prometheus.io/[Prometheus].

[discrete] == Contents

toc::[]

== Usage

You can either use a simple configuration, using command-line flags, or create a configuration file with a more advanced configuration.

Use the command-line:

[source]

$ ./prometheus-nginxlog-exporter
-format=""
-listen-port=4040
-namespace=nginx
[PATHS-TO-LOGFILES...]

Use the configuration file:

[source]

$ ./prometheus-nginxlog-exporter -config-file /path/to/config.hcl

You can verify your config file before deployment, which will exit with shell status indicating success:

[source]

$ ./prometheus-nginxlog-exporter -config-file /path/to/config.hcl -verify-config

Installation

There are multiple ways to install this exporter.

=== Docker

Docker images for this exporter are available at the quay.io and pkg.github.com registries:

  • quay.io/martinhelmich/prometheus-nginxlog-exporter:v1
  • ghcr.io/martin-helmich/prometheus-nginxlog-exporter/exporter:v1

Have a look at the https://github.com/martin-helmich/prometheus-nginxlog-exporter/releases[releases page] to see the available versions and how to pull their images. In general, I would recommend using the v1 tag instead of latest.

Run the exporter as follows (adjust paths like /path/to/logs and /path/to/config to your own needs):

[source]

$ docker run
--name nginx-exporter
-v /path/to/logs:/mnt/nginxlogs
-p 4040:4040
quay.io/martinhelmich/prometheus-nginxlog-exporter
mnt/nginxlogs/access.log


Command-line flags and arguments can simply be appended to the docker run command, for example to use a configuration file:

[source]

$ docker run
--name nginx-exporter
-p 4040:4040
-v /path/to/logs:/mnt/nginxlogs
-v /path/to/config.hcl:/etc/prometheus-nginxlog-exporter.hcl
quay.io/martinhelmich/prometheus-nginxlog-exporter
-config-file /etc/prometheus-nginxlog-exporter.hcl


=== DEB and RPM packages

Each https://github.com/martin-helmich/prometheus-nginxlog-exporter/releases[release] from 1.5.1 or newer provides both DEB and RPM packages.

DEB:

$ wget https://github.com/martin-helmich/prometheus-nginxlog-exporter/releases/download/v1.9.2/prometheus-nginxlog-exporter_1.9.2_linux_amd64.deb
$ apt install ./prometheus-nginxlog-exporter_1.9.2_linux_amd64.deb

RPM:

$ wget https://github.com/martin-helmich/prometheus-nginxlog-exporter/releases/download/v1.9.2/prometheus-nginxlog-exporter_1.9.2_linux_amd64.rpm
$ yum localinstall prometheus-nginxlog-exporter_1.9.2_linux_amd64.rpm

The package come with a dependency on systemd and configure the exporter to be running automatically:

$ systemctl status prometheus-nginxlog-exporter
$ # systemctl disable prometheus-nginxlog-exporter
$ # systemctl enable prometheus-nginxlog-exporter

The packages drop a configuration file to /etc/prometheus-nginxlog-exporter.hcl which you can adjust to your own needs.

Manual installation, with systemd

If you do not want to use one of the pre-built packages, you can download the binary itself and manually configure systemd to start it. You can find an example unit file for this service https://github.com/martin-helmich/prometheus-nginxlog-exporter/blob/master/res/package/prometheus-nginxlog-exporter.service[in this repository]. Simply copy the unit file to /etc/systemd/system:

$ wget -O /etc/systemd/system/prometheus-nginxlog-exporter.service https://raw.githubusercontent.com/martin-helmich/prometheus-nginxlog-exporter/master/res/package/prometheus-nginxlog-exporter.service
$ systemctl enable prometheus-nginxlog-exporter
$ systemctl start prometheus-nginxlog-exporter

The shipped unit file expects the binary to be located in /usr/sbin/prometheus-nginxlog-exporter (if you sideload the exporter without using your package manager, you might want to put it to /usr/local, instead) and the configuration file in /etc/prometheus-nginxlog-exporter.hcl. Adjust to your own needs.

Kubernetes

If you run a logfile-generating service (be it NGINX, or anything that generates similar access log files) in Kubernetes, you can run the exporter as a sidecar along your "main" container within the same pod.

The following example shows you how to deploy the exporter as a sidecar, accepting logs from the main container via syslog:

[source,yaml]

apiVersion: v1 kind: Pod metadata: name: nginx-example annotations: prometheus.io/scrape: "true" prometheus.io/port: "4040" spec: containers: - name: web image: nginx # ... - name: exporter image: docker.pkg.github.com/martin-helmich/prometheus-nginxlog-exporter/exporter:v1 args: ["-config-file", "/etc/prometheus-nginxlog-exporter/config.hcl"] volumeMounts: - name: exporter-config mountPath: /etc/prometheus-nginxlog-exporter volumes: - name: exporter-config configMap: name: exporter-config


In this example, the configuration file is passed via the exporter-config ConfigMap. This might look like follows:

[source,yaml]

apiVersion: v1 kind: ConfigMap metadata: name: exporter-config data: config.hcl: | listen { port = 4040 }

namespace "nginx" {
  source = {
    syslog {
      listen_address = "udp://127.0.0.1:5531"
      format = "rfc3164"
    }
  }

  format = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$http_x_forwarded_for\""

  labels {
    app = "default"
  }
}

The config file instructs the exporter to accept log input via syslog. To forward logs to the exporter, just instruct your main container to send its access logs via syslog to 127.0.0.1:5531 (which works, since the main container and the sidecar share their network namespace).

Build from source

To build the exporter from source, simply build it with go get:

$ go get github.com/martin-helmich/prometheus-nginxlog-exporter

Alternatively, clone this repository and just run go build:

$ git clone https://github.com/martin-helmich/prometheus-nginxlog-exporter.git
$ cd prometheus-nginxlog-exporter
$ go build

== Collected metrics

This exporter collects the following metrics. This collector can listen on multiple log files at once and publish metrics in different namespaces. Each metric uses the labels method (containing the HTTP request method) and status (containing the HTTP status code).

[IMPORTANT]

Keep in mind that some of these metrics will require certain values to be present in your access log format (for example, the http_upstream_time_seconds metric will require your access to contain the variable $upstream_response_time.

Metrics are exported at the /metrics path.

These metrics are exported:

|=== | _http_response_count_total | The total amount of processed HTTP requests/responses. | _http_response_size_bytes | The total amount of transferred content in bytes. | _http_request_size_bytes | The total amount of received traffic in bytes. This metrics requires the $request_length variable in the log format. | _http_upstream_time_seconds | A summary vector of the upstream response times in seconds. Logging these needs to be specifically enabled in NGINX using the $upstream_response_time variable in the log format. | _http_upstream_time_seconds_hist | Same as _http_upstream_time_seconds, but as a histogram vector. Also requires the $upstream_response_time variable in the log format. | _http_response_time_seconds | A summary vector of the total response times in seconds. Logging these needs to be specifically enabled in NGINX using the $request_time variable in the log format. | _http_response_time_seconds_hist | Same as _http_response_time_seconds, but as a histogram vector. Also requires the $request_time variable in the log format. |===

Additional labels can be configured in the configuration file (see below).

`` can be omitted or overridden - see > for more information.

== Configuration file

You can specify a configuration file to read at startup. The configuration file is expected to be either in https://github.com/hashicorp/hcl[HCL] or YAML format. Here's an example file:

[source,hcl]

listen { port = 4040 address = "10.1.2.3" metrics_endpoint = "/metrics" }

consul { enable = true address = "localhost:8500" datacenter = "dc1" scheme = "http" token = "" service { id = "nginx-exporter" name = "nginx-exporter" address = "192.168.3.1" tags = ["foo", "bar"] } }

namespace "app1" { format = "$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"" source { files = [ "/var/log/nginx/app1/access.log" ] }

log can be printed to std out, e.g. for debugging purposes (disabled by default)

print_log = false

metrics_override = { prefix = "myprefix" }

namespace_label = "vhost"

labels { app = "application-one" environment = "production" foo = "bar" }

histogram_buckets = [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10] }

namespace "app2" { format = "$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $upstream_response_time" source { files = [ "/var/log/nginx/app2/access.log" ] }

}

The same file as YAML file:

[source,yaml]

listen: port: 4040 address: "10.1.2.3" metrics_endpoint: "/metrics"

consul: enable: true address: "localhost:8500" datacenter: dc1 scheme: http token: "" service: id: "nginx-exporter" name: "nginx-exporter" address = "192.168.3.1" tags: ["foo", "bar"]

namespaces:

  • name: app1 format: "$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"" source: files: - /var/log/nginx/app1/access.log

    metrics_override:

    prefix: "myprefix"

    namespace_label: "vhost"

    labels: app: "application-one" environment: "production" foo: "bar" histogram_buckets: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10]
  • name: app2 format: "$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $upstream_response_time" source: files: - /var/log/nginx/app2/access.log

Advanced features

Namespace as labels

For historic reasons, this exporter exports separate metrics for different namespaces (because the namespace is part of the met

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Gohacktoberfestmetrics-exporternginxprometheus-exporter

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryDevOps
PricingOpen source

> Related tools

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理