因为nginx 已经提供了stub_status 模块,一般我们可以基于此进行监控,目前官方提供了一个exporter
尽管有一些限制(web必须使用8080)。以下是一个简单的学习使用
环境准备
- docker-compose 文件
注意使用了ranadeeppolavarapu 提供的nginx 镜像(很方便,可以学习各种nginx 插件的使用)
version: "3"
services:
httpservice:
image: ranadeeppolavarapu/nginx-http3:edge
volumes:
- "./nginx.conf:/etc/nginx/nginx.conf"
- "./h3.nginx.conf:/etc/nginx/conf.d/h3.nginx.conf"
- "./status.conf:/etc/nginx/conf.d/status.conf"
- "./localhost.crt:/etc/ssl/localhost.crt"
- "./localhost.key:/etc/ssl/localhost.key"
ports:
- "443:443/tcp"
- "443:443/udp"
- "8080:8080"
prome:
image: nginx/nginx-prometheus-exporter:0.8.0
command: -nginx.scrape-uri http://httpservice:8080/stub_status
ports:
- "9113:9113"
- nginx 配置
nginx.conf:
核心部分主要是关于nginx 的配置加载
include /etc/nginx/conf.d/*.conf;
status.conf:
server {
listen 8080;
server_name localhost;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript;
brotli_static on;
brotli on;
brotli_types text/plain text/css application/json application/javascript application/x-javascript text/javascript;
brotli_comp_level 4;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location = /stub_status {
stub_status;
}
}
- 启动访问效果
exporter信息
说明
nginx-prometheus-exporter 的实现并不是很难,可以学习下实现,而且目前官方也提供了grafana 的dashboard 配置,但是总的来说监控点
还是太弱了
参考资料
https://github.com/nginxinc/nginx-prometheus-exporter
http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
https://github.com/RanadeepPolavarapu/docker-nginx-http3