监控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对时序数据进行查询和聚合。
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
tar zxvf nginx-1.15.9.tar.gz
unzip nginx-module-vts-master.zip //此软件包为监控nginx的模块
mv nginx-module-vts-master /usr/local/ //移动到/usr/local/下
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel make
useradd -M -s /sbin/nologin nginx
./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --add-module=/usr/local/nginx-module-vts-master/
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
vim /lib/systemd/system/nginx.service
chmod 754 /lib/systemd/system/nginx.service
nginx -V
主要是修改默认日志文件格式,添加压缩配置,添加监控配置
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;
}
nginx -t
重启nginx
systemctl start nginx
访问网页http://192.168.226.131
查看nginx服务有没有启动
访问状态监控模块http://192.168.226.131/status