规则引擎接口
更新时间:2026-09-08
设置规则引擎配置
Go
1cli := GetDefaultClient()
2
3// 忽略大小写
4ignoreCase := true
5
6// 各配置项取值
7cacheKeyQuery := false
8cacheKeyIgnoreCase := true
9offlineMode := "OFF"
10refreshRevalidateEnabled := true
11httpToHttpsEnabled := "ON"
12httpToHttpsCode := "302"
13hstsMaxAge := -1
14hstsIncludeSubDomains := false
15hstsPreload := false
16isa := "ON"
17http2Disable := "OFF"
18http3Enable := true
19webSocketEnabled := true
20webSocketTimeout := 10
21http2Origin := "ON"
22clientMaxBodySize := "500m"
23compress := "ON"
24originLoadTimeout := 30
25originConnectTimeout := 5
26enableRedirectFollow := "ON"
27maxRedirectFollowCount := 2
28originRange := "force_all"
29originPartSize := "512k"
30realIpEnabled := true
31realIpName := "True-Client-Ip"
32errorPageCode400 := 400
33errorPageUrl400 := "http://test.eo.com/400.html"
34errorPageCode403 := 403
35errorPageUrl403 := "http://test.eo.com/403.html"
36trafficLimitEnable := true
37trafficLimitRate := 1
38trafficLimitStartHour := 10
39trafficLimitEndHour := 19
40trafficLimitRateUnit := "k"
41antiType := "typeA"
42antiSecretKey := "your_secret_key"
43antiNewsecretKey := "your_new_secret_key"
44antiTimeout := 1800
45antiTimestampFormat := "dec"
46antiAuthArg := "auth_key"
47originArgIgnore := true
48urlRuleScheme := "http"
49urlRuleHost := "test.eo.com"
50urlRuleDstPath := "/test/1.txt"
51urlRuleQuery := "OFF"
52urlRuleStatus := 302
53sslMode := "custom"
54sslCipherList := "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256"
55sslModePtr := &sslMode
56sslCipherListPtr := &sslCipherList
57ocsp := "ON"
58clientCacheTtlMode := "custom"
59clientCacheTtlValue := 150
60originHttpPort := 80
61originHttpsPort := 443
62originHost := "test.eo.com"
63
64resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
65 PageRules: &[]api.PageRule{
66 {
67 Name: "rule",
68 Status: "ON",
69 Rules: [][]api.Rule{
70 // 同一内层切片内的条件为「与」关系
71 {
72 {MatchFrom: "path", Operator: "inValues", Values: []string{"/test"}, IgnoreCase: &ignoreCase},
73 {MatchFrom: "directory", Operator: "inValues", Values: []string{"/test/"}},
74 {MatchFrom: "arg", Operator: "inValues", MatchKey: "test", Values: []string{"abc"}},
75 {MatchFrom: "suffix", Operator: "inValues", Values: []string{"jpg"}},
76 {MatchFrom: "header", Operator: "inValues", MatchKey: "user-agent", Values: []string{"test"}},
77 {MatchFrom: "fullUrl", Operator: "inValues", Values: []string{"/test/test.txt"}},
78 {MatchFrom: "basename", Operator: "inValues", Values: []string{"test.mp4"}},
79 // 中国内地用 Pro,其他国家用 Cty
80 {MatchFrom: "remoteGeos", Operator: "inValues", Values: []api.RemoteGeo{
81 {Pro: "beijing"},
82 {Cty: "bt"},
83 }},
84 // 支持 IP 地址和 IP 段
85 {MatchFrom: "remoteAddrs", Operator: "inValues", Values: []string{"1.2.3.4", "172.16.0.0/12"}},
86 // 运营商用单个字符串,多个以 | 分隔
87 {MatchFrom: "remoteIsp", Operator: "inValues", Values: "cm|un|ct"},
88 {MatchFrom: "method", Operator: "inValues", Values: []string{"GET", "POST"}},
89 {MatchFrom: "cookie", Operator: "inValues", MatchKey: "test", Values: []string{"abc"}},
90 {MatchFrom: "host", Operator: "inValues", Values: []string{"1.test.com"}},
91 },
92 // 不同内层切片之间为「或」关系
93 {
94 // 正则匹配,Values 传单个字符串
95 {MatchFrom: "path", Operator: "regex", Values: "^/example/test[123]/$"},
96 // 存在性判断,不需要 Values
97 {MatchFrom: "arg", Operator: "exists", MatchKey: "test"},
98 },
99 },
100 Config: &api.RuleConfig{
101 // 节点缓存
102 CacheTtl: &[]api.CacheTtl{
103 {Value: "/", Weight: 100, OverrideOrigin: false, Ttl: 0, Type: "path"},
104 },
105
106 // 自定义 cacheKey
107 CacheKey: &api.RuleCacheKey{
108 Query: &cacheKeyQuery,
109 IgnoreCase: &cacheKeyIgnoreCase,
110 IncludeArgs: &[]string{"a", "b"},
111 Headers: &[]string{"Accept-Language"},
112 Cookie: &[]string{"uid"},
113 },
114
115 // 离线模式
116 OfflineMode: &offlineMode,
117
118 // 忽略客户端刷新
119 RefreshRevalidate: &api.RefreshRevalidate{Enabled: &refreshRevalidateEnabled},
120
121 // 强制 HTTPS
122 HttpToHttpsEnabled: &httpToHttpsEnabled,
123 HttpToHttpsCode: &httpToHttpsCode,
124
125 // HSTS
126 Hsts: &api.HSTS{
127 MaxAge: &hstsMaxAge,
128 IncludeSubDomains: &hstsIncludeSubDomains,
129 Preload: &hstsPreload,
130 },
131
132 // 智能加速
133 Isa: &isa,
134
135 // HTTP2 / HTTP3 / WebSocket
136 Http2Disable: &http2Disable,
137 Http3: &api.HTTP3{Enable: &http3Enable},
138 WebSocket: &api.WebSocket{
139 Enabled: &webSocketEnabled,
140 Timeout: &webSocketTimeout,
141 },
142
143 // HTTP2 回源、最大上传大小
144 Http2Origin: &http2Origin,
145 ClientMaxBodySize: &clientMaxBodySize,
146
147 // 页面压缩
148 Compress: &compress,
149 CompressMethodArray: &[]string{"gzip", "br"},
150
151 // 回源超时时间
152 OriginTimeout: &api.OriginTimeout{
153 LoadTimeout: &originLoadTimeout,
154 ConnectTimeout: &originConnectTimeout,
155 },
156
157 // 回源 301/302 跟随
158 OriginRedirectOptions: &api.OriginRedirectOptions{
159 EnableRedirectFollow: &enableRedirectFollow,
160 MaxRedirectFollowCount: &maxRedirectFollowCount,
161 },
162
163 // 回源 range
164 OriginOptions: &api.OriginOptions{
165 Range: &originRange,
166 PartSize: &originPartSize,
167 },
168
169 // 用户 IP 获取
170 RealIp: &api.RealIp{
171 Enabled: &realIpEnabled,
172 Name: &realIpName,
173 },
174
175 // 自定义错误页面
176 ErrorPage: &[]api.ErrorPage{
177 {Code: &errorPageCode400, Url: &errorPageUrl400},
178 {Code: &errorPageCode403, Url: &errorPageUrl403},
179 },
180
181 // 单链接限速
182 TrafficLimit: &api.TrafficLimit{
183 Enable: &trafficLimitEnable,
184 LimitRate: &trafficLimitRate,
185 LimitStartHour: &trafficLimitStartHour,
186 LimitEndHour: &trafficLimitEndHour,
187 LimitRateUnit: &trafficLimitRateUnit,
188 },
189
190 // URL 鉴权
191 AntiHotLink: &api.AntiHotLink{
192 AntiType: &antiType,
193 SecretKey: &antiSecretKey,
194 NewsecretKey: &antiNewsecretKey,
195 Timeout: &antiTimeout,
196 TimestampFormat: &antiTimestampFormat,
197 AuthArg: &antiAuthArg,
198 },
199
200 // 回源请求参数
201 OriginArg: &api.OriginArg{
202 Ignore: &originArgIgnore,
203 Args: &[]string{"test"},
204 },
205
206 // 访问 URL 重定向
207 UrlRules: &[]api.UrlRules{
208 {
209 Scheme: &urlRuleScheme,
210 Host: &urlRuleHost,
211 DstPath: &urlRuleDstPath,
212 Query: &urlRuleQuery,
213 Status: &urlRuleStatus,
214 },
215 },
216
217 // SSL/TLS 安全配置
218 SslProtocols: &[]string{"TLSv1.0", "TLSv1.1", "TLSv1.2", "TLSv1.3"},
219 SslMode: &sslModePtr,
220 SslCipherList: &sslCipherListPtr,
221
222 // OCSP Stapling
223 Ocsp: &ocsp,
224
225 // 状态码缓存
226 CacheCodeTtl: &[]api.CacheCodeTtl{
227 {Value: "404", Weight: 100, OverrideOrigin: true, Ttl: 10, Type: "code"},
228 {Value: "400", Weight: 100, OverrideOrigin: true, Ttl: 10, Type: "code"},
229 },
230
231 // 浏览器缓存 TTL
232 ClientCacheTtl: &api.ClientCacheTtl{
233 Mode: &clientCacheTtlMode,
234 Ttl: &clientCacheTtlValue,
235 },
236
237 // HTTP 回源请求头
238 OriginRequest: &api.OriginRequest{
239 AddHeaders: &api.HeaderMap{"test": "${uri}", "test2": "11"},
240 RemoveHeaders: &api.HeaderMap{"Content-Type": ""},
241 },
242
243 // HTTP 节点响应头
244 ClientResponse: &api.ClientResponse{
245 AddHeaders: &api.HeaderMap{"test": "${uri}", "test2": "11"},
246 RemoveHeaders: &api.HeaderMap{"Content-Type": ""},
247 },
248
249 // 源站修改(与 OriginPool 二选一)
250 OriginConfig: &[]api.OriginItem{
251 {
252 Addr: "1.1.1.1",
253 Type: "IP",
254 UpstreamProtocol: "*",
255 HttpPort: &originHttpPort,
256 HttpsPort: &originHttpsPort,
257 Host: &originHost,
258 },
259 },
260
261 // 源站池(与 OriginConfig 二选一,二者不可同时设置)
262 //OriginPool: &api.OriginPool{
263 // OriginPoolId: "your_origin_pool_id",
264 // UpstreamProtocol: "*",
265 // HttpPort: &originHttpPort,
266 // HttpsPort: &originHttpsPort,
267 //},
268 },
269 },
270 },
271})
272result, _ := json.Marshal(resp)
273fmt.Printf("result: %s\n", result)
274fmt.Printf("err:%+v\n", err)
规则引擎的详细参数说明可参考API文档:https://cloud.baidu.com/doc/GEO/s/dmimubjhg
api.PageRule 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Name | String | 是 | 规则名称。 |
| Status | String | 是 | 规则状态。 |
| Rules | [][]api.Rule | 是 | 请求的匹配规则。 |
| Config | *api.RuleConfig | 是 | 命中后生效的配置项集合。 |
注:如需清空所有规则,可显式传 &[]api.PageRule{}。
api.Rule 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| MatchFrom | String | 是 | 匹配类型。 |
| Operator | String | 是 | 操作符。 |
| MatchKey | String | 否 | 匹配类型的 key,arg、header、cookie 等需要指定 key 的匹配类型使用。 |
| Values | interface{} | 否 | 匹配值。 |
| IgnoreCase | *Bool | 否 | 是否忽略大小写。 |
api.RemoteGeo 字段说明(matchFrom=remoteGeos 时作为 Values 的元素):
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Pro | String | 否 | 中国内地省份,如 beijing。 |
| Cty | String | 否 | 其他国家,如 bt。 |
api.RuleConfig 主要字段说明:
| 字段 | 类型 | 说明 |
|---|---|---|
| CacheTtl | *[]api.CacheTtl | 节点缓存。 |
| CacheKey | *api.RuleCacheKey | 自定义cacheKey。 |
| OfflineMode | *String | 离线模式。 |
| RefreshRevalidate | *api.RefreshRevalidate | 忽略客户端刷新。 |
| HttpToHttpsEnabled | *String | 强制 HTTPS 开关。 |
| HttpToHttpsCode | *String | 强制 HTTPS 跳转状态码。 |
| Hsts | *api.HSTS | HSTS。 |
| Isa | *String | 智能加速。 |
| Http2Disable | *String | HTTP2。 |
| Http3 | *api.HTTP3 | HTTP3。 |
| WebSocket | *api.WebSocket | WebSocket。 |
| Http2Origin | *String | HTTP2 回源。 |
| ClientMaxBodySize | *String | 最大上传大小。 |
| Compress | *String | 页面压缩开关。 |
| CompressMethodArray | *[]String | 页面压缩方式。 |
| OriginTimeout | *api.OriginTimeout | 回源超时时间。 |
| OriginRedirectOptions | *api.OriginRedirectOptions | 回源301/302跟随。 |
| OriginOptions | *api.OriginOptions | 回源range。 |
| RealIp | *api.RealIp | 用户IP获取。 |
| ErrorPage | *[]api.ErrorPage | 自定义错误页面。 |
| TrafficLimit | *api.TrafficLimit | 单链接限速。 |
| AntiHotLink | *api.AntiHotLink | URL鉴权。 |
| OriginArg | *api.OriginArg | 回源请求参数。 |
| UrlRules | *[]api.UrlRules | 访问URL重定向。 |
| SslProtocols | *[]String | SSL/TLS 版本。 |
| SslMode | **String | 加密算法套件模式。 |
| SslCipherList | **String | 加密算法套件列表。 |
| Ocsp | String | OCSP。 |
| CacheCodeTtl | []api.CacheCodeTtl | 状态码缓存。 |
| ClientCacheTtl | api.ClientCacheTtl | 浏览器缓存 TTL。 |
| OriginConfig | []api.OriginItem | 源站配置。 |
| OriginPool | api.OriginPool | 源站池。 |
| OriginRequest | api.OriginRequest | HTTP 回源请求头。 |
| ClientResponse | *api.ClientResponse | HTTP 节点响应头。 |
查询规则引擎配置
Go
1cli := GetDefaultClient()
2cfg, err := cli.GetSiteConfig("your_site.com")
3if err != nil {
4 fmt.Printf("err:%+v\n", err)
5 return
6}
7
8// 只查询规则引擎配置
9if cfg.PageRules != nil {
10 pageRulesData, _ := json.Marshal(cfg.PageRules)
11 fmt.Printf("cacheTtl: %s\n", pageRulesData)
12}
13
14// cfg.PageRules 为该站点当前的规则引擎配置;
评价此篇文章
