目录

监控nginx主要用到以下三个模块

安装依赖环境

解压缩文件包

创建运行用户

检查配置文件是否配置正确

使用systemctl 管理

修改配置文件

编辑测试是否加载完成编辑


监控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 Prome theus consumption. 主要用于收集nginx的监控数据,并给prometheus提供监控接口,默认端口号为9913

3、prometheus:监控nginx-vts-exporter提供的nginx数据,并且存储在时序数据库中,可以使用rromQL对时序数据进行查询和聚合。

安装依赖环境

systemctl stop firewalld
systemctl disable firewalld
setenforce 0

yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel make

监控 nginx接口 nginx标准监控端口为_服务器

解压缩文件包

cd /opt
tar -xvf nginx-1.15.9.tar.gz
cd nginx-1.15.9
./configure \
--prefix=/usr/local/nginx \                        指定路径
--user=nginx \                                     指定用户
--group=nginx \                                    指定组
--add-module=/usr/local/nginx-module-vts-master/   代替上一条

监控 nginx接口 nginx标准监控端口为_缓存_02

监控 nginx接口 nginx标准监控端口为_nginx_03

监控 nginx接口 nginx标准监控端口为_运维_04

监控 nginx接口 nginx标准监控端口为_监控 nginx接口_05

创建运行用户

useradd -M -s /sbin/nologin nginx

检查配置文件是否配置正确

nginx -t     #检查配置文件是否配置正确
nginx        #启动

使用systemctl 管理

vim /usr/lib/systemd/system/nginx.service
[Unit]	
Description=nginx							#描述
After=network.target						#描述服务类别
[Service]
Type=forking								#后台运行类型
PIDFile =/usr/local/nginx/logs/nginx.pid	#PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx		#启动服务
ExecrReload=/bin/kill -s HUP $MAINPID		#根据PID重载配置
ExecrStop=/bin/kill -s QUIT $MAINPID		#根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target					#启动级别

chmod 754 /lib/systemd/system/nginx.service		#设置754权限是一种安全优化
systemctl start nginx.service
systemctl enable nginx.service

PS:
systemctl status 多个服务
watch -n 2 systemctl status

监控 nginx接口 nginx标准监控端口为_服务器_06

监控 nginx接口 nginx标准监控端口为_服务器_07

监控 nginx接口 nginx标准监控端口为_监控 nginx接口_08

修改配置文件

vim nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    vhost_traffic_status_zone;			#流量状态监控

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" } }';

PS:主要是修改默认日志文件格式,添加压缩配置,添加监控配置,具体可根据自己的需求修改

监控 nginx接口 nginx标准监控端口为_监控 nginx接口_09

监控 nginx接口 nginx标准监控端口为_nginx_10

监控 nginx接口 nginx标准监控端口为_服务器_11

location / {
            root   html;
            index  index.html index.htm;
        }
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }

监控 nginx接口 nginx标准监控端口为_服务器_12

测试是否加载完成

监控 nginx接口 nginx标准监控端口为_缓存_13

监控 nginx接口 nginx标准监控端口为_缓存_14

监控 nginx接口 nginx标准监控端口为_服务器_15

监控列表各项信息

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:总数