t1k:用于 Chaitin/SafeLine WAF 的 T1K 协议的 Lua 实现
安装
如果您尚未设置 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-t1k
CentOS/RHEL 8+、Fedora Linux、Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install lua5.1-resty-t1k
要将此 Lua 库与 NGINX 一起使用,请确保已安装 nginx-module-lua。
本文档描述了 lua-resty-t1k v1.2.1,该版本发布于 2026 年 8 月 5 日。
概要
location / {
access_by_lua_block {
local t1k = require "resty.t1k"
local t = {
mode = "block", -- block 或 monitor 或 off,默认为 off
host = "unix:/workdir/snserver.sock", -- 必填,SafeLine WAF 检测服务主机,支持 Unix 域套接字、IP 或域名,字符串类型
port = 8000, -- 当 host 为 IP 或域名时必填,SafeLine WAF 检测服务端口,整数类型
connect_timeout = 1000, -- 连接超时时间,单位毫秒,整数类型,默认为 1 秒(1000 毫秒)
send_timeout = 1000, -- 发送超时时间,单位毫秒,整数类型,默认为 1 秒(1000 毫秒)
read_timeout = 1000, -- 读取超时时间,单位毫秒,整数类型,默认为 1 秒(1000 毫秒)
req_body_size = 1024, -- 请求体大小,单位 KB,整数类型,默认为 1MB(1024KB)
keepalive_size = 256, -- 与 SafeLine WAF 检测服务的最大并发空闲连接数,整数类型,默认为 256
keepalive_timeout = 60000, -- 空闲连接超时时间,单位毫秒,整数类型,默认为 60 秒(60000 毫秒)
remote_addr = "http_x_forwarded_for: 1", -- 来自 ngx.var.VARIABLE 的远程地址,字符串类型,默认为来自 ngx.var.remote_addr
log_resp = false, -- 将响应上报给 SafeLine WAF 检测服务,布尔类型,默认为 false
resp_body_size = 4, -- 上报的响应体大小,单位 KB,整数类型,默认为 4KB,仅在 log_resp 为 true 时使用
extra_ignored_content_types = "text/csv", -- 需要跳过的额外响应内容类型,逗号分隔,字符串类型,仅在 log_resp 为 true 时使用
}
local ok, err, _ = t1k.do_access(t, true)
if not ok then
ngx.log(ngx.ERR, err)
end
}
header_filter_by_lua_block {
local t1k = require "resty.t1k"
t1k.do_header_filter()
}
-- 以下两个代码块仅在 log_resp 为 true 时需要
body_filter_by_lua_block {
local t1k = require "resty.t1k"
t1k.do_body_filter()
}
log_by_lua_block {
local t1k = require "resty.t1k"
t1k.do_log()
}
}
响应日志记录
当启用 log_resp 时,在请求完成后,响应状态行、响应头以及最多 resp_body_size KB 的响应体将被上报给 SafeLine WAF 检测服务。该上报通过 ngx.timer 发送,因此不会增加响应本身的延迟。
当请求已被拦截,或响应的 Content-Type 匹配内置的忽略类型(音频、视频、字体、图像及其他二进制媒体类型)时,响应将被跳过。使用 extra_ignored_content_types 可以跳过额外的内容类型。
Lua Resty T1K 与 C T1K 的对比
C T1K 作为 SafeLine 企业版的一部分,是一种使用 C 语言编写的高性能部署模式。它兼容所有版本的 Nginx,无需通过 OpenResty(lua_nginx_module)进行部署。
| Lua Resty T1K | C T1K | |
|---|---|---|
| 请求检测 | ✅ | ✅ |
| 响应检测 | ❌ | ✅ |
| 健康检查* | ❌ | ✅ |
| Cookie 防护 | ❌ | ✅ |
| 机器人防护 | ❌ | ✅ |
| 代理端统计 | ❌ | ✅ |
* APISIX 为 chaitin-waf 插件实现了健康检查功能。更多信息请参阅 chaitin-waf 文档。
GitHub
您可以在 nginx-module-t1k 的 GitHub 仓库中找到有关此模块的更多配置技巧和文档。