⚠️ 本配置适用于没有登录功能的 WordPress 网站。如果网站具有登录功能,使用此配置可能会导致登录延迟 5~6 秒。
原文参考地址:
https://website-custom.com/cloudflare-cdn-tutorial/#heading-d8sr34xhq
本文对原有规则进行了更新优化,适配 Cloudflare 最新版本。
一、设置页面规则(Page Rules)
进入 Cloudflare 后台,点击域名进入管理页面。
导航至:左侧【规则】→【页面规则】。
免费用户可设置三条规则,点击“创建页面规则”按钮。
规则 1:绕过 WordPress 后台缓存
WordPress 后台路径通常为 domain.com/wp-admin/xxxx
。
设置如下:
-
URL:
domain.com/wp-admin*
-
缓存级别:绕过(Bypass)
同时建议修改登录地址为 domain.com/wp-admin-xxx
形式,进一步防止缓存。
规则 2:绕过 JSON 动态请求缓存
WordPress 中如 domain.com/wp-json/xxxx
的请求为动态数据,不能缓存,否则可能导致功能异常。
设置如下:
-
URL:
domain.com/wp-json*
-
缓存级别:绕过(Bypass)
规则 3:缓存所有页面内容
设置如下:
-
URL:
domain.com/*
-
缓存级别:缓存所有内容(Cache Everything)
-
边缘缓存 TTL:1 个月
-
电子邮件混淆技术:开启
-
浏览器完整性检查:开启
-
浏览器缓存 TTL:1 个月
-
规则顺序:此规则必须排在最后一条,以确保前两条优先生效
二、设置缓存规则(Cache Rules)
导航至左侧【缓存】→【Cache Rules】→“创建规则”
缓存规则一:缓存静态资源文件
-
命名:缓存静态文件
-
表达式(使用表达式生成器):
(http.request.uri contains ".jpg" and http.request.uri contains ".png" and http.request.uri contains ".webp" and http.request.uri contains ".exe" and http.request.uri contains ".woff" and http.request.uri contains ".js" and http.request.uri contains ".gif" and http.request.uri contains ".ico" and http.request.uri contains ".zip" and http.request.uri contains ".jsp" and http.request.uri contains ".pdf" and http.request.uri contains ".woff2" and http.request.uri contains ".css" and http.request.uri contains ".txt" and http.request.uri contains ".svg" and http.request.uri contains ".rar" and http.request.uri contains ".tar")
-
缓存资格:符合缓存条件
-
边缘 TTL:使用源站缓存控制标头
-
浏览器 TTL:接受源服务器 TTL
缓存规则二:不缓存后台与动态请求
-
命名:后台绕过缓存
-
表达式:
顺序如下
三、清除 Cloudflare 缓存
在网站更新样式或内容后,应清除缓存以立即生效。
进入【缓存】→【配置】界面,可选择:
-
清除单个页面缓存
-
清除整个网站缓存(推荐)
关闭缓存配置的方法
若不想继续使用缓存规则,可按以下步骤操作:
-
将所有相关开关关闭;
-
清除全站缓存;
-
刷新网站,即可完全禁用缓存机制。
暂无评论内容