stats: 是一个基于 nginx-module-lua 的 NGINX 统计模块,统计的键和值是可配置的,可以使用 NGINX 核心的变量和本模块的变量。统计结果存储在 mongodb 中。
安装
如果您尚未设置 RPM 仓库订阅,请 注册。然后您可以继续以下步骤。
CentOS/RHEL 7 或 Amazon Linux 2
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install lua-resty-stats
CentOS/RHEL 8+、Fedora Linux、Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install lua5.1-resty-stats
要在 NGINX 中使用此 Lua 库,请确保已安装 nginx-module-lua。
本文档描述了 lua-resty-stats v1.0.3,于 2020 年 11 月 28 日发布。
#set ngx_lua's environment variable:
# init the lua-resty-stats
init_worker_by_lua '
local stats = require("resty.stats")
-- add the default stats that named "stats_host"
stats.add_def_stats()
-- the general stats"s config
local update = {["$inc"]= {count=1, ["hour_cnt.$hour"]=1, ["status.$status"]=1,
["req_time.all"]="$request_time", ["req_time.$hour"]="$request_time"}}
-- stats by uri
stats.add_stats_config("stats_uri",
{selector={date="$date",key="$uri"}, update=update,
indexes={{keys={'date', 'key'}, options={unique=true}},{keys={'key'}, options={}}} })
-- stats by arg
stats.add_stats_config("stats_arg",
{selector={date="$date",key="$arg_client_type"}, update=update,
indexes={{keys={'date', 'key'}, options={unique=true}},{keys={'key'}, options={}}} })
-- stats by uri and args
stats.add_stats_config("stats_uri_arg",
{selector={date="$date",key="$uri?$arg_from"}, update=update,
indexes={{keys={'date', 'key'}, options={unique=true}},{keys={'key'}, options={}}} })
-- stats by http request header
stats.add_stats_config("stats_header_in",
{selector={date="$date",key="city:$http_city"}, update=update,
indexes={{keys={'date', 'key'}, options={unique=true}},{keys={'key'}, options={}}} })
-- stats by http response header
stats.add_stats_config("stats_header_out",
{selector={date="$date",key="cache:$sent_http_cache"}, update=update,
indexes={{keys={'date', 'key'}, options={unique=true}},{keys={'key'}, options={}}} })
local mongo_cfg = {host="192.168.1.201", port=27017, dbname="ngx_stats"}
local flush_interval = 2 -- second
local retry_interval = 0.2 -- second
-- init stats and start flush timer.
stats.init(mongo_cfg, flush_interval, retry_interval)
';
server {
listen 80;
server_name localhost;
location /byuri {
echo "byuri: $uri";
log_by_lua '
local stats = require("resty.stats")
stats.log("stats_uri")
stats.log("stats_host")
';
}
location /byarg {
echo_sleep 0.005;
echo "login $args";
log_by_lua '
local stats = require("resty.stats")
stats.log("stats_arg")
';
}
location /byarg/404 {
request_stats statby_arg "clitype:$arg_client_type";
return 404;
log_by_lua '
local stats = require("resty.stats")
stats.log("stats_arg")
';
}
location /byuriarg {
echo "$uri?$args";
log_by_lua '
local stats = require("resty.stats")
stats.log("stats_uri_arg")
';
}
location /byhttpheaderin {
echo "city: $http_city";
log_by_lua '
local stats = require("resty.stats")
stats.log("stats_header_in")
';
}
location /byhttpheaderout/ {
proxy_pass http://127.0.0.1:82;
log_by_lua '
local stats = require("resty.stats")
stats.log("stats_header_out")
';
}
}
server {
listen 82;
server_name localhost;
location /byhttpheaderout/hit {
add_header cache hit;
echo "cache: hit";
}
location /byhttpheaderout/miss {
add_header cache miss;
echo "cache: miss";
}
}
server {
listen 2000;
server_name localhost;
location /stats {
set $template_root /path/to/lua-resty-stats/view;
content_by_lua_file '/path/to/lua-resty-stats/view/main.lua';
}
}
变量
- nginx_core 模块支持的变量: http://nginx.org/en/docs/http/ngx_http_core_module.html#variables
- 本模块变量
- date: 当前日期,格式为: 1970-09-28
- time: 当前时间,格式为: 12:00:00
- year: 当前年份
- month: 当前月份
- day: 当前日期
- hour: 当前小时
- minute: 当前分钟
- second: 当前秒
方法
要加载此库,
您需要在 ngx_lua 的 lua_package_path 指令中指定此库的路径。例如:
http {
}
您可以使用 require 将库加载到本地 Lua 变量中:
local stats = require("resty.stats")
add_def_stats
语法: stats.add_def_stats()
添加预定义的统计配置,包括:
stats_name: stats_host
stats_config:
{
selector={date='$date',key='$host'},
update={['$inc']= {count=1, ['hour_cnt.$hour']=1, ['status.$status']=1,
['req_time.all']="$request_time", ['req_time.$hour']="$request_time"}},
indexes={
{keys={'date', 'key'}, options={unique=true}},
{keys={'key'}, options={}}
},
}
}
add_stats_config
语法: stats.add_stats_config(stats_name, stats_config)
添加一个自定义统计配置项,包含 stats_name 和 stats 配置。
* stats_name 是统计的名称,也是 mongodb 表的名称。
该名称将在调用 stats.log(stats_name) 方法时使用。
* stats_config 用于定义统计的值。
stats_config 是一个包含一些字段的表:
* selector 一个 mongodb 查询语句。例如: {date="$date",key="$host"}
* update 一个 mongodb 更新语句。例如: {["$inc"]= {count=1, ["hour_cnt.$hour"]=1, ["status.$status"]=1,
["req_time.all"]="$request_time", ["req_time.$hour"]="$request_time"}}
* indexes 一个包含所有索引字段的表。
selector 和 update 配置可以使用 变量。
请注意,"$inc" 不是 NGINX 变量,它是 mongodb 的操作符。
init
语法: stats.init(mongo_cfg, flush_interval, retry_interval)
初始化统计库。
* mongo_cfg mongodb 配置,包含字段:
* host mongodb 的主机
* port mongodb 的端口
* dbname mongodb 的数据库名称。
* flush_interval 将数据刷新到 mongodb 的时间间隔,时间单位为秒。
* retry_interval 刷新错误时的重试时间间隔,时间单位为秒。
log
语法: stats.log(stats_name)
在日志阶段收集指定的(通过 stats_name)统计信息。
* stats_name 是通过 stats.add_stats_config 添加的一个统计名称。
如果 stats_name 为 nil,日志方法将收集所有已配置的统计信息。
简单查询和 API
lua-resty-stats 提供了一个简单的查询页面和 API 接口,可以按照以下步骤使用: * 向 nginx.conf 添加位置配置
location /stats {
set $template_root /path/to/lua-resty-stats/view;
content_by_lua_file '/path/to/lua-resty-stats/view/main.lua';
}
- 访问查询页面。例如
http://192.168.1.xxx/stats:

- 访问 API:
## 按日期
curl http://127.0.0.1:8020/stats/api?table=stats_uri&date=2020-02-20&limit=100
## 按日期,今天
curl http://127.0.0.1:8020/stats/api?table=stats_uri&date=today&limit=10
## 按键(日期参数被忽略)
curl http://127.0.0.1:8020/stats/api?table=stats_uri&key=/path/to/uri
- API 响应将类似于以下内容:
{
"stats": [
{
"hour_cnt": {
"19": 24
},
"count": 24,
"status": {
"200": 24
},
"total": 24,
"req_time": {
"19": 13.262,
"all": 13.262
},
"percent": 100,
"key": "/path/to/uri",
"date": "2020-09-24"
}
]
}
如果您在更新中配置了一些其他字段,这将会有所不同
简单演示
您可以使用 include 指令将其包含在 nginx.conf 中。例如:
include /path/to/simple_stats.conf;
GitHub
您可以在 nginx-module-stats 的 GitHub 仓库 中找到此模块的其他配置提示和文档。