nginx-module-vts

基于 FFI 的 ngx_lua 模块新 Lua API

$ opm get BoTranVan/nginx-module-vts

Nginx 虚拟主机流量状态模块

[!许可证](https://github.com/vozlt/nginx-module-vts/blob/master/LICENSE)

Nginx 虚拟主机流量状态模块

版本

本文档描述了 nginx-module-vts `v0.1.15`,发布于 2017 年 4 月 3 日。

依赖项

兼容性

  • Nginx

    • 1.11.x (最后测试版本:1.11.10)

    • 1.10.x (最后测试版本:1.10.3)

    • 1.8.x (最后测试版本:1.8.0)

    • 1.6.x (最后测试版本:1.6.3)

    • 1.4.x (最后测试版本:1.4.7)

更早版本未经测试。

截图

!screenshot-vts-0

!screenshot-vts-1

安装

  1. 克隆 Git 仓库。

    ` shell> git clone git://github.com/vozlt/nginx-module-vts.git `

  1. 通过添加 `--add-module=/path/to/nginx-module-vts` 将模块添加到构建配置中。

  1. 构建 nginx 二进制文件。

  1. 安装 nginx 二进制文件。

概要

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

描述

这是一个 Nginx 模块,提供对虚拟主机状态信息的访问。它包含当前状态,例如服务器、上游和缓存。这类似于 nginx plus 的实时活动监控。内置的 html 也是从旧版本的演示页面中获取的。

首先,需要 `vhost_traffic_status_zone` 指令,然后如果设置了 `vhost_traffic_status_display` 指令,就可以通过以下方式访问:

  • /status/format/json

  • /status/format/html

  • /status/format/jsonp

  • /status/control

  • 如果请求 `/status/format/json`,将返回一个 JSON 文档,其中包含用于实时仪表板和第三方监控工具的当前活动数据。

  • 如果请求 `/status/format/html`,将返回一个内置的 HTML 实时仪表板,该仪表板内部请求 `/status/format/json`。

  • 如果请求 `/status/format/jsonp`,将返回一个包含当前活动数据的 JSONP 回调函数,用于实时仪表板和第三方监控工具。

  • 如果请求 `/status/control`,将在通过查询字符串重置或删除区域后返回一个 JSON 文档。查看 控制

JSON 文档包含以下内容:

    {
        "hostName": ...,
        "nginxVersion": ...,
        "loadMsec": ...,
        "nowMsec": ...,
        "connections": {
            "active":...,
            "reading":...,
            "writing":...,
            "waiting":...,
            "accepted":...,
            "handled":...,
            "requests":...
        },
        "sharedZones": {
            "name":...,
            "maxSize":...,
            "usedSize":...,
            "usedNode":...
        },
        "serverZones": {
            "...":{
                "requestCounter":...,
                "inBytes":...,
                "outBytes":...,
                "responses":{
                    "1xx":...,
                    "2xx":...,
                    "3xx":...,
                    "4xx":...,
                    "5xx":...,
                    "miss":...,
                    "bypass":...,
                    "expired":...,
                    "stale":...,
                    "updating":...,
                    "revalidated":...,
                    "hit":...,
                    "scarce":...
                },
                "requestMsec":...,
                "requestMsecs":{
                    "times":[...],
                    "msecs":[...]
                },
            }
            ...
        },
        "filterZones": {
            "...":{
                "...":{
                    "requestCounter":...,
                    "inBytes":...,
                    "outBytes":...,
                    "responses":{
                        "1xx":...,
                        "2xx":...,
                        "3xx":...,
                        "4xx":...,
                        "5xx":...,
                        "miss":...,
                        "bypass":...,
                        "expired":...,
                        "stale":...,
                        "updating":...,
                        "revalidated":...,
                        "hit":...,
                        "scarce":...
                    },
                    "requestMsec":...,
                    "requestMsecs":{
                        "times":[...],
                        "msecs":[...]
                    },
                },
                ...
            },
            ...
        },
        "upstreamZones": {
            "...":[
                {
                    "server":...,
                    "requestCounter":...,
                    "inBytes":...,
                    "outBytes":...,
                    "responses":{
                        "1xx":...,
                        "2xx":...,
                        "3xx":...,
                        "4xx":...,
                        "5xx":...
                    },
                    "requestMsec":...,
                    "requestMsecs":{
                        "times":[...],
                        "msecs":[...]
                    },
                    "responseMsec":...,
                    "responseMsecs":{
                        "times":[...],
                        "msecs":[...]
                    },
                    "weight":...,
                    "maxFails":...,
                    "failTimeout":...,
                    "backup":...,
                    "down":...
                }
                ...
            ],
            ...
        }
        "cacheZones": {
            "...":{
                "maxSize":...,
                "usedSize":...,
                "inBytes":...,
                "outBytes":...,
                "responses":{
                    "miss":...,
                    "bypass":...,
                    "expired":...,
                    "stale":...,
                    "updating":...,
                    "revalidated":...,
                    "hit":...,
                    "scarce":...
                }
            },
            ...
        }
    }
  • main

    • 基本版本,运行时间((nowMsec - loadMsec)/1000)

    • nowMsec,loadMsec 都是毫秒。

  • connections

    • 总连接数和请求数(与 NGINX 中的 stub_status_module 相同)

  • sharedZones

    • nginx-module-vts 中使用的共享内存信息。

  • serverZones

    • 每个服务器区域的流量(进/出)以及请求和响应计数和缓存命中率

    • 总流量(进/出)以及请求和响应计数(区域名称为 `*`)和命中率

  • filterZones

    • 每个服务器区域的流量(进/出)以及请求和响应计数和缓存命中率,通过 `vhost_traffic_status_filter_by_set_key` 指令过滤

    • 总流量(进/出)以及请求和响应计数(区域名称为 `*`)和命中率,通过 `vhost_traffic_status_filter_by_set_key` 指令过滤

  • upstreamZones

    • 每个上游组中每个服务器的流量(进/出)以及请求和响应计数

    • nginx.conf 中的当前设置(权重、最大失败次数、失败超时等)

  • cacheZones

    • 使用 proxy_cache 指令时,每个缓存区域的流量(进/出)以及大小(容量/使用)和命中率。

`vhost_traffic_status_display_format` 指令设置默认输出格式,可以是 json 或 html。(默认:json)

流量计算如下:

  • ServerZones

    • in += requested_bytes

    • out += sent_bytes

  • FilterZones

    • in += 通过过滤器请求的字节数

    • out += 通过过滤器发送的字节数

  • UpstreamZones

    • in += 通过 ServerZones 请求的字节数

    • out += 通过 ServerZones 发送的字节数

  • cacheZones

    • in += 通过 ServerZones 请求的字节数

    • out += 通过 ServerZones 发送的字节数

所有计算都在 Nginx 的日志处理阶段进行。内部重定向(X-Accel-Redirect 或 error_page)不计算在 UpstreamZones 中。

`注意事项:` 此模块依赖于 nginx 日志系统(NGX_HTTP_LOG_PHASE:nginx http 的最后一个阶段),因此流量在某些情况下可能与实际带宽流量不同。Websocket、取消的下载可能会导致不准确。模块的工作方式与 access_log 指令“on”或“off”无关。同样,此模块在“access_log off”上运行良好。使用多个域时,它设置为服务器名称指令的第一个域(左侧)。如果您不希望这样,请查看 vhost_traffic_status_filter_by_hostvhost_traffic_status_filter_by_set_key 指令。

查看以下模块以获取 `stream` 流量统计信息:

控制

可以通过查询字符串重置或删除流量区域。请求将返回一个 JSON 文档。

  • URI 语法

    • /{status_uri}*/control?cmd={command}*&group={group}*&zone={name}*

    http {
    
        geoip_country /usr/share/GeoIP/GeoIP.dat;
    
        vhost_traffic_status_zone;
        vhost_traffic_status_filter_by_set_key $geoip_country_code country::*;
    
        ...
    
        server {
    
            server_name example.org;
    
            ...
    
            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;
            }
        }
    }

