nginx流量监控模块nginxmodulevts使用

2023年 7月 15日 32.3k 0

nginx-module-vts他可以记录单个页面的流量,http status的流量,后端代理的流量已经动态dns的流量,还有来自地区/国家的流量,其中可以进行限制流量,并且他还有一个页面,可以根据server_name进行统计域名的流量已经状态码,只需要简单的配置和编译就可以实现,如果希望使用docker,那就太好了,因为我已经为你准备好了示例 docker安装nginx1.14.0-vts

模块的下载

这里还加了luajit-2.0

[root@linuxea-VM-Node203 ~]# git clone git://github.com/vozlt/nginx-module-vts.git "/usr/local/nginx-module-vts"
[root@linuxea-VM-Node203 ~]# git clone git://github.com/vozlt/nginx-module-sts.git "/usr/local/nginx-module-sts"
[root@linuxea-VM-Node203 ~]# git clone git://github.com/vozlt/nginx-module-stream-sts.git "/usr/local/nginx-module-stream-sts"
### git clone lua_module
[root@linuxea-VM-Node203 ~]# curl -Lk https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz |tar xz -C /usr/local
[root@linuxea-VM-Node203 ~]#  curl -Lk https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz |tar xz -C  /usr/local
[root@linuxea-VM-Node203 ~]#  curl -Lk http://luajit.org/download/LuaJIT-2.0.5.tar.gz |tar xz -C /usr/local
[root@linuxea-VM-Node203 ~]# cd /usr/local/LuaJIT-2.0.5 && make && make install 
[root@linuxea-VM-Node203 ~]# export LUAJIT_LIB=/usr/local/lib
[root@linuxea-VM-Node203 ~]# export LUAJIT_INC=/usr/local/include/luajit-2.0

编译安装

我们下载最新的nginx,创建用户,编译并添加模块

[root@linuxea-VM-Node203 ~]# useradd www -s /sbin/nologin -M
[root@linuxea-VM-Node203 ~]# curl -Lk http://nginx.org/download/nginx-1.14.0.tar.gz |tar xz -C /usr/local 
[root@linuxea-VM-Node203 ~]#  cd /usr/local/nginx-1.14.0 && ./configure 
--prefix=/usr/local/nginx 
--conf-path=/etc/nginx/nginx.conf 
--user=www 
--group=www 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--pid-path=/var/run/nginx/nginx.pid 
--lock-path=/var/lock/nginx.lock 
--with-http_ssl_module 
--with-http_stub_status_module 
--with-http_gzip_static_module 
--with-http_flv_module 
--with-http_mp4_module 
--with-http_geoip_module 
--http-client-body-temp-path=/var/tmp/nginx/client 
--http-proxy-temp-path=/var/tmp/nginx/proxy 
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi 
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi 
--add-module=/usr/local/lua-nginx-module-0.10.13 
--add-module=/usr/local/ngx_devel_kit-0.3.1rc1 
--add-module=/usr/local/nginx-module-vts 
--with-stream 
--add-module=/usr/local/nginx-module-sts 
--add-module=/usr/local/nginx-module-stream-sts && make -j2 && make install
[root@linuxea-VM-Node203 /usr/local/nginx-1.14.]#  ln -s /usr/local/lib/libluajit-5.1.so.2 /lib/ 

nginx-module-vts配置文件

我分别编辑了两个conf文件include进去加载主配置文件在http部分 : nginx-module-vts_zone.conf

geoip_country /etc/nginx/GeoIP.dat;  # 使用GeoIP计算各个国家/地区的流量
vhost_traffic_status_zone; # 必须的指令
vhost_traffic_status_filter_by_host on; # 以server_name的形式展示
#vhost_traffic_status_bypass_stats on; # 不会统计流量页面的数据流量
vhost_traffic_status_filter_by_set_key $geoip_country_code country::*; # # 使用GeoIP计算各个国家/地区的流量
    map $http_user_agent $filter_user_agent { # 计算单个用户代理的流量
        default 'unknown';
        ~iPhone ios;
        ~Android android;
        ~(MSIE|Mozilla) windows;
    }
vhost_traffic_status_filter_by_set_key $filter_user_agent agent::*; # 计算单个用户代理的流量

加载vhost的server部分: nginx-module-vts_zone.conf

vhost_traffic_status_set_by_filter $variable group/zone/name; # 获取存储在共享内存中的指定状态值。
vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name; # 以server_name的形式展示
vhost_traffic_status_bypass_stats on; # 不会统计流量页面的数据流量
vhost_traffic_status_filter_by_set_key $status $server_name; # 计算详细的http状态代码的流量
vhost_traffic_status_filter_by_set_key $filter_user_agent agent::$server_name; # 计算单个用户代理的流量

或者可以这样http段直接加

    vhost_traffic_status_zone;
    vhost_traffic_status_filter_by_host on;    

server段直接加

server {
    listen 8295;
    server_name localhost;
# disaned status vhost_traffic_status off;    
#    vhost_traffic_status off;
        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }

