Skip to content

Instantly share code, notes, and snippets.

@arsalanses
Created April 9, 2024 07:23
Show Gist options
  • Select an option

  • Save arsalanses/6f959c61a5bb0165151ec7aed678185f to your computer and use it in GitHub Desktop.

Select an option

Save arsalanses/6f959c61a5bb0165151ec7aed678185f to your computer and use it in GitHub Desktop.
Nginx + Vector, log to prometheus metrics
services:
nginx:
image: nginx:1.25.4-alpine-slim
restart: always
container_name: "nginx"
ports:
- "127.0.0.1:8080:80"
volumes:
- ./config/nginx/default.conf:/etc/nginx/conf.d/default.conf
- ./config/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./config/nginx/.htpasswd:/etc/nginx/.htpasswd
- ./host.access.log:/var/log/nginx/host.access.log
vector:
image: hub.hamdocker.ir/timberio/vector:0.36.0-alpine
container_name: vector
volumes:
- ./config/vector/vector.yml:/etc/vector/vector.yaml:ro
- ./host.access.log:/var/log/host.access.log:ro
ports:
- "127.0.0.1:9598:9598"
logging:
options:
max-size: "512m"
limit_conn_zone $server_name zone=servconn:5m;
limit_req_zone $binary_remote_addr zone=onerps:5m rate=150r/s;
# proxy_cache_path /data/nginx/cache keys_zone=mycache:10m;
server {
listen 80;
listen [::]:80;
server_name localhost;
proxy_connect_timeout 120;
proxy_send_timeout 120;
proxy_read_timeout 120;
#access_log /var/log/nginx/host.access.log main;
location / {
access_log /var/log/nginx/host.access.log custom;
limit_rate 2M;
limit_conn servconn 1024;
limit_req zone=onerps burst=75;
location ~* \.(js|css|woff|gif|png|svg|ico)$ {
# proxy_cache mycache;
# add_header X-Cache $upstream_cache_status;
expires 60s;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location = /basic_status {
stub_status;
satisfy all;
auth_basic "Login";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
sources:
basic_nginx_metrics:
type: nginx_metrics
endpoints:
- http://nginx/basic_status
namespace: nginx
scrape_interval_secs: 15
auth:
strategy: "basic"
user: "admin"
password: "1234"
access_log_file:
type: file
include:
- /var/log/host.access.log
transforms:
parse_nginx_log:
type: remap
inputs:
- access_log_file
drop_on_abort: true
source: |-
. = parse_nginx_log!(.message, "combined")
log_to_metric_file:
type: log_to_metric
inputs:
- parse_nginx_log
metrics:
- field: status
type: counter
namespace: nginx
name: response_total
tags:
agent: "{{agent}}"
client: "{{client}}"
referer: "{{referer}}"
request: "{{request}}"
size: "{{size}}"
status: "{{status}}"
sinks:
console:
inputs:
- basic_nginx_metrics
- log_to_metric_file
target: stdout
type: console
encoding:
codec: json
prometheus_exporter:
type: prometheus_exporter
inputs:
- log_to_metric_file
- basic_nginx_metrics
address: 0.0.0.0:9598
auth:
strategy: "basic"
user: "admin"
password: "1234"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment