Page Rules - 页面规则
Cloudflare Page Rules 允许你为特定 URL 路径自定义 Cloudflare 设置。
使用场景
- 启用/禁用缓存
- URL 转发(重定向)
- 修改安全级别
- 调整缓存 TTL
- 绕过缓存
- 启用 Browser Cache TTL
配置方式
通过 Dashboard
- 登录 Cloudflare Dashboard
- 选择域名 → Rules → Page Rules
- 创建页面规则:
- 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 = Standard2. 缓存 TTL
URL: *example.com/images/*
设置: Edge Cache TTL = 1 hour3. URL 转发
URL: example.com/old-path/*
设置: Forwarding URL = 301 - example.com/new-path/$14. 绕过缓存
URL: *example.com/api/*
设置: Cache Level = Bypass5. 启用缓存
URL: *example.com/assets/*
设置: Cache Level = Cache Everything6. Browser Cache TTL
URL: *example.com/static/*
设置: Browser Cache TTL = 1 year7. 禁用性能优化
URL: *example.com/admin/*
设置: Disable Performance8. 安全级别
URL: *example.com/api/*
设置: Security Level = Essentially Off9. 自动 HTTPS 重写
URL: example.com/*
设置: Automatic HTTPS Rewrites = On10. 在线绕过缓存
URL: *example.com/wp-admin/*
设置: Disable Cache优先级
Page Rules 按照创建顺序从上到下匹配,先匹配的规则优先级更高。
建议:
- 将最具体的规则放在前面
- 通配符规则放在后面
- 使用「拖拽」调整顺序
限制
| 计划 | Page Rule 数量 |
|---|---|
| Free | 3 |
| Pro | 20 |
| Business | 50 |
| Enterprise | 125 |
最佳实践
- 精确匹配优先: 先写具体路径,后写通配符
- 避免冲突: 同一 URL 不要被多个规则覆盖
- 定期审查: 删除不再使用的规则
- 使用测试: 部署前用
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 Rules | Origin Rules |
|---|---|---|
| 用途 | 缓存、转发、安全 | 修改源站 IP/端口 |
| 优先级 | 高(按顺序) | 低 |
| 限制数量 | 有(根据计划) | 无 |
| 匹配方式 | URL 模式 | 主机名/路径 |