打开浏览器web-name:8295/status20180801-1.png

  • 这个模块本身是可以直接用来prometheus使用的,只要访问/status/format/prometheus即可,本地来搞一下看看效果,过滤一段试试

    [root@linuxea-VM-Node203 /etc/nginx]# curl 10.10.240.203:8295/status/format/prometheus|grep nginx_vts_server_requests_total
    % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
  • 7631 100 7631 0 0 10.3M 0 --:--:-- --:--:-- --:--:-- 7452k

    HELP nginx_vts_server_requests_total The requests counterTYPE nginx_vts_server_requests_total counter

    nginx_vts_server_requests_total{host="10.10.240.203",code="1xx"} 0nginx_vts_server_requests_total{host="10.10.240.203",code="2xx"} 1663nginx_vts_server_requests_total{host="10.10.240.203",code="3xx"} 0nginx_vts_server_requests_total{host="10.10.240.203",code="4xx"} 0nginx_vts_server_requests_total{host="10.10.240.203",code="5xx"} 0nginx_vts_server_requests_total{host="10.10.240.203",code="total"} 1663nginx_vts_server_requests_total{host="linuxea.ds.com",code="1xx"} 0nginx_vts_server_requests_total{host="linuxea.ds.com",code="2xx"} 0nginx_vts_server_requests_total{host="linuxea.ds.com",code="3xx"} 294nginx_vts_server_requests_total{host="linuxea.ds.com",code="4xx"} 0nginx_vts_server_requests_total{host="linuxea.ds.com",code="5xx"} 0nginx_vts_server_requests_total{host="linuxea.ds.com",code="total"} 294nginx_vts_server_requests_total{host="*",code="1xx"} 0nginx_vts_server_requests_total{host="*",code="2xx"} 1663nginx_vts_server_requests_total{host="*",code="3xx"} 294nginx_vts_server_requests_total{host="*",code="4xx"} 0nginx_vts_server_requests_total{host="*",code="5xx"} 0nginx_vts_server_requests_total{host="*",code="total"} 1957[root@linuxea-VM-Node203 /etc/nginx]#

    当然这样一来安全就有些问题了
    #### 配置nginx认证
    在公网上跑的时候出来iptables的防火墙对固定ip放行端口的同时,一定要在骚一些弄个用户验证
    生成一个htpasswd的用户和密码,用户名:linuxea 密码:www.linuxea.com

    [root@linuxea-VM-Node63 /etc/nginx/vhost]# htpasswd -c /usr/local/ngxpasswd linuxeaNew password: Re-type new password: Adding password for user linuxea

    添加到nginx的状态页面中来
    主要添加如下:

    auth_basic "Please enter your id and password!"; auth_basic_user_file /etc/nginx/ngxpasswd;

    如下:

    server { listen 8295; server_name localhost; auth_basic "Please enter your id and password!"; auth_basic_user_file /etc/nginx/ngxpasswd;

    #disaned status vhost_traffic_status off; vhost_traffic_status off; location /status {

      vhost_traffic_status_display;
      vhost_traffic_status_display_format html;

    }}

    接着打开就需要认证了
    ![20180801-2.png][2]
    到此nginx认证完成
    #### 配置prometheus抓取端
  • metrics_path字段的位置需要写明/status/format/prometheus
  • basic_auth 用户和密码,在上面进行配置nginx的认证的那些
  - job_name: "nginx"
    metrics_path: /status/format/prometheus
    basic_auth:
      username: linuxea
      password: 'www.linuxea.com'
    static_configs:
    - targets:
      - '10.10.240.203:8295'
      labels:
        group: 'nginx'

添加到proentheus上可以抓取,20180801-3.png

  • 如果有问题,你应该检查targets是否up20180801-4.png

    安装nginx-vts-exporter

    事实上在我对比后,nginx-vts-exporter更适用prometheus抓取,里面有一些是nginx-module-vts没有的,so,我们进行安装nginx-vts-exporter

    [root@linuxea-VM-Node203 /etc/nginx/vhost]# docker pull sophos/nginx-vts-exporter:latest
    [root@linuxea-VM-Node203 ~]# docker run  -ti --rm --env NGINX_STATUS="http://linuxea:www.linuxea.com@localhost:8295/status/format/json" sophos/nginx-vts-exporter

    这时候会启动9913端口,通过浏览器可以访问(你可能需要做好防火墙规则),因为之前加了验证,这里需要添加用户和密码http://linuxea:www.linuxea.com@localhost:8295/status/format/json

  • 通过9913端口可以查看所有的指标。我这里用linuxea做测试
