站点全局配置接口
更新时间:2026-06-18
站点配置接口
EO 站点的全局配置功能共用同一接口:
PUT /v2/geo/site/{site}/config:设置配置GET /v2/geo/site/{site}/config:查询配置(返回站点全部配置项)
api.SiteConfig 是统一的配置容器,所有字段均为指针类型 + omitempty:未赋值的字段不会出现在请求 body 中,未来新增配置项时按相同方式扩展即可。如需将某个配置显式设置为空(例如 cacheTtl: [] 表示遵循源站-默认缓存策略),传一个非 nil 的空值(如 &[]api.CacheTtl{})。
相关配置接口说明可参考对应配置的API文档:https://cloud.baidu.com/doc/GEO/s/Pmiigxbf0
设置节点缓存配置
Go
1cli := GetDefaultClient()
2resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
3 CacheTtl: &[]api.CacheTtl{
4 {
5 Value: "/",
6 Weight: 100,
7 OverrideOrigin: true,
8 Ttl: 2592000,
9 Type: "path",
10 },
11 },
12})
13result, _ := json.Marshal(resp)
14fmt.Printf("result: %s\n", result)
15fmt.Printf("err:%+v\n", err)
api.CacheTtl 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Type | String | 是 | 其合法值为“path”。表示缓存目录的路径。 |
| Value | String | 是 | 其合法值为“/”。表示根目录。 |
| Weight | Int | 是 | 权重,合法值 100。 |
| OverrideOrigin | Bool | 是 | 表示缓存是否遵循源站。值为 true 时,表示不遵循源站,按照该条配置规则缓存。值为 false 时,表示遵循源站。 |
| Ttl | Int | 是 | 缓存时间,单位秒;0 表示不缓存。 |
设置查询字符串配置
Go
1cli := GetDefaultClient()
2query := false
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 CacheKey: &api.CacheKey{
5 Query: &query,
6 IncludeArgs: &[]string{"test1"},
7 },
8})
9result, _ := json.Marshal(resp)
10fmt.Printf("result: %s\n", result)
11fmt.Printf("err:%+v\n", err)
api.CacheKey 字段说明(IncludeArgs 与 ExcludeArgs 不可同时设置;二者仅在 Query=false 时生效):
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Query | *Bool | 是 | true 保留全部参数参与缓存;false 忽略全部参数参与缓存。 |
| IncludeArgs | *[]String | 否 | 保留指定参数参与缓存(仅 Query=false 时有效)。 |
| ExcludeArgs | *[]String | 否 | 忽略指定参数参与缓存(仅 Query=false 时有效)。 |
设置离线模式配置
Go
1cli := GetDefaultClient()
2offlineMode := "ON"
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 OfflineMode: &offlineMode,
5})
6result, _ := json.Marshal(resp)
7fmt.Printf("result: %s\n", result)
8fmt.Printf("err:%+v\n", err)
OfflineMode 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| OfflineMode | *String | 是 | ON 开启离线模式;OFF 关闭离线模式。 |
设置强制 HTTPS 配置
Go
1cli := GetDefaultClient()
2httpToHttpsEnabled := "ON"
3httpToHttpsCode := "302"
4resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
5 HttpToHttpsEnabled: &httpToHttpsEnabled,
6 HttpToHttpsCode: &httpToHttpsCode,
7})
8result, _ := json.Marshal(resp)
9fmt.Printf("result: %s\n", result)
10fmt.Printf("err:%+v\n", err)
HttpToHttps* 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| HttpToHttpsEnabled | *String | 是 | ON 开启强制 HTTPS 重定向;OFF 关闭。 |
| HttpToHttpsCode | *String | 否 | 重定向状态码,301 或 302;HttpToHttpsEnabled=OFF 时无效。 |
设置 HSTS 配置
Go
1cli := GetDefaultClient()
2maxAge := -1
3includeSubDomains := false
4preload := false
5resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
6 Hsts: &api.HSTS{
7 MaxAge: &maxAge,
8 IncludeSubDomains: &includeSubDomains,
9 Preload: &preload,
10 },
11})
12result, _ := json.Marshal(resp)
13fmt.Printf("result: %s\n", result)
14fmt.Printf("err:%+v\n", err)
api.HSTS 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| MaxAge | *Int | 是 | 配置保存时间,单位为天, 用户输入值为 0 ~ 730 或者 -1,为 -1 时表示关闭该配置项。 |
| IncludeSubDomains | *Bool | 是 | 是否包含子域名。 |
| Preload | *Bool | 是 | 是否支持预加载。 |
设置 HTTP2 配置
Go
1cli := GetDefaultClient()
2http2Disable := "OFF"
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 Http2Disable: &http2Disable,
5})
6result, _ := json.Marshal(resp)
7fmt.Printf("result: %s\n", result)
8fmt.Printf("err:%+v\n", err)
Http2Disable 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Http2Disable | *String | 是 | OFF 开启 HTTP2;ON 关闭 HTTP2。 |
设置 HTTP3 配置
Go
1cli := GetDefaultClient()
2http3Enable := true
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 Http3: &api.HTTP3{
5 Enable: &http3Enable,
6 },
7})
8result, _ := json.Marshal(resp)
9fmt.Printf("result: %s\n", result)
10fmt.Printf("err:%+v\n", err)
api.HTTP3 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Enable | *Bool | 是 | true 开启 HTTP3;false 关闭 HTTP3。 |
设置最大上传大小配置
Go
1cli := GetDefaultClient()
2clientMaxBodySize := "500m"
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 ClientMaxBodySize: &clientMaxBodySize,
5})
6result, _ := json.Marshal(resp)
7fmt.Printf("result: %s\n", result)
8fmt.Printf("err:%+v\n", err)
ClientMaxBodySize 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| ClientMaxBodySize | *String | 是 | 单位支持 b、k、m(忽略大小写,b 可省略)。最大 500m。示例:100、100k、100M。 |
设置页面压缩配置
Go
1cli := GetDefaultClient()
2compress := "ON"
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 Compress: &compress,
5 CompressMethodArray: &[]string{"gzip", "br"},
6})
7result, _ := json.Marshal(resp)
8fmt.Printf("result: %s\n", result)
9fmt.Printf("err:%+v\n", err)
Compress* 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Compress | *String | 是 | ON 开启页面压缩;OFF 关闭页面压缩。 |
| CompressMethodArray | *[]String | 是 | 压缩方式,合法值:gzip、br,可同时启用。 |
设置智能加速配置
Go
1cli := GetDefaultClient()
2isa := "ON"
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 Isa: &isa,
5})
6result, _ := json.Marshal(resp)
7fmt.Printf("result: %s\n", result)
8fmt.Printf("err:%+v\n", err)
Isa 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Isa | *String | 是 | ON 开启智能加速;OFF 关闭智能加速。 |
设置自定义 HTTP 头配置
注意:本接口为全量更新,每次设置需带上希望保留的全部规则,否则原有配置会被覆盖。
Go
1cli := GetDefaultClient()
2resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
3 HttpHeader: &[]api.HttpHeader{
4 {Type: "response", Header: "Cache-Control", Value: "test", Action: "add"},
5 {Type: "origin", Header: "Expires", Value: "test", Action: "add"},
6 },
7})
8result, _ := json.Marshal(resp)
9fmt.Printf("result: %s\n", result)
10fmt.Printf("err:%+v\n", err)
api.HttpHeader 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Type | String | 是 | origin 表示回源生效;response 表示给用户响应时生效。 |
| Header | String | 是 | header 为 http 头字段,一般为 HTTP 的标准 Header,其长度限制为 128。也可以是用户自定义的 Header。 |
| Value | String | 是 | 指定 header 的值,其长度限制为 1000。可以是常量,也可以是变量;删除 HTTP 头时可以传空字符串 ""。变量约束:以 $ 开始的子串必须符合 ${x} 模式,合法变量为:• ${uri} 客户端请求的 URL 路径部分(不含查询参数)• ${host} 客户端请求 host 头部值• ${scheme} 客户端请求协议(http 或 https)• ${request_uri} 客户端请求路径和参数(含查询参数)• ${jvip} 节点 IP• ${remote_addr} 客户端 IP(存在代理时不准确)• ${request_id} 请求的唯一标识符典型非法值: • 变量不符合限制,如 X-REQ-${url}• 含 $ 但不符合 ${x} 模式,如 X-REQ-$uri注意:value 不支持 $ 纯字符透传,如 X-$ 非法。 |
| Action | String | 是 | 设置 HTTP 头合法值为 add,删除 HTTP 头合法值为 remove。 |
注:最多设置 20 条 HTTP 回源请求头规则,最多设置 20 条 HTTP 节点响应头规则;不支持删除以 ohc、baidu 开头的回源请求头。
设置状态码缓存配置
Go
1cli := GetDefaultClient()
2resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
3 CacheCodeTtl: &[]api.CacheTtlCode{
4 {Value: "404", Weight: 100, OverrideOrigin: true, Ttl: 10, Type: "code"},
5 {Value: "400", Weight: 100, OverrideOrigin: true, Ttl: 10, Type: "code"},
6 },
7})
8result, _ := json.Marshal(resp)
9fmt.Printf("result: %s\n", result)
10fmt.Printf("err:%+v\n", err)
api.CacheTtlCode 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Type | String | 是 | 合法值 code,表示异常状态码缓存。 |
| Value | String | 是 | 4xx:400/401/403/404/405/407/414/451;5xx: 500/501/502/503/504/509/514。 |
| Ttl | Int | 是 | 缓存时间,单位秒,取值范围 0~315360000。 |
| OverrideOrigin | Bool | 是 | 合法值 true。 |
| Weight | Int | 是 | 权重,合法值 100。 |
注:最多 15 条状态码缓存规则,且不可重复。
设置 gRPC 回源配置
Go
1cli := GetDefaultClient()
2grpcOrigin := "ON"
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 GrpcOrigin: &grpcOrigin,
5})
6result, _ := json.Marshal(resp)
7fmt.Printf("result: %s\n", result)
8fmt.Printf("err:%+v\n", err)
GrpcOrigin 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| GrpcOrigin | *String | 是 | ON 开启 gRPC 回源;OFF 关闭 gRPC 回源。 |
设置 HTTP2 回源配置
Go
1cli := GetDefaultClient()
2http2Origin := "ON"
3resp, err := cli.SetSiteConfig("your_site.com", &api.SiteConfig{
4 Http2Origin: &http2Origin,
5})
6result, _ := json.Marshal(resp)
7fmt.Printf("result: %s\n", result)
8fmt.Printf("err:%+v\n", err)
Http2Origin 字段说明:
| 字段 | 类型 | 是否必选 | 说明 |
|---|---|---|---|
| Http2Origin | *String | 是 | ON 开启 HTTP2 回源;OFF 关闭 HTTP2 回源。 |
查询站点配置
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.CacheTtl != nil {
10 cacheTtlData, _ := json.Marshal(cfg.CacheTtl)
11 fmt.Printf("cacheTtl: %s\n", cacheTtlData)
12}
13
14// 查询由多个字段组成的配置项(如「页面压缩」由 Compress + CompressMethodArray 共同组成)
15if cfg.Compress != nil || cfg.CompressMethodArray != nil {
16 compressData, _ := json.Marshal(struct {
17 Compress *string `json:"compress,omitempty"`
18 CompressMethodArray *[]string `json:"compressMethodArray,omitempty"`
19 }{cfg.Compress, cfg.CompressMethodArray})
20 fmt.Printf("compress: %s\n", compressData)
21}
22// 同理「强制 HTTPS」由 HttpToHttpsEnabled 和 HttpToHttpsCode 组成,可按相同方式聚合查询。
23// 注意:当 HttpToHttpsEnabled = "OFF" 时,服务端不返回 HttpToHttpsCode
24if cfg.HttpToHttpsEnabled != nil {
25 httptohttpsData, _ := json.Marshal(struct {
26 HttpToHttpsEnabled *string `json:"httpToHttpsEnabled,omitempty"`
27 HttpToHttpsCode *string `json:"httpToHttpsCode,omitempty"`
28 }{cfg.HttpToHttpsEnabled, cfg.HttpToHttpsCode})
29 fmt.Printf("httpToHttps: %s\n", httptohttpsData)
30}
31
32// 查询全部配置
33allData, _ := json.Marshal(cfg)
34fmt.Printf("SiteConfig: %s\n", allData)
35
36// cfg.CacheTtl 为该站点当前的节点缓存配置;
37// cfg.CacheKey 为该站点当前的查询字符串配置;
38// cfg.OfflineMode 为该站点当前的离线模式配置;
39// cfg.HttpToHttpsEnabled 和 cfg.HttpToHttpsCode 为该站点当前的强制 HTTPS 配置;
40// cfg.Hsts 为该站点当前的 HSTS 配置;
41// cfg.Http2Disable 为该站点当前的 HTTP2 配置;
42// cfg.Http3 为该站点当前的 HTTP3 配置;
43// cfg.ClientMaxBodySize 为该站点当前的最大上传大小配置;
44// cfg.Compress 和 cfg.CompressMethodArray 为该站点当前的页面压缩配置;
45// cfg.Isa 为该站点当前的智能加速配置;
46// cfg.HttpHeader 为该站点当前的自定义 HTTP 头配置;
47// cfg.CacheCodeTtl 为该站点当前的状态码缓存配置;
48// cfg.GrpcOrigin 为该站点当前的 gRPC 回源配置;
49// cfg.Http2Origin 为该站点当前的 HTTP2 回源配置;
评价此篇文章