如果按照上述方式设置,则控制 URI 类似于 `example.org/status/control`。

可用的请求参数如下:

  • cmd=\<status\|reset\|delete\>

    • status

      • 它将以 json 格式返回流量区域的状态,类似于 `status/format/json`。

    • reset

      • 它将重置流量区域,但不会删除共享内存中的节点。(= 初始化为 0)

    • delete

      • 它将删除共享内存中的流量区域。当重新请求时,它会重新创建。

  • group=\<server\|filter\|upstream@alone\|upstream@group\|cache\|*\>

    • server

    • filter

    • upstream@alone

    • upstream@group

    • cache

    • \*

  • zone=name

    • server

      • name

    • filter

      • filter_group@name

    • upstream@group

      • upstream_group@name

    • upstream@alone

      • @name

    • cache

      • name

要即时获取流量区域的状态

这与 `status/format/json` 类似,不同之处在于它可以获取每个区域。

要获取所有区域

  • 这与 `status/format/json` 完全相同。

    • /status/control?cmd=status&group=*

要获取组区域

  • mainZones

    • /status/control?cmd=status&group=server&zone=::main

  • serverZones

    • /status/control?cmd=status&group=server&zone=*

  • filterZones

    • /status/control?cmd=status&group=filter&zone=*

  • upstreamZones

    • /status/control?cmd=status&group=upstream@group&zone=*

  • upstreamZones::nogroups

    • /status/control?cmd=status&group=upstream@alone&zone=*

  • cacheZones

    • /status/control?cmd=status&group=cache&zone=*

