1
Files
cf_rule/cache/readme.md
Hubert Chen 10fa599481 update rules
waf:
    use challenge instead of block
cache:
    update new URL and URI path
2025-12-06 20:54:12 +08:00

66 lines
4.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## Cloudflare 的缓存规则
> [!WARNING]
> 套用规则前请替换对应的域名,并将缓存资格设为 **绕过缓存 / Bypass cache**
> 规则中不允许加注释,记得删掉
使用方法:前往 Cloudflare Dashboard > 域名 > 缓存 > Cache Rules > 创建规则 > 编辑表达式
在添加下面的配置前你需要创建另一个缓存规则,在这个规则中将 `传入请求匹配` 设为 `所有传入请求``缓存资格` 设为 `符合缓存条件``边缘 TTL``浏览器 TTL` 随意来都可以,看你站点内容的更新频率,我的不是什么需要实时更新的站点,我分别设定成了一天和 12 小时,最后将放置位置设为 `第一个`,保存返回
接下来再创建一个规则,`传入请求匹配` 选择 `自定义筛选表达式`,再点击右下角的 `编辑表达式`,根据你的需要将配置填入。之后缓存资格设为 `绕过缓存`,如果你之前有配置,放置位置可能会是自定义,只要保持在前一个规则后面就行,你可以退出到上一个页面查看具体排序,保存即可
### [default.clj](./default.clj) ⬅️ 点击链接查看完整规则
此规则的缓存资格设为 **绕过缓存 / Bypass cache**
```clj
( http.host eq "api.trle5.xyz" ) or # 个人的一些 API
( http.host eq "lume.trle5.xyz" and ( http.request.uri.path contains "/search/" ) ) or # 绕过 lume theme shiraha 的搜索
( http.host eq "memos.trle5.xyz" and ( http.request.uri.path contains "/api/" ) ) or # 绕过 memos api (旧版)
( http.host eq "alist.trle5.xyz" and ( http.request.uri.path contains "/api/" ) ) or # 绕过 alist api
( http.host eq "nxiv.trle5.xyz" and ( http.request.uri.path contains "/api/" ) ) or # 绕过 immich api
( http.host eq "pic.trle5.xyz" and not ( # 另一个 memos 站点用来存图片不绕过资源文件
http.request.uri.path contains "/o/r/" or
http.request.uri.path contains "/assets/"
)
) or
( http.host eq "gitea.trle5.xyz" and not ( # 不绕过以下全部内容
# 头像和资源文件
http.request.uri.path contains "/assets/" or
http.request.uri.path contains "/avatars/" or
http.request.uri.path contains "/repo-avatars/" or
# 组织中公开的仓库
( starts_with( http.request.uri.path, "/mirrors" ) and (
http.request.uri.path contains "/abeezee/" or
http.request.uri.path contains "/urara/" or
http.request.uri.path contains "/urara-docs/" or
http.request.uri.path contains "/canokey-nrf52/"
# 不在列表中的即为 DYNAMIC 动态
)
) or
# 个人账户公开的仓库
( starts_with( http.request.uri.path, "/trle5/" ) and (
http.request.uri.path contains "/cf_rule/" or
http.request.uri.path contains "/.profile/" or
http.request.uri.path contains "/templates/" or
http.request.uri.path contains "/flutter_clock_example/" or
http.request.uri.path contains "/l4d2_tools/" or
http.request.uri.path contains "/hexo_s/" or
http.request.uri.path contains "/lyric/" or
http.request.uri.path contains "/pixelbookgo_hackintosh/" or
http.request.uri.path contains "/tplate/" or
http.request.uri.path contains "/trbot/"
# 不在列表中的即为 DYNAMIC 动态
)
)
)
)
```
关于仓库设定的命令,这里的匹配方式有点问题,如果像这样在仓库名前后都加斜杠,但是 gitea 给的仓库路径默认是不带后斜杠的,这样会导致没有尾斜杠的请求回源(即缓存没有生效)。若单独留一个前斜杠或后斜杠,可能会导致错误的匹配,例如 `/bot` 规则,名为 `/bot_utils` 的仓库在访问时也会因为包含 `/bot` 而被错误的匹配而进入到缓存中,目前没有什么好的解决方法
如果你担心你访问以上设定不绕过的目录时被记录下登录状态(例如 gitea 右上角显示着自己的登录头像),你还可以设定一个 UA 规则。这个就比较简单,`传入请求匹配` 设为 `自定义筛选表达式`,再将 `字段` 设为 `用户代理` `运算符` 选择 `等于``值` 就填入自己的浏览器 UA最后 `缓存资格` 选择 `绕过缓存` 保存即可,顺序放到前面两个规则之后即可
不过这个方法可能会因为浏览器自动更新而失效,因为 UA 会有变动,你需要在每次更新浏览器后重新设定一次,而且这样的话你访问网站时无论规则如何,你自己的请求都是会回源的,加载速度没有 CDN 加成