nginx增加VTS模塊的實現(xiàn)
一、VTS模塊的核心功能
Nginx Virtual Host Traffic Status Module 實時監(jiān)控 Nginx 虛擬主機(或服務(wù)器塊)的流量狀態(tài)、請求指標、響應(yīng)性能等數(shù)據(jù),并提供可視化頁面和 API 接口,方便運維人員排查問題、分析流量趨勢
VTS 模塊能監(jiān)控的指標覆蓋 “請求 - 流量 - 響應(yīng)” 全鏈路,且支持按虛擬主機、服務(wù)、位置(Location)粒度拆分
| 功能分類 | 核心監(jiān)控指標 |
|---|---|
| 請求指標 | 總請求數(shù)、成功請求數(shù)、各 HTTP 狀態(tài)碼(2xx/3xx/4xx/5xx)請求數(shù)、每秒請求數(shù)(RPS) |
| 流量指標 | 入站流量(接收字節(jié)數(shù))、出站流量(發(fā)送字節(jié)數(shù))、每秒流量(BPS) |
| 響應(yīng)性能指標 | 平均響應(yīng)時間、響應(yīng)時間分布(如 100ms 內(nèi)、500ms 內(nèi)的請求占比) |
| 連接指標 | 當前活躍連接數(shù)、空閑連接數(shù)、已關(guān)閉連接數(shù) |
| 過濾與排序 | 支持按 “請求數(shù)、流量、響應(yīng)時間” 等維度排序,也可過濾特定虛擬主機或狀態(tài)碼 |
| 數(shù)據(jù)輸出方式 | 1. 內(nèi)置 HTML 可視化頁面(直觀查看) 2. JSON/CSV/Prometheus 格式 API(集成監(jiān)控系統(tǒng)) |
二、增加VTS模塊
首先需要上傳vts模塊包nginx-module-vts-master.zip到/root家目錄
下載與當前 Nginx 版本一致的源碼和 VTS 模塊源碼
執(zhí)行./configure時,在復(fù)制的原有參數(shù)后追加--add-module=path/nginx-module-vts
[root@web-2 ~]# vim install_nginx.sh #!/bin/bash set -e #下載nginx mkdir -p /nginx9 cd /nginx9 curl -O https://nginx.org/download/nginx-1.28.0.tar.gz echo "下載nginx成功" #解壓nginx源碼包 tar xf nginx-1.28.0.tar.gz cd nginx-1.28.0 #解決nginx依賴的軟件 yum install gcc pcre-devel openssl-devel zlib-devel vim net-tools unzip -y #解壓vts模塊包 nginx-module-vts-master.zip cp /root/nginx-module-vts-master.zip . unzip nginx-module-vts-master.zip #編譯前的配置工作 ./configure --prefix=/usr/local/nginx9 --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_stub_status_module --with-stream --with-stream_ssl_module --with-threads --add-module=/nginx9/nginx-1.28.0/nginx-module-vts-master #編譯 make -j 2 #編譯安裝 make install #修改PATH環(huán)境變量 echo 'PATH=/usr/local/nginx9/sbin:$PATH' >> /etc/bashrc echo "安裝nginx成功,請使用nginx" #考慮nginx開機自啟 echo '/usr/local/nginx9/sbin/nginx' >>/etc/rc.local chmod +x /etc/rc.d/rc.local su
執(zhí)行腳本
[root@web2 ~]# bash install_nginx.sh
如需給原來的nginx增加則需替換二進制文件,且參數(shù)要–prefix要一致,用objs/nginx替換sbin/nginx
目前有多個版本的nginx了,如何啟動指定版本的nginx?
-> 使用絕對路徑去啟動nginx
/usr/local/nginx1/sbin/nginx
/usr/local/nginx1/sbin/nginx -s stop
/usr/local/nginx1/sbin/nginx -s reload
直接輸入nginx會啟動最新版本的nginx,因為PATH變量里的最左邊的路徑是最新的nginx的安裝路徑
[root@web2 sbin]# /usr/local/nginx1/sbin/nginx -s stop
[root@web2 sbin]# which nginx
/usr/local/nginx9/sbin/nginx
啟動新的nginx
[root@web2 sbin]# nginx [root@web2 sbin]# ps aux|grep nginx root 5435 0.0 0.0 12220 2464 ? Ss 16:35 0:00 nginx: master process nginx sc 5436 0.0 0.1 16632 5536 ? S 16:35 0:00 nginx: worker process root 5438 0.0 0.0 6636 2176 pts/0 S+ 16:35 0:00 grep --color=auto nginx [root@web2 sbin]# nginx -V nginx version: nginx/1.28.0 built by gcc 11.5.0 20240719 (Red Hat 11.5.0-5) (GCC) built with OpenSSL 3.2.2 4 Jun 2024 TLS SNI support enabled configure arguments: --prefix=/usr/local/nginx9 --user=sc --group=sc --with-http_ssl_module --with-http_v2_module --with-http_v3_module --with-http_stub_status_module --with-stream --with-stream_ssl_module --with-threads --add-module=/nginx9/nginx-1.28.0/nginx-module-vts-master
修改nginx.conf主配置文件,添加支持vts的功能
[root@web2 sbin]# cd /usr/local/nginx9/conf/
[root@web2 conf]# vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#添加vts功能的配置
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.feng.com;
#charset koi8-r;
access_log logs/feng.com.access.log main;
location / {
root html;
index index.html index.htm;
}
#添加vts功能的配置
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
}
..... 省略部分配置
刷新配置
[root@web2 conf]# nginx -t
nginx: the configuration file /usr/local/nginx9/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx9/conf/nginx.conf test is successful
[root@web2 conf]# nginx -s reload
最后去訪問web服務(wù)器
訪問效果

到此這篇關(guān)于nginx增加VTS模塊的實現(xiàn)的文章就介紹到這了,更多相關(guān)nginx VTS模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx+php出現(xiàn)No input file specified解決辦法
這篇文章主要介紹了nginx+php出現(xiàn)No input file specified解決辦法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
Nginx訪問控制與參數(shù)調(diào)優(yōu)的方法
這篇文章主要介紹了Nginx訪問控制與參數(shù)調(diào)優(yōu)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
配置nginx保證frps服務(wù)器與web共用80端口的方法
這篇文章主要介紹了frps服務(wù)端與nginx可共用80端口的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

