1.监控server和location的流量需要 nginx-module-vts 模块
GitHub - vozlt/nginx-module-vts: Nginx 虚拟主机流量状态模块https://github.com/vozlt/nginx-module-vts
下载 nginx-module-vts 直接复制粘贴即可
cd /root
git clone https://github.com/vozlt/nginx-module-vts.git
2.下载完成后重新编译一下nginx
./configure --add-module=/root/nginx-module-vts
如遇到此报错
./configure: error: the Google perftools module requires the Google perftools
library. You can either do not enable the module or install the library
安装 sudo yum install google-perftools google-perftools-devel 即可解决
3.在nginx.conf下添加以下内容
http {
.
.
vhost_traffic_status_zone;
#代理的location添加 vhost_traffic_status_filter_by_set_key /25 /25::$server_name; 其中/25 /25要和location后面的保持一致
location /25/ {
vhost_traffic_status_filter_by_set_key /25 /25::$server_name;
rewrite ^/25/(.*)([^/])$ http://代理的IP/25/$1$2/ ;
proxy_pass http://被代理的IP:8000/;
}
#代理的location添加 vhost_traffic_status_filter_by_set_key /status /status::$server_name; 其中/status /status要和location后面的保持一致
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
vhost_traffic_status_filter_by_set_key /status /status::$server_name;
}
4.如启动nginx报以下错误
[root@test2 sbin]#nginx -s reload
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
只需用-c指以下配置文件即可解决
[root@test2 sbin]#nginx -c /usr/local/nginx/conf/nginx.conf
[root@test2 sbin]#nginx -s reload #最后在重启以下即可完成了
5.现在浏览器上访问一下代理的地址,在访问IP/status即可
6.如果不想要在浏览器上显示/status::192.....,需要添加以下配置
location /status {
vhost_traffic_status_bypass_stats on;
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
vhost_traffic_status_filter_by_set_key /status /status::$server_name;