Skip to content

Page Rules - 页面规则

Cloudflare Page Rules 允许你为特定 URL 路径自定义 Cloudflare 设置。

使用场景

  • 启用/禁用缓存
  • URL 转发(重定向)
  • 修改安全级别
  • 调整缓存 TTL
  • 绕过缓存
  • 启用 Browser Cache TTL

配置方式

通过 Dashboard

  1. 登录 Cloudflare Dashboard
  2. 选择域名 → Rules → Page Rules
  3. 创建页面规则:
    • URL 匹配: 定义规则生效的 URL 模式
    • 设置: 添加一个或多个设置项

URL 匹配模式

# 精确匹配
example.com/api

# 通配符匹配
*example.com/api/*          # 匹配所有 /api/ 开头的路径
example.com/*.jpg           # 匹配所有 .jpg 文件
example.com/api/*           # 匹配 /api/ 下的所有内容

# 正则表达式(需开启)
example.com/api/*/user/*

常用设置

1. 缓存级别

URL: *example.com/static/*
设置: Cache Level = Standard

2. 缓存 TTL

URL: *example.com/images/*
设置: Edge Cache TTL = 1 hour

3. URL 转发

URL: example.com/old-path/*
设置: Forwarding URL = 301 - example.com/new-path/$1

4. 绕过缓存

URL: *example.com/api/*
设置: Cache Level = Bypass

5. 启用缓存

URL: *example.com/assets/*
设置: Cache Level = Cache Everything

6. Browser Cache TTL

URL: *example.com/static/*
设置: Browser Cache TTL = 1 year

7. 禁用性能优化

URL: *example.com/admin/*
设置: Disable Performance

8. 安全级别

URL: *example.com/api/*
设置: Security Level = Essentially Off

9. 自动 HTTPS 重写

URL: example.com/*
设置: Automatic HTTPS Rewrites = On

10. 在线绕过缓存

URL: *example.com/wp-admin/*
设置: Disable Cache

优先级

Page Rules 按照创建顺序从上到下匹配,先匹配的规则优先级更高

建议:

  • 将最具体的规则放在前面
  • 通配符规则放在后面
  • 使用「拖拽」调整顺序

限制

计划Page Rule 数量
Free3
Pro20
Business50
Enterprise125

最佳实践

  1. 精确匹配优先: 先写具体路径,后写通配符
  2. 避免冲突: 同一 URL 不要被多个规则覆盖
  3. 定期审查: 删除不再使用的规则
  4. 使用测试: 部署前用 curl -I 验证响应头

示例配置

# 规则 1: API 绕过缓存
URL: *example.com/api/*
设置: Cache Level = Bypass

# 规则 2: 静态资源长缓存
URL: *example.com/static/*
设置:
  - Cache Level = Cache Everything
  - Edge Cache TTL = 1 month
  - Browser Cache TTL = 1 year

# 规则 3: 旧 URL 重定向
URL: example.com/old-path/*
设置: Forwarding URL = 301 - https://example.com/new-path/$1

# 规则 4: 管理后台禁用缓存
URL: *example.com/admin/*
设置: Disable Cache

# 规则 5: 图片优化
URL: *example.com/images/*
设置: Polish = Lossless

与 Origin Rules 的区别

特性Page RulesOrigin Rules
用途缓存、转发、安全修改源站 IP/端口
优先级高(按顺序)
限制数量有(根据计划)
匹配方式URL 模式主机名/路径

基于 MIT 许可证发布。