美文网首页
Nginx虚拟主机流量状态模块(nginx-module-vts

Nginx虚拟主机流量状态模块(nginx-module-vts

作者: tobrainto | 来源:发表于2021-02-17 22:35 被阅读0次

    装完NG,为了拿到各种状态指标,就要对NG做监控。
    Github 2.3k的开源项目nginx-module-vts没准真是你需求的。
    链接数,qps,1xx、2xx,、3xx、4xx、5xx的响应数,响应耗时,响应时间分布,访问用户国家分布;甚至是基于各种状态的流量控制统统能满足你的需求。
    nginx-module-vts具体怎么用?我们还是从官方文档开始撸吧,还是英文的,那就翻译一下吧。
    内容有点长,你看到的文档会四篇文档的方式分别出现。这里是第三篇,第一篇你可以点这里查看第二篇你可以点这里查看

    Nginx虚拟主机流量状态模块

    目录

    12、变量

    提供了以下嵌入式变量:

    • $vts_request_counter
      • 从客户端接收的客户端请求总数。
    • $vts_in_bytes
      • 从客户端接收的字节总数。
    • $vts_out_bytes
      • 发送到客户端的总字节数。
    • $vts_1xx_counter
      • 状态代码为1xx的响应数。
    • $vts_2xx_counter
      • 状态代码为2xx的响应数。
    • $vts_3xx_counter
      • 状态代码为3xx的响应数。
    • $vts_4xx_counter
      • 状态代码为4xx的响应数。
    • $vts_5xx_counter
      • 状态代码为5xx的响应数。
    • $vts_cache_miss_counter
      • 缓存未命中数。
    • $vts_cache_bypass_counter
      • 缓存旁路数。
    • $vts_cache_expired_counter
      • 缓存已过期数。
    • $vts_cache_stale_counter
      • 缓存失效的数量。
    • $vts_cache_updating_counter
      • 缓存更新的次数。
    • $vts_cache_revalidated_counter
      • 重新验证的缓存数。
    • $vts_cache_hit_counter
      • 缓存命中数。
    • $vts_cache_scarce_counter
      • 未达缓存要求的请求的数量。
    • $vts_request_time_counter
      • 请求处理时间的累计数量。
    • $vts_request_time
      • 请求处理的平均时间。

    13、流量限制

    它能够通过使用指令vhost_traffic_status_limit_traffic来限制每个主机的总流量。
    它还可以通过使用指令vhost_traffic_status_limit_traffic_by_set_key来限制所有流量。
    当超过限制时,服务器将返回503(服务暂时不可用)错误以响应请求。返回码可以更改。

    限制server的流量

    http {
    
        vhost_traffic_status_zone;
    
        ...
    
        server {
    
            server_name *.example.org;
    
            vhost_traffic_status_limit_traffic in:64G;
            vhost_traffic_status_limit_traffic out:1024G;
    
            ...
        }
    }
    
    • *.example.org上的入/出总流量分别限制为64G和1024G。如果启用vhost_traffic_status_filter_by_host指令,则每个域(domain)单独工作。

    限制筛选器(filter)的流量

    http {
        geoip_country /usr/share/GeoIP/GeoIP.dat;
    
        vhost_traffic_status_zone;
    
        ...
    
        server {
    
            server_name example.org;
    
            vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;
            vhost_traffic_status_limit_traffic_by_set_key FG@country::$server_name@US out:1024G;
            vhost_traffic_status_limit_traffic_by_set_key FG@country::$server_name@CN out:2048G;
    
            ...
    
        }
    }
    
    
    • 限制example.org上进入美国和中国的总流量分别为1024G和2048G。

    限制上游(upstream)流量

    http {
    
        vhost_traffic_status_zone;
    
        ...
    
        upstream backend {
            server 10.10.10.17:80;
            server 10.10.10.18:80;
        }
    
        server {
    
            server_name example.org;
    
            location /backend {
                vhost_traffic_status_limit_traffic_by_set_key UG@backend@10.10.10.17:80 in:512G;
                vhost_traffic_status_limit_traffic_by_set_key UG@backend@10.10.10.18:80 in:1024G;
                proxy_pass http://backend;
            }
    
            ...
    
        }
    }
    
    
    • 将进入example.org上游后端的每个对等端的总流量分别限制为512G和1024G。

    注意: 流量是累积传输或计数器,而不是带宽。

    14、用例

    它能够使用指令vhost_traffic_status_filter_by_set_key计算用户定义的单个统计信息。

    使用GeoIP计算各个国家的流量

    http {
        geoip_country /usr/share/GeoIP/GeoIP.dat;
    
        vhost_traffic_status_zone;
        vhost_traffic_status_filter_by_set_key $geoip_country_code country::*;
    
        ...
    
        server {
    
            ...
    
            vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;
    
            location /status {
                vhost_traffic_status_display;
                vhost_traffic_status_display_format html;
            }
        }
    }
    
    • 计算总服务器组(server groups)的各个国家/地区的流量。
    • 计算每个服务器组(server groups)的各个国家/地区的流量。

    基本上,国旗图像是HTML内置的。
    如果country字符串包含在vhost_traffic_status_filter_by_set_key指令的第二个参数group name中,则启用国家标志图像。

    单个存储容量(storage-volume)的流量计算

    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;
            }
        }
    }
    
    • 计算与location指令正则表达式匹配的单个存储容量的流量。

    计算单个用户代理(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_user_agent的流量计算

    为详细的http状态代码计算流量

    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;
            }
        }
    }
    
    • 按照详细的http status code计算流量

    注意:变量$status 在nginx-(1.3.2, 1.2.2)中可用。

    动态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;
            }
        }
    }
    
    • 计算域elb.example.org的各个IP的流量。
      如果elb.example.org有多个DNS A记录,将在filterZones中的显示所有ip。
      在上述设置中,NGINX启动或重新加载配置时,它会查询DNS服务器来解析域并将DNS A记录缓存在内存中。
      当DNS管理员更改了DNS A记录时内存中的对应记录并不会更新,除非NGINX 重新启动(restart)或重新加载(reload)。
    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;
            }
        }
    }
    
    • 计算域elb.example.org的各个IP的流量。
      如果elb.example.org的DNS A记录被更改,则::nogroups中将同时显示旧IP和新IP。
      与第一个上游组(upstream grou)设置不同,即使DNS管理员更改了DNS A记录,第二个设置也能正常工作。

    注意: 更多有关NGINX DNS的详细信息请参阅dns-service-discovery-nginx-plus

    排除状态页的流量计算

    http {
        vhost_traffic_status_zone;
    
        ...
    
        server {
    
            ...
    
            location /status {
                vhost_traffic_status_bypass_limit on;
                vhost_traffic_status_bypass_stats on;
                vhost_traffic_status_display;
                vhost_traffic_status_display_format html;
            }
        }
    }
    

    永久维护统计数据

    http {
        vhost_traffic_status_zone;
        vhost_traffic_status_dump /var/log/nginx/vts.db;
    
        ...
    
        server {
    
            ...
    
        }
    }
    
    • 使用vhost_traffic_status_dump指令可以永久地维护统计数据,即使系统已经重新启动或nginx已经重新启动。
      有关详细用法,请参见 vhost_traffic_status_dump

    15、自定义

    安装模块后进行自定义

    1. 您需要将{{uri}}字符串更改为status.template.html中的状态uri,如下所示:
    shell> vi share/status.template.html
    
    var vtsStatusURI = "yourStatusUri/format/json", vtsUpdateInterval = 1000;
    
    1. 然后,自定义status.template.html并将其复制到服务器(server)根目录,如下所示:
    shell> cp share/status.template.html /usr/share/nginx/html/status.html
    
    1. 配置 nginx.conf
       server {
           server_name example.org;
           root /usr/share/nginx/html;
    
           # Redirect requests for / to /status.html
           location = / {
               return 301 /status.html;
           }
    
           location = /status.html {}
    
           # Everything beginning /status (except for /status.html) is
           # processed by the status handler
           location /status {
               vhost_traffic_status_display;
               vhost_traffic_status_display_format json;
           }
       }
    
    
    1. 访问 html
    http://example.org/status.html
    

    在安装模块之前进行自定义

    1. 修改share/status.template.html(不要更改{{uri}}字符串)
    2. 重新创建ngx_http_vhost_traffic_status_module_html.h, 如下所示:
    shell> cd util
    shell> ./tplToDefine.sh ../share/status.template.html > ../src/ngx_http_vhost_traffic_status_module_html.h
    
    1. 通过添加--add-module=/path/to/nginx-module-vts,将模块添加到构建配置中。
    1. 构建nginx二进制文件。

    2. 安装nginx二进制文件。

    凋谢的不是花,萎蔫的是时间;星辰大海,点滴为途。

    相关文章

      网友评论

          本文标题:Nginx虚拟主机流量状态模块(nginx-module-vts

          本文链接:https://www.haomeiwen.com/subject/xmbwxltx.html