跳到主要内容

安全与加速 规则表达式指南

本文档整理自 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.hostString请求主机名www.example.com
http.request.uriStringURI(路径 + 查询串)/image/cat.jpg?width=400
http.request.uri.pathStringURI 路径/image/cat.jpg
http.request.uri.path.extensionString文件扩展名jpg
http.request.uri.queryString查询字符串width=400&height=300
http.request.uri.argsMap<Array<String>>查询参数映射{"format":["webp"]}
http.request.full_uriString完整 URI(含协议、主机名)https://www.example.com/image/cat.jpg
http.request.methodStringHTTP 方法GET
http.request.headersObject请求头映射(键为小写){"content-type":["application/json"]}
http.request.cookiesStringCookie 映射{"sessionid":["330668"]}
http.cookieStringCookie 标头原始值sessionid=330688;userid=abc123
http.refererStringReferer 标头http://www.example.com/index
http.user_agentStringUser-Agent 标头Mozilla/5.0
http.request.body.formMap<Array<String>>form 格式请求体{"username":["admin"]}
http.request.body.mimeString请求体 MIME 类型application/json
http.request.body.rawString原始请求体ABC123
http.request.timestamp.secInteger安全与加速 节点收到请求的 Unix 时间戳(秒)1735019278
http.request.versionStringHTTP 协议版本HTTP/1.1
http.x_forwarded_forStringX-Forwarded-For 标头192.168.0.1, 10.10.0.1
ip.srcIP address客户端源 IP192.0.2.1
ip.src.ispString运营商编码100017(中国电信)
ip.src.versionStringIP 协议版本IPv4 / IPv6
ip.src.region_codeString负载均衡器所在区域EAS
ip.src.subdivision_1_iso_codeString一级行政区划CN-ZJ
ip.src.cityString城市代码310000
ip.src.city_nameString城市名称hangzhou
ip.src.lonFloat经度120.12
ip.src.latFloat纬度30.16
ip.src.postal_codeString邮政编码310000
ip.src.timezoneString时区UTC+8
ip.geoip.asnumIntegerASN 编号37963
ip.geoip.continentString大洲编码AS(亚洲)
ip.geoip.countryString国家/地区编码CN
sslBoolean是否使用 HTTPStrue

国家/地区常用编码

编码含义
CN中国内地
US美国
HK中国香港
TW中国台湾
JP日本
SG新加坡
GB英国
DE德国
FR法国
KR韩国

大洲编码

编码大洲
AF非洲
AN南极洲
AS亚洲
EU欧洲
NA北美洲
OC大洋洲
SA南美洲

3.2 扩展字段

字段类型说明
ali.ja3_hashStringJA3 TLS 指纹
ali.ja4StringJA4 TLS 指纹
ali.js_detection.passedBoolean是否已通过 JavaScript 检测
ali.static_resourceBoolean是否为静态请求
ali.tls_client_auth.cert_verifiedBoolean客户端证书是否已验证
ali.tls_hashStringTLS 指纹哈希
ali.site.nameString安全与加速 站点名称

3.3 原始字段

字段类型说明
http.request.body.rawStringHTTP 请求原始正文

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 布尔值

布尔值直接使用 truefalse,不加引号。

(ssl eq true)

5.3 数字

支持整数和浮点数。

(ip.geoip.asnum ge 45104)

5.4 数组

数组使用方括号 [],元素之间用逗号分隔。但 安全与加速 规则表达式中的集合匹配优先使用 {} 空格分隔语法。

5.5 空值

部分字段允许配置空字符串,需结合具体运算符使用。http.request.uri.args 字段仅在 eqnecontainsmatches 运算符下支持空值。


6. 转换函数

安全与加速 规则语言提供转换函数,用于操作和验证表达式中的值。

函数说明格式示例
concat字符串拼接concat(String | Integer | Bytes | Array elements):Stringconcat("https://www.example.com", http.request.uri.path)
ends_with是否以指定字符串结尾ends_with(source, substring):Booleanends_with(http.request.uri.path, ".html")
starts_with是否以指定字符串开头starts_with(source, substring):Booleanstarts_with(http.request.uri.path, "/api/")
lower转小写lower(String):Stringlower(http.host) eq "www.example.com"
upper转大写upper(String):Stringupper(http.host)
regex_replace正则替换regex_replace(source, regex, replacement):Stringregex_replace(http.request.uri.path, "/cat/(.*)$", "/dog/${1}")
wildcard_replace通配符替换wildcard_replace(source, pattern, replacement, flags?):Stringwildcard_replace(http.request.uri.path, "/*", "/apps/${1}")
to_string转字符串to_string(Integer | Boolean | IP address):Stringto_string(ip.src.asnum)

6.1 函数使用限制

  • ends_with / starts_withsource 参数必须是字段,不能是字面量字符串。例如不能写成 ends_with("foo.html", ".html")
  • regex_replace 默认只替换第一个匹配项;区分大小写;$ 在替换字符串中需用 $$ 转义;支持 ${N} 引用捕获组(最多 8 个)。
  • wildcard_replacesource 必须是字段;wildcard_pattern 中文字 * 需用 \* 转义,\ 需用 \\ 转义;** 无效;flagss 时启用区分大小写匹配;采用最短(惰性)匹配。

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"]))
(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. 表达式编辑器与生成器

安全与加速 控制台提供两种配置方式:

  1. 图形化控件:通过下拉框、输入框配置条件,自动生成表达式。
  2. 表达式编辑器:直接编写表达式代码,支持括号嵌套和分组符号。

使用表达式编辑器时,请注意:

  • 单条表达式必须用括号包裹。
  • 组合表达式建议显式使用括号控制优先级。
  • 字符串中的特殊字符需要正确转义。
  • 正则表达式需符合 PCRE 标准。

9. 注意事项

  1. 字段大小写敏感性:不同字段的大小写敏感性不同,配置前请确认字段属性。
  2. 空字符串支持:部分字段不允许空字符串匹配值。
  3. 套餐限制:正则匹配、子条件数量等能力受套餐版本限制。
  4. 性能影响:过长的字符串、复杂的嵌套或正则可能影响匹配性能。
  5. 表达式长度:单条规则总长度不超过 4K 字符。