mainZones 值是默认状态值,包括 `hostName`、`nginxVersion`、`loadMsec`、`nowMsec`、`connections`。

要获取每个区域

  • serverZones 中的单个区域

    • /status/control?cmd=status&group=server&zone=name*

  • filterZones 中的单个区域

    • /status/control?cmd=status&group=filter&zone=filter_group*@name*

  • upstreamZones 中的单个区域

    • /status/control?cmd=status&group=upstream@group&zone=upstream_group*@name*

  • upstreamZones::nogroups 中的单个区域

    • /status/control?cmd=status&group=upstream@alone&zone=name*

  • cacheZones 中的单个区域

    • /status/control?cmd=status&group=cache&zone=name*

要即时重置流量区域

它将指定区域的值重置为 0。

要重置所有区域

  • /status/control?cmd=reset&group=*

要重置组区域

  • serverZones

    • /status/control?cmd=reset&group=server&zone=*

  • filterZones

    • /status/control?cmd=reset&group=filter&zone=*

  • upstreamZones

    • /status/control?cmd=reset&group=upstream@group&zone=*

  • upstreamZones::nogroups

    • /status/control?cmd=reset&group=upstream@alone&zone=*

  • cacheZones

    • /status/control?cmd=reset&group=cache&zone=*

要重置每个区域

  • serverZones 中的单个区域

    • /status/control?cmd=reset&group=server&zone=name*

  • filterZones 中的单个区域

    • /status/control?cmd=reset&group=filter&zone=filter_group*@name*

  • upstreamZones 中的单个区域

    • /status/control?cmd=reset&group=upstream@group&zone=upstream_group*@name*

  • upstreamZones::nogroups 中的单个区域

    • /status/control?cmd=reset&group=upstream@alone&zone=name*

  • cacheZones 中的单个区域

    • /status/control?cmd=reset&group=cache&zone=name*

要即时删除流量区域

它将删除共享内存中的指定区域。

要删除所有区域

  • /status/control?cmd=delete&group=*

要删除组区域

  • serverZones

    • /status/control?cmd=delete&group=server&zone=*

  • filterZones

    • /status/control?cmd=delete&group=filter&zone=*

  • upstreamZones

    • /status/control?cmd=delete&group=upstream@group&zone=*

  • upstreamZones::nogroups

    • /status/control?cmd=delete&group=upstream@alone&zone=*

  • cacheZones

    • /status/control?cmd=delete&group=cache&zone=*

要删除每个区域

  • serverZones 中的单个区域

    • /status/control?cmd=delete&group=server&zone=name*

  • filterZones 中的单个区域

    • /status/control?cmd=delete&group=filter&zone=filter_group*@name*

  • upstreamZones 中的单个区域

    • /status/control?cmd=delete&group=upstream@group&zone=upstream_group*@name*

  • upstreamZones::nogroups 中的单个区域

    • /status/control?cmd=delete&group=upstream@alone&zone=name*

  • cacheZones 中的单个区域

    • /status/control?cmd=delete&group=cache&zone=name*

设置

可以使用 `vhost_traffic_status_set_by_filter` 指令分别获取 nginx 配置中的状态值。它可以获取几乎所有状态值,并将其存储在用户定义的变量中,该变量是第一个参数。

  • 指令语法

    • vhost_traffic_status_set_by_filter $variable group/zone/name

    http {
    
        geoip_country /usr/share/GeoIP/GeoIP.dat;
    
        vhost_traffic_status_zone;
        vhost_traffic_status_filter_by_set_key $geoip_country_code country::*;
    
        ...
        upstream backend {
            10.10.10.11:80;
            10.10.10.12:80;
        }
    
        server {
    
            server_name example.org;
    
            ...
    
            vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;
    
            vhost_traffic_status_set_by_filter $requestCounter server/example.org/requestCounter;
            vhost_traffic_status_set_by_filter $requestCounterKR filter/country::example.org@KR/requestCounter;
    
            location /backend {
                vhost_traffic_status_set_by_filter $requestCounterB1 upstream@group/backend@10.10.10.11:80/requestCounter;
                proxy_pass http://backend;
            }
        }
    }

