安全与加速 规则表达式指南
本文档整理自 NamesLink 安全与加速(Security Acceleration)帮助中心,用于指导用户编写和 AI 生成 安全与加速 规则表达式。
1. 规则表达式概述
安全与加速 规则表达式用于根据请求特征匹配流量并实施差异化策略。规则由匹配条件和执行动作两部分组成:当用户请求命中规则条件时,规则上配置的功能优先生效。
使用限制
- 单条规则配置(规则条件 + 功能配置)的长度限制为 4K 字符。
- 站点维度上所有配置信息(全局配置、规则配置、证书配置)总大小不超过 512K 字符(安全防护中的规则配置除外)。
- 规则名称长度限制为 128 字符。
- 单条规则的内嵌子条件数量与套餐类型相关,企业版最多支持 20 条子条件。
2. 表达式基本结构
单条表达式
单条表达式由匹配字段、匹配运算符、匹配值组成:
( <匹配字段> <匹配运算符> <匹配值> )
示例:
(http.host eq "www.example.com")
组合表达式
支持使用逻辑运算符组合多条子条件:
- 并且(
and):请求需同时满足多个条件。 - 或者(
or):请求只需满足多个条件中的一个。
示例:
(http.host eq "www.example.com" and ip.geoip.country eq "CN")
嵌套层级
安全与加速 规则支持在或关系中嵌套并且关系,最大嵌套深度为 2 层。使用括号 () 控制优先级:
(http.host eq "www.example.com")
or
(http.host eq "blog.example.com" and not (ip.geoip.country eq "CN"))
or
(http.host eq "login.example.com")
全局匹配
当用户希望规则应用于所有请求时,可直接使用 true:
true
3. 匹配字段
安全与加速 匹配字段分为三类:
- 标准字段:HTTP、IP、TLS 等标准协议中的属性。
- 扩展字段:安全与加速 对请求/响应进行特定运算后得到的计算值。
- 原始字段:安全与加速 保留的客户端请求原始属性值。
3.1 标准字段
| 字段 | 类型 | 说明 | 示例 |
|---|---|---|---|
http.host | String | 请求主机名 | www.example.com |
http.request.uri | String | URI(路径 + 查询串) | /image/cat.jpg?width=400 |
http.request.uri.path | String | URI 路径 | /image/cat.jpg |
http.request.uri.path.extension | String | 文件扩展名 | jpg |
http.request.uri.query | String | 查询字符串 | width=400&height=300 |
http.request.uri.args | Map<Array<String>> | 查询参数映射 | {"format":["webp"]} |
http.request.full_uri | String | 完整 URI(含协议、主机名) | https://www.example.com/image/cat.jpg |
http.request.method | String | HTTP 方法 | GET |
http.request.headers | Object | 请求头映射(键为小写) | {"content-type":["application/json"]} |
http.request.cookies | String | Cookie 映射 | {"sessionid":["330668"]} |
http.cookie | String | Cookie 标头原始值 | sessionid=330688;userid=abc123 |
http.referer | String | Referer 标头 | http://www.example.com/index |
http.user_agent | String | User-Agent 标头 | Mozilla/5.0 |
http.request.body.form | Map<Array<String>> | form 格式请求体 | {"username":["admin"]} |
http.request.body.mime | String | 请求体 MIME 类型 | application/json |
http.request.body.raw | String | 原始请求体 | ABC123 |
http.request.timestamp.sec | Integer | 安全与加速 节点收到请求的 Unix 时间戳(秒) | 1735019278 |
http.request.version | String | HTTP 协议版本 | HTTP/1.1 |
http.x_forwarded_for | String | X-Forwarded-For 标头 | 192.168.0.1, 10.10.0.1 |
ip.src | IP address | 客户端源 IP | 192.0.2.1 |
ip.src.isp | String | 运营商编码 | 100017(中国电信) |
ip.src.version | String | IP 协议版本 | IPv4 / IPv6 |
ip.src.region_code | String | 负载均衡器所在区域 | EAS |
ip.src.subdivision_1_iso_code | String | 一级行政区划 | CN-ZJ |
ip.src.city | String | 城市代码 | 310000 |
ip.src.city_name | String | 城市名称 | hangzhou |
ip.src.lon | Float | 经度 | 120.12 |
ip.src.lat | Float | 纬度 | 30.16 |
ip.src.postal_code | String | 邮政编码 | 310000 |
ip.src.timezone | String | 时区 | UTC+8 |
ip.geoip.asnum | Integer | ASN 编号 | 37963 |
ip.geoip.continent | String | 大洲编码 | AS(亚洲) |
ip.geoip.country | String | 国家/地区编码 | CN |
ssl | Boolean | 是否使用 HTTPS | true |
国家/地区常用编码
| 编码 | 含义 |
|---|---|
CN | 中国内地 |
US | 美国 |
HK | 中国香港 |
TW | 中国台湾 |
JP | 日本 |
SG | 新加坡 |
GB | 英国 |
DE | 德国 |
FR | 法国 |
KR | 韩国 |
大洲编码
| 编码 | 大洲 |
|---|---|
AF | 非洲 |
AN | 南极洲 |
AS | 亚洲 |
EU | 欧洲 |
NA | 北美洲 |
OC | 大洋洲 |
SA | 南美洲 |
3.2 扩展字段
| 字段 | 类型 | 说明 |
|---|---|---|
ali.ja3_hash | String | JA3 TLS 指纹 |
ali.ja4 | String | JA4 TLS 指纹 |
ali.js_detection.passed | Boolean | 是否已通过 JavaScript 检测 |
ali.static_resource | Boolean | 是否为静态请求 |
ali.tls_client_auth.cert_verified | Boolean | 客户端证书是否已验证 |
ali.tls_hash | String | TLS 指纹哈希 |
ali.site.name | String | 安全与加速 站点名称 |
3.3 原始字段
| 字段 | 类型 | 说明 |
|---|---|---|
http.request.body.raw | String | HTTP 请求原始正文 |
4. 运算符
4.1 比较运算符
| 运算符 | 名称 | 值类型 | 示例 |
|---|---|---|---|
eq | 等于 | String / int / Boolean / IP | (http.host eq "www.example.com") |
ne | 不等于 | String / int / Boolean / IP | (http.host ne "www.example.com") |
contains | 包含 | String | (http.host contains "example.com") |
not ... contains | 不包含 | String | (not http.host contains "example.com") |
matches | 与正则表达式匹配 | String | `(http.host matches "(www\ |
not ... matches | 与正则表达式不匹配 | String | `(not http.host matches "(www\ |
in | 等于其中任意一个 | Array | (http.host in {"www.example.com" "blog.example.com"}) |
not ... in | 不等于其中任意一个 | Array | (not http.host in {"www.example.com" "blog.example.com"}) |
starts_with | 开头为 | String | (starts_with(http.host, "blog")) |
not starts_with | 开头不为 | String | (not starts_with(http.host, "blog")) |
ends_with | 结尾为 | String | (ends_with(http.host, "cn")) |
not ends_with | 结尾不是 | String | (not ends_with(http.host, "cn")) |
lt | 小于 | int | (ip.geoip.asnum lt 45104) |
le | 小于或等于 | int | (ip.geoip.asnum le 45104) |
gt | 大于 | int | (ip.geoip.asnum gt 45104) |
ge | 大于或等于 | int | (ip.geoip.asnum ge 45104) |
len eq | 长度等于 | String | (len(http.request.cookies["session"]) eq 330688) |
len gt | 长度大于 | String | (len(http.request.cookies["session"]) gt 330688) |
len lt | 长度小于 | String | (len(http.request.cookies["session"]) lt 330688) |
exists | 存在 | String | (exists(http.request.headers["user-agent"])) |
not exists | 不存在 | String | (not exists(http.request.headers["user-agent"])) |
lower | 不区分大小写 | String | (lower(http.request.uri)) |
说明:
matches(正则)仅高级版和企业版套餐支持。
4.2 逻辑运算符
| 运算符 | 名称 | 示例 |
|---|---|---|
not | 逻辑非 | not (http.host eq "www.example.com") |
and | 逻辑且 | (http.host eq "www.example.com" and ip.geoip.country eq "CN") |
or | 逻辑或 | (http.host eq "www.example.com") or (ip.geoip.country eq "CN") |
4.3 数组与列表引用
in 运算符使用花括号 {},元素之间用空格分隔:
(http.host in {"www.example.com" "blog.example.com" "shop.example.com"})
也可以引用命名列表(需在 安全与加速 控制台中预先创建):
(ip.src in $<LIST_NAME>)
5. 匹配值
5.1 字符串
字符串使用双引号 " 或单引号 ' 包裹;特殊字符(如反斜杠 \)需要转义。
(http.host eq "www.example.com")
(http.request.uri contains "/api/v1/")
5.2 布尔值
布尔值直接使用 true 或 false,不加引号。
(ssl eq true)
5.3 数字
支持整数和浮点数。
(ip.geoip.asnum ge 45104)
5.4 数组
数组使用方括号 [],元素之间用逗号分隔。但 安全与加速 规则表达式中的集合匹配优先使用 {} 空格分隔语法。
5.5 空值
部分字段允许配置空字符串,需结合具体运算符使用。http.request.uri.args 字段仅在 eq、ne、contains、matches 运算符下支持空值。
6. 转换函数
安全与加速 规则语言提供转换函数,用于操作和验证表达式中的值。
| 函数 | 说明 | 格式 | 示例 |
|---|---|---|---|
concat | 字符串拼接 | concat(String | Integer | Bytes | Array elements):String | concat("https://www.example.com", http.request.uri.path) |
ends_with | 是否以指定字符串结尾 | ends_with(source, substring):Boolean | ends_with(http.request.uri.path, ".html") |
starts_with | 是否以指定字符串开头 | starts_with(source, substring):Boolean | starts_with(http.request.uri.path, "/api/") |
lower | 转小写 | lower(String):String | lower(http.host) eq "www.example.com" |
upper | 转大写 | upper(String):String | upper(http.host) |
regex_replace | 正则替换 | regex_replace(source, regex, replacement):String | regex_replace(http.request.uri.path, "/cat/(.*)$", "/dog/${1}") |
wildcard_replace | 通配符替换 | wildcard_replace(source, pattern, replacement, flags?):String | wildcard_replace(http.request.uri.path, "/*", "/apps/${1}") |
to_string | 转字符串 | to_string(Integer | Boolean | IP address):String | to_string(ip.src.asnum) |
6.1 函数使用限制
ends_with/starts_with的source参数必须是字段,不能是字面量字符串。例如不能写成ends_with("foo.html", ".html")。regex_replace默认只替换第一个匹配项;区分大小写;$在替换字符串中需用$$转义;支持${N}引用捕获组(最多 8 个)。wildcard_replace的source必须是字段;wildcard_pattern中文字*需用\*转义,\需用\\转义;**无效;flags为s时启用区分大小写匹配;采用最短(惰性)匹配。
7. 常见示例
7.1 主机名匹配
(http.host eq "www.example.com")
7.2 路径前缀匹配
(http.request.uri.path starts_with "/api/")
7.3 静态资源匹配
(http.request.uri.path.extension in {"jpg" "jpeg" "png" "gif" "css" "js" "webp" "svg" "ico"})
7.4 国家/地区匹配
(ip.geoip.country eq "CN")
(not ip.geoip.country eq "CN")
7.5 IP 白名单
(ip.src in {"1.2.3.4" "5.6.7.8"})
7.6 请求方法匹配
(http.request.method eq "POST")
7.7 组合条件
(http.host eq "www.example.com" and http.request.method eq "POST" and http.request.uri.path starts_with "/api/")
7.8 请求头匹配
(http.request.headers["x-api-key"] eq "secret-key")
(exists(http.request.headers["authorization"]))
7.9 Cookie 匹配
(http.request.cookies["sessionid"] eq "abc123")
7.10 User-Agent 匹配
(http.user_agent contains "bot")
7.11 HTTPS 匹配
(ssl eq true)
7.12 正则匹配
(http.request.uri.path matches "^/api/v[0-9]+/.*$")
8. 表达式编辑器与生成器
安全与加速 控制台提供两种配置方式:
- 图形化控件:通过下拉框、输入框配置条件,自动生成表达式。
- 表达式编辑器:直接编写表达式代码,支持括号嵌套和分组符号。
使用表达式编辑器时,请注意:
- 单条表达式必须用括号包裹。
- 组合表达式建议显式使用括号控制优先级。
- 字符串中的特殊字符需要正确转义。
- 正则表达式需符合 PCRE 标准。
9. 注意事项
- 字段大小写敏感性:不同字段的大小写敏感性不同,配置前请确认字段属性。
- 空字符串支持:部分字段不允许空字符串匹配值。
- 套餐限制:正则匹配、子条件数量等能力受套餐版本限制。
- 性能影响:过长的字符串、复杂的嵌套或正则可能影响匹配性能。
- 表达式长度:单条规则总长度不超过 4K 字符。