nginx 是我们常用反向代理软件, 它的监控对于我们来说十分重要, 今天介绍一些怎样利用 nginx-vts 模块和 prometheus 来实现对 nginx 的详细监控.
在使用 vts 之前, 我们想要获取 nginx 状态码统计, 访问量, 响应时间等都是通过 es 分析 nginx 访问日志得到的, 受限于公司 es 集群的规模, 我们没办法做时间跨度过大的统计. 后来调研 vts 模块, 发现我们这个模块可以完全满足我们的需求, 并且查询速度非常快, 查询半年数据也能很快展示出来 (我们 prometheus 只保留半年数据)
安装
1, 下载模块
https://github.com/vozlt/nginx-module-vts/releases
2, 编译
编译参数添加 --add-module=/path/to/nginx-module-vts
然后编译
3, 替换现有 bin 文件
4,reload nginx
通过 nginx -V 来查看模块是否添加成功
### 配置
1, 配置状态访问接口
在 default server 里添加状态查看 location, 并做好相关访问限制
- location /status{
- allow 127.0.0.1;
- deny all;
- vhost_traffic_status_display;
- vhost_traffic_status_display_format html;
- }
2, 开启 vts 统计
如果想统计所有的虚拟主机, 就在 http 配置中添加配置, 否则就只在想要监控的 server 中添加配置
配置说明:
vhost_traffic_status_zone; 开启基础监控
- vhost_traffic_status_filter on;
- vhost_traffic_status_filter_by_set_key $status $server_name;
- # 开启详细状态码统计
- vhost_traffic_status_filter on;
- vhost_traffic_status_filter_by_set_key $uri uris::$server_name;
- # 开启 uri 统计
此外还有基于地理信息的统计, 根据访问量或访问流量对 nginx
做访问限制, 详细使用见文档:
监控
prometheus 增加 nginx-vts 的 target 即可
http://127.0.0.1/status/format/prometheus
nginx-vts v0.1.17 之前的版本没有支持 promtheus 格式的输出, 如果是 v0.1.17 之前的版本, 需要通过 https://github.com/hnlq715/nginx-vts-exporter 将 nginx-vts 输出的信息转换为 prometheus 识别的格式. 当然, 建议最好还是使用最新的版本.
- prometheus record rules:
- groups:
- - name: nginx
- rules:
- #虚拟主机 rpm
- - expr: sum by (host) (rate(nginx_vts_server_requests_total{code="total",host!="localhost"}[40s])*60)
- record: 'nginx:total:rpm'
- #虚拟主机状态码 1xx 的 rpm
- - expr: sum by (host) (rate(nginx_vts_server_requests_total{code="1xx",host!="localhost"}[40s])*60)
- record: 'nginx:1xx:rpm'
- #虚拟主机状态码 2xx 的 rpm
- - expr: sum by (host) (rate(nginx_vts_server_requests_total{code="2xx",host!="localhost"}[40s])*60)
- record: 'nginx:2xx:rpm'
- #虚拟主机状态码 1xx 的 rpm
- - expr: sum by (host) (rate(nginx_vts_server_requests_total{code="3xx",host!="localhost"}[40s])*60)
- record: 'nginx:3xx:rpm'
- #虚拟主机状态码 5xx 的 rpm
- - expr: sum by (host) (rate(nginx_vts_server_requests_total{code="5xx",host!="localhost"}[40s])*60)
- record: 'nginx:5xx:rpm'
- #虚拟主机异常访问的 rpm
- - expr: sum by (host) (rate(nginx_vts_server_requests_total{code=~"[4-5]xx",host!="localhost"}[40s])*60)
- record: 'nginx:error:rpm'
- #nginx 实例的 rpm
- - expr: sum by (hostname) (rate(nginx_vts_server_requests_total{code="total"}[40s])*60)
- record: 'nginxhost:total:rpm'
- #nginx 实例状态码 2xx 的 rpm
- - expr: sum by (hostname) (rate(nginx_vts_server_requests_total{code="2xx"}[40s])*60)
- record: 'nginxhost:2xx:rpm'
- #nginx 实例异常访问的 rpm
- - expr: sum by (hostname) (rate(nginx_vts_server_requests_total{code=~"[4-5]xx"}[40s])*60)
- record: 'nginxhost:error:rpm'
- #虚拟主机的访问异常率
- - expr: 'nginx:error:rpm/nginx:total:rpm'
- record: 'http:error:rate'
- #nginx 实例的的访问异常率
- - expr: 'nginxhost:error:rpm/nginxhost:total:rpm'
- record: 'nginxhost:error:rate'
图表展示
因为 vts-module 暴露出的 metrics 里, server 和 upstream 并没有关联关系, 所以为了能在 grafana 中通过选择项目名称来同时展示 server 信息和 upstream 信息, 就需要在命名上做一些规范.
我们项目的命名方式: 机房名称. 运行环境. 项目属组. 项目名称
例如:
azure.prod.ops.download
一个 vhost 的配置大概如下:
- upstream azure.prod.ops.download {
- server 192.168.12.1 weight=2;
- server 192.168.12.2 weight=1;
- }
- server {
- listen 80;
- server_name
- azure.prod.ops.download
- download.domain1.com
- download.domain2.com
- ;
- access_log /usr/local/nginx/logs/azure.prod.ops.download_access.JSON JSON;
- error_log /usr/local/nginx/logs/azure.prod.ops.download_error.log;
- client_max_body_size 200m;
- location / {
- proxy_pass http://azure.prod.ops.download;
- proxy_set_header Host $http_host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_next_upstream error timeout http_404;
- }
- }
这样通过同一个 lebal 就能选择出 server 和 upstream 的信息.
如果一个 server 有多个 upstream, 就在后面添加子名称, 然后通过正则匹配前缀来获取所有的 upstream.
这样 prometheus 也可根据 idc 名称, 运行环境, 项目组来编写通用的 alert rules.
补充:
1, 参数 vhost_traffic_status_filter_by_host
vts-module 默认是将一个 vhost 的 metrics 汇总到第一个 server_name 上显示出来, 如果想按域名分开统计, 可以开启此参数. 注意: 如果一个 vhost 拥有多个域名, 开启此配置, 在监控和展示层面上会比较麻烦.
2, 如果没有 url 统计需求, 谨慎开启 uri 过滤. 如果需要开启, 要做好相关限制, 比如限制采集数量上限 vhost_traffic_status_filter_max_node. 尽量才用精确配置, 比如将 url 过滤参数写在需要监控的 localtion 中, 而不是整个 server 配置段里. 还有做好反扫描配置.
3, 如果在 http 段配置默认采集所有 vhosts 信息, 可在 server 配置段内, 通过 vhost_traffic_status_bypass_stats on 跳过采集
4,nginx-vts-modules 与模块冲突
来源: http://www.bubuko.com/infodetail-3274683.html