国内用Nginx的比较多,Nginx的监控比较老的方案多是经过跑脚本按期收集nginx的status模块的数据,或者监控nginx的日志;后来阿里的tengine在国内开始流行,因而诞生了不少不错的lua模块;可是这些监控方案在有新的监控需求的时候,可能就须要再修改脚本或者更改nginx conf配置,有时候不是特别的方便。用Prometheus进行nginx的监控能够自动的对相关server_name和upstream进行监控,你也能够自定义Prometheus的数据标签,实现对不一样机房和不一样项目的nginx进行监控。
监控Nginx主要用到如下三个模块:
nginx-module-vts:Nginx virtual host traffic status module,Nginx的监控模块,可以提供JSON格式的数据产出。
nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913。
Prometheus:监控Nginx-vts-exporter提供的Nginx数据,并存储在时序数据库中,可使用PromQL对时序数据进行查询和聚合。html
1、nginx-module-vts模块的编译
nginx_vts_exporter依赖nginx-module-vts模块,安装此模块无需任何其余依赖。模块与Nginx的版本兼容性以下:linux
1.11.x (last tested: 1.11.10)
1.10.x (last tested: 1.10.3)
1.8.x (last tested: 1.8.0)
1.6.x (last tested: 1.6.3)
1.4.x (last tested: 1.4.7)
同时适用于tengine,其余nginx早期版本未作验证。
安装步骤:nginx
shell> git clone git://github.com/vozlt/nginx-module-vts.git
编译配置
在nginx编译时添加vts模块--add-module=/path/to/nginx-module-vts
下载官方的软件包并编译进vts模块,例如:git
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_sysguard_module --add-module=nginx-module-vts
安装:make && make install
2、Nginx Conf配置
更改Nginx Conf的配置,添加监控接口/status/:github
http { vhost_traffic_status_zone; vhost_traffic_status_filter_by_host on; ... server { ... location /status { vhost_traffic_status_display; vhost_traffic_status_display_format html; } } }
配置建议: shell
vhost_traffic_status_filter_by_host on;
server { ... vhost_traffic_status off; ... }
假如nginx没有规范配置server_name或者无需进行监控的server上,那么建议在此vhost上禁用统计监控功能。不然会出现“127.0.0.1”,hostname等的域名监控信息。
3、监控数据的查看
安装完vts模块后,能够经过nginx status接口进行监控数据的查看,好比:http://127.0.0.1/status:数据库
在页面的最下方能够指定监控页面刷新的时间间隔,点击JSON,能够转为JSON格式输出。json
3、nginx-vts-exporter的使用
exporter会收集nginx性能指标的JSON格式数据,并汇总后暴露监控接口给Prometheus。后端
它的安装使用很简单,开箱即用:ide
# wget -c https://github.com/hnlq715/nginx-vts-exporter/releases/download/v0.9.1/nginx-vts-exporter-0.9.1.linux-amd64.tar.gz
# nginx-vts-exporter-0.9.1.linux-amd64/nginx-vts-exporter -nginx.scrape_timeout 10 -nginx.scrape_uri http://127.0.0.1/status/format/json
4、Nginx的监控数据类型
nginx-vts-exporter的数据类型命名空间默认以“nginx”开头,主要有以下9个:
HELP是对监控条目的解释,TYPE的格式是:监控条目名称+Prometheus数据类型:
# HELP nginx_server_bytes request/response bytes # TYPE nginx_server_bytes counter # HELP nginx_server_cache cache counter # TYPE nginx_server_cache counter # HELP nginx_server_connections nginx connections # TYPE nginx_server_connections gauge # HELP nginx_server_requestMsec average of request processing times in milliseconds # TYPE nginx_server_requestMsec gauge # HELP nginx_server_requests requests counter,能够区分状态码 # TYPE nginx_server_requests counter # HELP nginx_upstream_bytes request/response bytes # TYPE nginx_upstream_bytes counter # HELP nginx_upstream_requestMsec average of request processing times in milliseconds # TYPE nginx_upstream_requestMsec gauge # HELP nginx_upstream_requests requests counter,能够区分状态码 # TYPE nginx_upstream_requests counter # HELP nginx_upstream_responseMsec average of only upstream/backend response processing times in milliseconds # TYPE nginx_upstream_responseMsec gauge
5、Nginx监控在Prometheus的数据汇总
经常使用监控汇总表达式:
DomainName对应nginx conf里的server_name,这里能够根据不一样的server_name和upstream分别进行qps、2xx/3xx/4xx/5xx的状态码监控,另外也能够监控nginx每台后端server的qps和后端接口响应时间。
若是不须要区分server_name,能够把表达式里的$DomainName改成星号,“*****”表明全部;
sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))
(sum(irate(nginx_server_requests{code="4xx",host=~"$DomainName"}[5m])) / sum(irate(nginx_server_requests{code="total",host=~"$DomainName"}[5m]))) * 10000
sum(irate(nginx_upstream_requests{code="total",upstream="group1"}[5m]))
nginx_upstream_responseMsec{upstream=“group1”}
6、Nginx监控的展现
Dashboard的展现固然是使用grafana,本身根据表达式画图便可,监控图相似: