文章目录
一、Nginx监控模块简介
监控Nginx主要用到以下三个模块
1.nginx-module-vts:Nginx virtual host traffic status module,Nginx的监控模块,能够提供JSON格式的数据产出。
2、nginx-vts-exporter:Simple server that scrapes Nginx vts stats and exports them via HTTP for Prometheus consumption。主要用于收集Nginx的监控数据,并给Prometheus提供监控接口,默认端口号9913。
3、Prometheus:监控Nginx-vts-exporter提供的Nginx数据,并存储在时序数据库中,可以使用PromQL对时序数据进行查询和聚合。
二、安装过程
1.关闭防火墙、关闭核心防护
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
2.准备nginx软件包,并解压
tar zxvf nginx-1.15.9.tar.gz
unzip nginx-module-vts-master.zip //此软件包为监控nginx的模块
mv nginx-module-vts-master /usr/local/ //移动到/usr/local/下
3.安装依赖包
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel make
4.创建运行用户
useradd -M -s /sbin/nologin nginx
5.进行编译安装
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --add-module=/usr/local/nginx-module-vts-master/
make && make install
6.优化路径
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
7.添加nginx系统服务
vim /lib/systemd/system/nginx.service
chmod 754 /lib/systemd/system/nginx.service
8.检查开启的模块
nginx -V
9.修改配置文件
主要是修改默认日志文件格式,添加压缩配置,添加监控配置
cd /usr/local/nginx/conf/
vim nginx.conf
http下插入以下内容
log_format main '{ "@timestamp": "$time_local", '
'"@fields": { '
'"uri":"$request_uri",'
'"url":"$uri",'
'"upstream_addr":"$upstream_addr",'
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"body_bytes_sent": "$body_bytes_sent", '
'"host":"$host",'
'"server_addr":"$server_addr",'
'"request_time": "$request_time", '
'"request_time":"$request_time",'
'"status":"$status",'
'"request": "$request", '
'"request_method": "$request_method", '
'"size":$body_bytes_sent,'
'"upstream_time":"$upstream_response_time"'
'"http_referrer": "$http_referer", '
'"body_bytes_sent":"$body_bytes_sent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"http_user_agent": "$http_user_agent" } }';
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
server下location下插入以下内容
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
10.查看nginx配置文件
nginx -t
重启nginx
systemctl start nginx
11.访问测试
访问网页http://192.168.226.131
查看nginx服务有没有启动
访问状态监控模块http://192.168.226.131/status
- 监控列表各项信息
Server main 主服务器
Host:主机名
Version:版本号
Uptime:服务器运行时间
Connections active:当前客户端的连接数 reading:读取客户端连接的总数 writing:写入客户端连接的总数
Requsts accepted:接收客户端的连接总数 handled:已处理客户端的连接总数 Total:请求总数 Req/s:每秒请求的数量
Shared memory:共享内存 name:配置中指定的共享内存名称 maxSize:配置中指定的共享内存的最大限制 usedSize:共享内存的当前大小 usedNode:共享内存中当前使用的节点数 - Server zones 服务器区域
zone: 当前区域
Requests Total:请求总数 Req/s:每秒请求数 time:时间
Responses:状态码数量 1xx、2xx、3xx、4xx、5xx 表示响应不同状态码数量 Total:响应状态码的总数
Traffic表示流量 Sent:发送的流量 Rcvd:接收的流量 Sent/s:每秒发送的流量 Rcvd/s:每秒接收的流量
Cache表示缓存 Miss:未命中的缓存数 Bypass:避开的缓存数 Expirde:过期的缓存数 Stale:生效的缓存数 Updating:缓存更新的次数 Revalidated:重新验证的缓存书 Hit:缓存命中数 Scarce:未达缓存要求的请求次数Total:总数