上述设置如下:

  • $requestCounter

    • serverZones -> example.org -> requestCounter

  • $requestCounterKR

    • filterZones -> country::example.org -> KR -> requestCounter

  • $requestCounterB1

    • upstreamZones -> backend -> 10.0.10.11:80 -> requestCounter

请查看 vhost_traffic_status_set_by_filter 指令以了解详细的用法。

JSON

以下状态信息以 JSON 格式提供。

status 使用的 Json

/{status_uri}*/format/json

/{status_uri}*/control?cmd=status&...

  • hostName

    • 主机名。

  • nginxVersion

    • 提供的版本。

  • loadMsec

    • 加载进程时间,以毫秒为单位。

  • nowMsec

    • 当前时间,以毫秒为单位。

  • connections

    • active

      • 当前活动客户端连接数。

    • reading

      • 读取客户端连接的总数。

    • writing

      • 写入客户端连接的总数。

    • waiting

      • 等待客户端连接的总数。

    • accepted

      • 接受的客户端连接的总数。

    • handled

      • 处理的客户端连接的总数。

    • requests

      • 请求的客户端连接的总数。

  • sharedZones

    • name

      • 配置中指定的共享内存的名称。(默认:vhost_traffic_status

    • maxSize

      • 配置中指定的共享内存的最大尺寸限制。

    • usedSize

      • 共享内存的当前尺寸。

    • usedNode

      • 共享内存中使用的节点的当前数量。可以使用以下公式获得单个节点的近似尺寸:(usedSize / usedNode)

  • serverZones

    • requestCounter

      • 从客户端接收的客户端请求总数。

    • inBytes

      • 从客户端接收的字节总数。

    • outBytes

      • 发送给客户端的字节总数。

    • responses

      • 1xx、2xx、3xx、4xx、5xx

        • 状态代码为 1xx、2xx、3xx、4xx 和 5xx 的响应数。

      • miss

        • 缓存未命中数。

      • bypass

        • 缓存绕过数。

      • expired

        • 缓存过期数。

      • stale

        • 缓存陈旧数。

      • updating

        • 缓存更新数。

      • revalidated

        • 缓存重新验证数。

      • hit

        • 缓存命中数。

      • scarce

        • 缓存稀缺数。

    • requestMsec

      • 请求处理时间的平均值,以毫秒为单位。

    • requestMsecs

      • times

        • 请求处理时间,以毫秒为单位。

      • msecs

        • 请求处理时间,以毫秒为单位。

  • filterZones

    • 它提供与 `serverZones` 相同的字段,不同之处在于它包含组名称。

  • upstreamZones

    • server

      • 服务器的地址。

    • requestCounter

      • 转发到此服务器的客户端连接总数。

    • inBytes

      • 从此服务器接收的字节总数。

    • outBytes

      • 发送到此服务器的字节总数。

    • responses

      • 1xx、2xx、3xx、4xx、5xx

        • 状态代码为 1xx、2xx、3xx、4xx 和 5xx 的响应数。

    • requestMsec

      • 包括上游在内的请求处理时间的平均值,以毫秒为单位。

    • requestMsecs

      • times

        • 请求处理时间,以毫秒为单位。

      • msecs

        • 包括上游在内的请求处理时间,以毫秒为单位。

    • responseMsec

      • 仅上游响应处理时间的平均值,以毫秒为单位。

    • responseMsecs

      • times

        • 请求处理时间,以毫秒为单位。

      • msecs

        • 仅上游响应处理时间,以毫秒为单位。

    • weight

      • 服务器当前的weight设置。

    • maxFails

      • 服务器当前的max_fails设置。

    • failTimeout

      • 服务器当前的fail_timeout设置。

    • backup

      • 服务器当前的backup设置。

    • down

      • 服务器当前的down设置。

  • cacheZones

    • maxSize

      • 配置中指定的缓存最大大小限制。

    • usedSize

      • 当前缓存的大小。

    • inBytes

      • 从缓存接收的总字节数。

    • outBytes

      • 从缓存发送的总字节数。

    • responses

      • miss

        • 缓存未命中数。

      • bypass

        • 缓存绕过数。

      • expired

        • 缓存过期数。

      • stale

        • 缓存陈旧数。

      • updating

        • 缓存更新数。

      • revalidated

        • 缓存重新验证数。

      • hit

        • 缓存命中数。

      • scarce

        • 缓存稀缺数。

控制使用的Json

/{status_uri}*/control?cmd=reset&...

/{status_uri}*/control?cmd=delete&...

  • processingReturn

    • 结果为true或false。

  • processingCommandString

    • 请求的命令字符串。

  • processingGroupString

    • 请求的组字符串。

  • processingZoneString

    • 请求的区域字符串。

  • processingCounts

    • 实际处理数量。

变量

提供以下嵌入变量

  • $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

    • 请求处理时间的平均值。

限制

可以使用指令`vhost_traffic_status_limit_traffic`限制每个主机总流量。也可以使用指令`vhost_traffic_status_limit_traffic_by_set_key`限制所有流量。当流量超出限制时,服务器将在回复请求时返回 503 (服务暂时不可用) 错误。返回码可以更改。

限制服务器的流量

    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 指令,它将对每个域单独生效。

限制过滤器的流量

    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 的流量进入 US 和 CN 的总流量分别限制为 1024G 和 2048G。

限制上游的流量

    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(每个对等方)。

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

用例

可以使用指令 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;
            }
        }
    }
  • 计算所有服务器组的单个国家/地区的流量。

  • 计算每个服务器组的单个国家/地区的流量。

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

