前言Nginx官网有介绍各种监控方案,以前我们常用stub_status和Log日志来实现Nginx监控。本文主要介绍基于Prometheus的2种监控方案nginx-lua-prometheus和nginx-vts-exporter,在真实的生产环境中你可能需要研究和选择哪种方法才是适合你的,F5收购Nginx后的未来让我们一起拭目以待。Prometheus 监控 Nginx更新历史2019年03月25日 - 初稿阅读原文 - https://wsgzao.github.io/post…扩展阅读Monitoring NGINX - https://www.nginx.com/blog/mo...Nginx监控官网介绍的监控方案 - https://www.nginx.com/blog/mo...Prometheus 集成的 HTTP exporter 方案 - https://prometheus.io/docs/in…聊聊 Nginx 的监控 - https://zhuanlan.zhihu.com/p/…使用rpmbuild制作Nginx的RPM包 - https://wsgzao.github.io/post...Prometheus 监控 Nginxnginx-lua-prometheusNginx 需要添加 Lua 扩展https://github.com/knyar/ngin…# 下载redis_exporterhttps://github.com/knyar/nginx-lua-prometheus/releaseswget https://github.com/knyar/nginx-lua-prometheus/archive/0.20181120.tar.gztar xf 0.20181120.tar.gzcd nginx-lua-prometheus-0.20181120# 创建prometheus.lua目录mkdir -p /etc/nginx/lua/cp prometheus.lua /etc/nginx/lua/# 编辑nginx配置文件修改,注意修改lua_package_path “/etc/nginx/lua/prometheus.lua”;vim /etc/nginx/nginx.conflua_shared_dict prometheus_metrics 10M;lua_package_path “/etc/nginx/lua/prometheus.lua”;init_by_lua ’ prometheus = require(“prometheus”).init(“prometheus_metrics”) metric_requests = prometheus:counter( “nginx_http_requests_total”, “Number of HTTP requests”, {“host”, “status”}) metric_latency = prometheus:histogram( “nginx_http_request_duration_seconds”, “HTTP request latency”, {“host”}) metric_connections = prometheus:gauge( “nginx_http_connections”, “Number of HTTP connections”, {“state”})’;log_by_lua ’ metric_requests:inc(1, {ngx.var.server_name, ngx.var.status}) metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})’;# 创建nginx-lua-prometheusvim /etc/nginx/sites-available/nginx-lua-prometheusserver { listen 9145; location /metrics { content_by_lua ’ metric_connections:set(ngx.var.connections_reading, {“reading”}) metric_connections:set(ngx.var.connections_waiting, {“waiting”}) metric_connections:set(ngx.var.connections_writing, {“writing”}) prometheus:collect() ‘; }}# 创建软链接cd /etc/nginx/sites-enabled/ln -s ../sites-available/prometheus# 测试Nginx语法并reload测试metricsnginx -tnginx -s reloadcurl http://127.0.0.1:9145/metrics# iptables rule for Prometheus Nginx -A INPUT -s xxx -p tcp –dport 9145 -j ACCEPTnginx-vts-exporterhttps://github.com/hnlq715/ng…对方正在输入中Grafananginx-lua-prometheushttps://grafana.com/dashboard…nginx-vts-exporterhttps://grafana.com/dashboard…参考文献https://prometheus.io/docs/in…