一、Cloudflare缓存规则设置
1. 登录Cloudflare控制台
进入域名管理页面
2. 启用CDN(小云朵)
确保所有流量经过Cloudflare(橙色云图标)
二、排除登录用户的缓存
创建”Bypass Logged-in Users”缓存规则:
规则配置
规则名称:Bypass Logged-in Users
匹配条件:
(http.cookie contains "wordpress_logged_in_")
OR (http.cookie contains "wp_")
操作:
Cache Level → Bypass
三、排除动态JS文件
创建”Bypass Dynamic JS”缓存规则:
规则配置
规则名称:Bypass Dynamic JS
匹配条件:
(http.request.uri.path contains "/ajax/")
OR (http.request.uri contains "dynamic.js")
OR (http.request.uri.query contains "nocache")
操作:
Cache Level → Bypass
四、缓存HTML静态页面
创建”Cache HTML Pages”缓存规则:
规则配置
规则名称:Cache HTML Pages
匹配条件:
(http.request.uri.path ends_with ".html")
OR (http.request.uri matches "^/[^.?]+$")
操作:
Cache Level → Cache Everything
Edge Cache TTL → 1小时
五、规则优先级调整
确保规则按以下顺序排列:
- Bypass Logged-in Users
- Bypass Dynamic JS
- Cache HTML Pages
六、WordPress配置
1. 固定链接设置:
后台路径:设置 → 固定链接 → 选择”自定义结构”(示例:/%postname%
.html)
2. 安装缓存插件(可选):
add_action('init', function() {
if (is_user_logged_in()) {
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
}
});
七、验证配置
测试方法:
- 未登录用户访问页面 → 检查响应头是否包含
CF-Cache-Status: HIT
- 登录用户访问页面 → 确认
CF-Cache-Status: BYPASS
- 访问动态JS路径 → 确保未被缓存
没有回复内容