[root@linuxea-VM-Node203 /etc/nginx/vhost]# curl http://10.10.240.203:9913/metrics|grep "linuxea"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  7028  100  7028    0     0  2013k      0 --:--:-- --:--:-- --:--:-- 2287k
nginx_server_bytes{direction="in",host="linuxea.ds.com"} 1.700065e+06
nginx_server_bytes{direction="out",host="linuxea.ds.com"} 1.240604e+06
nginx_server_cache{host="linuxea.ds.com",status="bypass"} 0
nginx_server_cache{host="linuxea.ds.com",status="expired"} 0
nginx_server_cache{host="linuxea.ds.com",status="hit"} 0
nginx_server_cache{host="linuxea.ds.com",status="miss"} 0
nginx_server_cache{host="linuxea.ds.com",status="revalidated"} 0
nginx_server_cache{host="linuxea.ds.com",status="scarce"} 0
nginx_server_cache{host="linuxea.ds.com",status="stale"} 0
nginx_server_cache{host="linuxea.ds.com",status="updating"} 0
nginx_server_requestMsec{host="linuxea.ds.com"} 0
nginx_server_requests{code="1xx",host="linuxea.ds.com"} 0
nginx_server_requests{code="2xx",host="linuxea.ds.com"} 40
nginx_server_requests{code="3xx",host="linuxea.ds.com"} 3792
nginx_server_requests{code="4xx",host="linuxea.ds.com"} 1
nginx_server_requests{code="5xx",host="linuxea.ds.com"} 0
nginx_server_requests{code="total",host="linuxea.ds.com"} 3833

直接使用prometheus如:查看nginx_server_requests指标,host为linuxea.ds.com,30s的数据,只显示code和host字段

sum (irate(nginx_server_requests{host!="*",host="linuxea.ds.com",code!="total"}[30s])) by (code,host)

20180812-2.png并且可以和grafana配合使用,我这里将官网的模板也inport进去了,你可以去我gitlhub下载nginx-vts-stats_rev2 (1).json,也可以去grafana下载当你Import dashboard 后你会看到这样的一个画面20180812-3.png

额外的nginx-module-sts配置

nginx http段添加stream_server_traffic_status_zone;在http内include vhost/stream.conf;在http外include stream_server.conf;创建server段文件

[root@linuxea-VM-Node203 /etc/nginx]# cat vhost/stream.conf 
server {
    listen 82;
    server_name linuxea.ds.com;
    location /status {
        stream_server_traffic_status_display;
        stream_server_traffic_status_display_format html;
    }
    }

创建stream_server.conf 文件

[root@linuxea-VM-Node203 /etc/nginx]# cat stream_server.conf 
stream {
    geoip_country /etc/nginx/GeoIP.dat;
    server_traffic_status_zone;
    server_traffic_status_filter_by_set_key $geoip_country_code country::*;
    server {
        server_traffic_status_filter_by_set_key $geoip_country_code country::$server_addr:$server_port;
}

20180801-6.png

部分参数

对location指令的正则表达式匹配的单个storage的流量。

http {
    vhost_traffic_status_zone;

    ...

    server {

        ...

        location ~ ^/storage/(.+)/.*$ {
            set $volume $1;
            vhost_traffic_status_filter_by_set_key $volume storage::$server_name;
        }

        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

计算单个用户代理的流量

  • 计算个人的流量 http_user_agent
http {
    vhost_traffic_status_zone;

    map $http_user_agent $filter_user_agent {
        default 'unknown';
        ~iPhone ios;
        ~Android android;
        ~(MSIE|Mozilla) windows;
    }

    vhost_traffic_status_filter_by_set_key $filter_user_agent agent::*;

    ...

    server {

        ...

        vhost_traffic_status_filter_by_set_key $filter_user_agent agent::$server_name;

        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

http status code状态码的流量

http {
    vhost_traffic_status_zone;

    server {

        ...

        vhost_traffic_status_filter_by_set_key $status $server_name;

        location /status {
            vhost_traffic_status_display;
            vhost_traffic_status_display_format html;
        }
    }
}

计算动态dns的流量如果域具有多个DNS A记录,则可以使用过滤器功能或proxy_pass中的变量计算域的各个IP的流量。

http {
    vhost_traffic_status_zone;

    upstream backend {
        elb.example.org:80;
    }

    ...

    server {

        ...

        location /backend {
            vhost_traffic_status_filter_by_set_key $upstream_addr upstream::backend;
            proxy_pass backend;
        }
    }
}
  • 计算域的各个IP的流量elb.example.org。如果elb.example.org有多个DNS A记录,将显示所有IP filterZones。在上述设置中,当NGINX启动或重新加载配置时,它会查询DNS服务器以解析域,并将DNS A记录缓存在内存中。因此,即使DNS管理员对DNS A记录进行了分区,DNS A记录也不会在内存中更改,除非NGINX重新启动或重新加载。

    http {
      vhost_traffic_status_zone;
    
      resolver 10.10.10.53 valid=10s
    
      ...
    
      server {
    
          ...
    
          location /backend {
              set $backend_server elb.example.org;
              proxy_pass http://$backend_server;
          }
      }
    }
  • 计算域的各个IP的流量elb.example.org。如果elb.example.org更改了DNS A记录,将同时显示旧IP和新IP ::nogroups。与第一个上游组设置不同,即使DNS管理员对DNS A记录进行了分析,第二个设置也能正常工作。永久保留统计数据

    http {
      vhost_traffic_status_zone;
      vhost_traffic_status_dump /var/log/nginx/vts.db;
    
      ...
    
      server {
    
          ...
    
      }
    }

    vhost_traffic_status_filter_by_host on; 会更加不同的server_name进行统计参考 : https://github.com/vozlt/nginx-module-vts

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论