计算单个存储卷的流量

    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 指令的正则表达式匹配的单个存储卷的流量。

计算单个用户代理的流量

    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 的流量

计算动态 DNS 的流量

如果域有多个 DNS A 记录,可以使用过滤器功能或代理传递中的变量计算域的单个 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 记录,内存中的 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;
            }
        }
    }
  • 计算域 elb.example.org 的单个 IP 的流量。如果 elb.example.org 的 DNS A 记录发生更改,将在 ::nogroups 中显示旧 IP 和新 IP。与第一个上游组设置不同,第二个设置即使 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 {
    
            ...
    
        }
    }
  • 即使系统已重新启动或 nginx 已重新启动,vhost_traffic_status_dump 指令也会永久保留统计数据。有关详细用法,请参阅vhost_traffic_status_dump 指令。

定制

在安装模块后进行定制

  1. 您需要将 {{uri}} 字符串更改为 status.template.html 中的您的状态 uri,如下所示: ` shell> vi share/status.template.html ` ` var vtsStatusURI = "yourStatusUri/format/json", vtsUpdateInterval = 1000; `

  1. 然后,定制并复制 status.template.html 到服务器根目录,如下所示: ` shell> cp share/status.template.html /usr/share/nginx/html/status.html `

  1. 配置 nginx.conf `Nginx server { server_name example.org; root /usr/share/nginx/html;

    # 将对 / 的请求重定向到 /status.html location = / { return 301 /status.html; }

    location = /status.html {}

    # 所有以 /status 开头的请求(除了 /status.html)都由状态处理程序处理 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}} 字符串)

  1. 重新创建 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 二进制文件。

  1. 安装 nginx 二进制文件。

指令

vhost_traffic_status

| - | - | | --- | --- | | 语法 | vhost_traffic_status \<on\|off\> | | 默认值 | off | | 上下文 | http, server, location |

描述:启用或禁用模块工作。如果您设置了 vhost_traffic_status_zone 指令,则会自动启用。

vhost_traffic_status_zone

| - | - | | --- | --- | | 语法 | vhost_traffic_status_zone [shared:name:size] | | 默认值 | shared:vhost_traffic_status:1m | | 上下文 | http |

描述:为共享内存区域设置参数,该区域将保存各种键的状态。该缓存由所有工作进程共享。

vhost_traffic_status_dump

| - | - | | --- | --- | | 语法 | vhost_traffic_status_dump path [period] | | 默认值 | - | | 上下文 | http |

描述:启用统计数据转储和还原。path 是转储统计数据的路径(例如 /var/log/nginx/vts.db)。period 是备份周期时间(默认:60 秒)。如果 nginx 通过信号(SIGKILL)退出,则无论备份周期如何,都会立即备份。

vhost_traffic_status_display

| - | - | | --- | --- | | 语法 | vhost_traffic_status_display | | 默认值 | - | | 上下文 | http, server, location |

描述:启用或禁用模块显示处理程序。

vhost_traffic_status_display_format

| - | - | | --- | --- | | 语法 | vhost_traffic_status_display_format \<json\|html\|jsonp\> | | 默认值 | json | | 上下文 | http, server, location |

描述:设置显示处理程序的输出格式。如果您设置了 json,则将以 JSON 文档形式响应。如果您设置了 html,则将以 HTML 中的内置动态仪表板形式响应。如果您设置了 jsonp,则将以 JSONP 回调函数(默认:ngx_http_vhost_traffic_status_jsonp_callback)形式响应。

vhost_traffic_status_display_jsonp

| - | - | | --- | --- | | 语法 | vhost_traffic_status_display_jsonp callback | | 默认值 | ngx_http_vhost_traffic_status_jsonp_callback | | 上下文 | http, server, location |

描述:设置 JSONP 的回调名称。

vhost_traffic_status_display_sum_key

| - | - | | --- | --- | | 语法 | vhost_traffic_status_display_sum_key name | | 默认值 | * | | 上下文 | http, server, location |

描述:在 serverZones 字段的 JSON 中设置总计键字符串。默认总计键字符串为“*”。

vhost_traffic_status_filter

| - | - | | --- | --- | | 语法 | vhost_traffic_status_filter \<on\|off\> | | 默认值 | on | | 上下文 | http, server, location |

描述:启用或禁用过滤器功能。

vhost_traffic_status_filter_by_host

| - | - | | --- | --- | | 语法 | vhost_traffic_status_filter_by_host \<on\|off\> | | 默认值 | off | | 上下文 | http, server, location |

描述:根据 Host 标头字段启用或禁用键。如果您设置了 on 并且 nginx 的 server_name 指令设置了多个或以星号开头的通配符名称,例如“*.example.org”,并请求使用主机名(例如 (a|b|c).example.org 或 *.example.org)的服务器,则 json serverZones 将按如下方式打印

    server {
      server_name *.example.org;
      vhost_traffic_status_filter_by_host on;
    
      ...
    
    }


      ...
      "serverZones": {
          "a.example.org": {
          ...
          },
          "b.example.org": {
          ...
          },
          "c.example.org": {
          ...
          }
          ...
       },
       ...

它提供与设置 vhost_traffic_status_filter_by_set_key $host 相同的功能。

vhost_traffic_status_filter_by_set_key

| - | - | | --- | --- | | 语法 | vhost_traffic_status_filter_by_set_key key [name] | | 默认值 | - | | 上下文 | http, server, location |

描述:根据用户定义的变量启用键。key 是计算流量的键字符串。name 是计算流量的组字符串。keyname 可以包含变量,例如 $host、$server_name。如果指定了第二个参数 name,则 name 的组属于 filterZones。如果没有指定第二个参数 name,则 key 的组属于 serverZones。以下是如何使用 geoip 模块的示例

    server {
      server_name example.org;
      vhost_traffic_status_filter_by_set_key $geoip_country_code country::$server_name;
    
      ...
    
    }


      ...
      "serverZones": {
      ...
      },
      "filterZones": {
          "country::example.org": {
              "KR": {
                  "requestCounter":...,
                  "inBytes":...,
                  "outBytes":...,
                  "responses":{
                      "1xx":...,
                      "2xx":...,
                      "3xx":...,
                      "4xx":...,
                      "5xx":...,
                      "miss":...,
                      "bypass":...,
                      "expired":...,
                      "stale":...,
                      "updating":...,
                      "revalidated":...,
                      "hit":...,
                      "scarce":...
                  },
                  "requestMsec":...
              },
              "US": {
              ...
              },
              ...
          },
          ...
      },
      ...
    

vhost_traffic_status_filter_check_duplicate

| - | - | | --- | --- | | 语法 | vhost_traffic_status_filter_check_duplicate \<on\|off\> | | 默认值 | on | | 上下文 | http, server, location |

描述:启用或禁用 vhost_traffic_status_filter_by_set_key 的重复数据删除。如果启用此选项,则每个指令(http、server、location)中重复值(key + name)只处理一个。

vhost_traffic_status_limit

| - | - | | --- | --- | | 语法 | vhost_traffic_status_limit \<on\|off\> | | 默认值 | on | | 上下文 | http, server, location |

描述:启用或禁用限制功能。

vhost_traffic_status_limit_traffic

| - | - | | --- | --- | | 语法 | vhost_traffic_status_limit_traffic member:size [code] | | 默认值 | - | | 上下文 | http, server, location |

描述:为指定的 member 启用流量限制。member 是限制流量的成员字符串。size 是限制流量的大小(k/m/g)。code 是对拒绝请求的响应中返回的代码(默认:503)。

可用的 member* 字符串如下:

  • request

    • 从客户端接收的客户端请求总数。

  • in

    • 从客户端接收的字节总数。

  • out

    • 发送给客户端的字节总数。

  • 1xx

    • 状态代码为1xx的响应数量。

  • 2xx

    • 状态代码为2xx的响应数量。

  • 3xx

    • 状态代码为3xx的响应数量。

  • 4xx

    • 状态代码为4xx的响应数量。

  • 5xx

    • 状态代码为5xx的响应数量。

  • cache_miss

    • 缓存未命中数。

  • cache_bypass

    • 缓存绕过数。

  • cache_expired

    • 缓存过期数。

  • cache_stale

    • 缓存陈旧数。

  • cache_updating

    • 缓存更新数。

  • cache_revalidated

    • 缓存重新验证数。

  • cache_hit

    • 缓存命中数。

  • cache_scarce

    • 缓存稀缺数。

vhost_traffic_status_limit_traffic_by_set_key

| - | - | | --- | --- | | 语法 | vhost_traffic_status_limit_traffic_by_set_key key member:size [code] | | 默认值 | - | | 上下文 | http, server, location|

描述: 为指定的成员启用流量限制。是一个用于限制流量的键字符串。成员是一个用于限制流量的成员字符串。大小是一个用于限制流量的大小(k/m/g)。代码是在拒绝请求时返回的代码。(默认:503)

* 语法如下:

  • *@[子组*@]名称*

可用的* 字符串如下:

  • NO

    • 服务器组。

  • UA

    • 上游单独组。

  • UG

    • 上游组。(使用子组*)

  • CC

    • 缓存组。

  • FG

    • 过滤器组。(使用子组*)

可用的 member* 字符串如下:

  • request

    • 从客户端接收的客户端请求总数。

  • in

    • 从客户端接收的字节总数。

  • out

    • 发送给客户端的字节总数。

  • 1xx

    • 状态代码为1xx的响应数量。

  • 2xx

    • 状态代码为2xx的响应数量。

  • 3xx

    • 状态代码为3xx的响应数量。

  • 4xx

    • 状态代码为4xx的响应数量。

  • 5xx

    • 状态代码为5xx的响应数量。

  • cache_miss

    • 缓存未命中数。

  • cache_bypass

    • 缓存绕过数。

  • cache_expired

    • 缓存过期数。

  • cache_stale

    • 缓存陈旧数。

  • cache_updating

    • 缓存更新数。

  • cache_revalidated

    • 缓存重新验证数。

  • cache_hit

    • 缓存命中数。

  • cache_scarce

    • 缓存稀缺数。

成员vhost_traffic_status_limit_traffic指令相同。

vhost_traffic_status_limit_check_duplicate

| - | - | | --- | --- | | 语法 | vhost_traffic_status_limit_check_duplicate \<on\|off\> | | 默认 | on | | 上下文 | http, server, location |

描述: 启用或禁用对 vhost_traffic_status_limit_by_set_key 的重复数据删除。如果启用了此选项,则在每个指令(http、server、location)中只处理一个重复值(成员 | + 成员)。

vhost_traffic_status_set_by_filter

| - | - | | --- | --- | | 语法 | vhost_traffic_status_set_by_filter $变量 /区域/名称 | | 默认 | - | | 上下文 | http, server, location, if |

描述: 获取存储在共享内存中的指定状态值。它可以获取几乎所有状态值,获取的值存储在第一个参数的$变量中。

    • server

    • filter

    • upstream@alone

    • upstream@group

    • cache

  • 区域

    • server

      • name

    • filter

      • filter_group@name

    • upstream@group

      • upstream_group@name

    • upstream@alone

      • @name

    • cache

      • name

  • name

    • requestCounter

      • 从客户端接收的客户端请求总数。

    • requestMsec

      • 请求处理时间的平均值,以毫秒为单位。

    • responseMsec

      • 仅上游响应处理时间的平均值,以毫秒为单位。

    • inBytes

      • 从客户端接收的字节总数。

    • outBytes

      • 发送给客户端的字节总数。

    • 1xx、2xx、3xx、4xx、5xx

      • 状态代码为 1xx、2xx、3xx、4xx 和 5xx 的响应数。

    • cacheMaxSize

      • 配置中指定的缓存最大大小限制。

    • cacheUsedSize

      • 当前缓存的大小。

    • cacheMiss

      • 缓存未命中数。

    • cacheBypass

      • 缓存绕过数。

    • cacheExpired

      • 缓存过期数。

    • cacheStale

      • 缓存陈旧数。

    • cacheUpdating

      • 缓存更新数。

    • cacheRevalidated

      • 缓存重新验证数。

    • cacheHit

      • 缓存命中数。

    • cacheScarce

      • 缓存稀缺数。

    • weight

      • 服务器的当前权重设置。

    • maxFails

      • 服务器的当前max_fails设置。

    • failTimeout

      • 服务器的当前fail_timeout设置。

    • backup

      • 服务器的当前备份设置。(0\|1)

    • down

      • 服务器的当前停用设置。(0\|1)

注意事项: 名称区分大小写。所有返回值都采用整数类型。

例如

  • serverZones中的requestCounter

    • vhost_traffic_status_set_by_filter $requestCounter server/example.org/requestCounter

  • filterZones中的requestCounter

    • vhost_traffic_status_set_by_filter $requestCounter filter/country::example.org@KR/requestCounter

  • upstreamZones中的requestCounter

    • vhost_traffic_status_set_by_filter $requestCounter upstream@group/backend@10.10.10.11:80/requestCounter

  • upstreamZones::nogroups中的requestCounter

    • vhost_traffic_status_set_by_filter $requestCounter upstream@alone/10.10.10.11:80/requestCounter

  • cacheZones中的cacheHit

    • vhost_traffic_status_set_by_filter $cacheHit cache/my_cache_name/cacheHit

vhost_traffic_status_average_method

| - | - | | --- | --- | | 语法 | vhost_traffic_status_average_method \<AMM\|WMA\> [周期] | | 默认 | AMM 60s | | 上下文 | http, server, location |

描述: 设置用于计算响应处理时间的平均值的公式方法。周期是用于平均值计算的值的有效时间。(默认:60s)如果周期设置为 0,则忽略有效时间。在这种情况下,即使没有请求,并且在时间过去后,也会显示最后一个平均值。相应的 JSON 中的值是requestMsecresponseMsec

vhost_traffic_status_bypass_limit

| - | - | | --- | --- | | 语法 | vhost_traffic_status_bypass_limit \<on\|off\> | | 默认 | off | | 上下文 | http, server, location |

描述: 启用或禁用绕过vhost_traffic_status_limit指令。如果启用了此选项,则会绕过限制功能。这在您想连接到状态网页(如/status)时非常有用,而不管vhost_traffic_status_limit指令是什么,如下所示

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

vhost_traffic_status_bypass_stats

| - | - | | --- | --- | | 语法 | vhost_traffic_status_bypass_stats \<on\|off\> | | 默认 | off | | 上下文 | http, server, location |

描述: 启用或禁用绕过vhost_traffic_status。如果启用了此选项,则会绕过流量状态统计功能。换句话说,它不包括在流量状态统计中。这在您想忽略状态网页(如/status)中的请求时非常有用,如下所示

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

另请参见

待办事项

  • 添加对实现数据恢复的支持。

捐赠

[!许可证](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=PWWSYKQ9VKH38&lc=KR&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted)

作者

YoungJoo.Kim(김영주) [<vozltx@gmail.com>]

POD 错误

嗨!上面的文档存在一些编码错误,以下是解释:

大约第 987 行

未终止的 I<...> 序列

大约第 1029 行

未终止的 I<...> 序列

大约第 1043 行

未终止的 I<...> 序列

大约第 1166 行

未终止的 I<...> 序列

大约第 1208 行

未终止的 I<...> 序列

大约第 1222 行

未终止的 I<...> 序列

大约第 1345 行

未终止的 I<...> 序列

大约第 1387 行

未终止的 I<...> 序列

大约第 1401 行

未终止的 I<...> 序列

大约第 1531 行

未终止的 I<...> 序列

大约第 1533 行

未终止的 I<...> 序列

大约第 2481 行

未终止的 I<...> 序列

大约第 2483 行

未终止的 I<...> 序列

大约第 3627 行

未终止的 I<...> 序列

大约第 3875 行

未终止的 I<...> 序列

大约第 3880 行

未终止的 I<...> 序列

大约第 3887 行

未终止的 I<...> 序列

大约第 3927 行

未终止的 I<...> 序列

大约第 3955 行

未终止的 I<...> 序列

大约第 3965 行

未终止的 I<...> 序列

作者

BoTranVan (r3ministry),r3s3arch3r

许可证

3bsd

依赖项

nginx >= 1.11.x, openresty

版本