目录
一、nginx监控模块简介
二、配置nginx-module-vst-master
2.1 上传nginx-module-vst-master软件包
2.2 安装nginx依赖环境
2.3 编译安装nginx
2.4 优化管理
2.5 检查开启的模块
2.6 修改配置文件
2.7 启动测试
2.8 开机自启
引言:
使用nginx-module-vts监控Nginx的请求以便进行分析,监控请求主要是在应用层面,除了请求之外我们一般还需要监控经过nginx的流量、连接数等偏底层的数据,这些数据对分析nginx的性能比较重要下面我们看下如何对nginx流量进行监控
监控Nginx主要用到以下三个模块:
[root@oracle opt]# rz -E
rz waiting to receive.
[root@oracle opt]# ls
apache-maven-3.8.5-bin.tar.gz jdk-8u201-linux-x64.rpm nginx-1.20.2 nginx-1.20.2.tar.gz nginx-module-vts-master.zip rh
[root@oracle opt]# unzip nginx-module-vts-master.zip
[root@oracle opt]# mv nginx-module-vts-master /usr/local/
[root@oracle opt]# yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
[root@oracle opt]# tar zxvf nginx-1.20.2.tar.gz
[root@oracle opt]# cd nginx-1.20.2/
[root@oracle nginx-1.20.2]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-module-vts-master/
checking for OS
[root@oracle nginx-1.20.2]# make && make install
[root@oracle nginx-1.20.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
[root@oracle nginx-1.20.2]# nginx -v
nginx version: nginx/1.20.2
PS:主要是修改默认日志文件格式,添加压缩配置,添加监控配置,具体可根据自己的需求修改
http {
include mime.types;
default_type application/octet-stream;
vhost_traffic_status_zone; #流量状态监控
#vhost_traffic_status_filter_by_host on;
PS:开启此功能,在Nginx配置有多个server_name的情况下,会根据不同的server_name进行流量的统计,否则默认会把流量全部计算到第一个server_name上。
假如nginx没有规范配置server_name或者无需进行监控的server上,那么建议在此vhost上禁用统计监控功能。否则会出现“127.0.0.1”,hostname等的域名监控信息
user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
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" } }';
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json application/javascript;
gzip_vary on;
proxy_http_version 1.1;
proxy_set_header Connection "";
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
}
默认nginx安装目录为/usr/local/nginx/,相关目录都在这个目录下
启动nginx
[root@oracle conf]# /usr/local/nginx/sbin/nginx
测试
浏览器输入IP+80端口,可打开nginx默认页面
浏览器输入“IP:80/status”可打开监控页面
将“/usr/local/nginx/sbin/nginx”命令加入“/etc/rc.d/rc.local”文件并添加权限即可
chmod +x /etc/rc.d/rc.local