本文记录用 Cloudflare Worker 反代 GitHub 图床的完整方案,涵盖 Worker 部署、PicGo 配置与 jsDelivr 备用线路。

1. 基础环境信息

项目 值
GitHub 图床仓库 zzc-blog/img-bed
目标分支 main
Worker 访问域名 https://img.zzc.dpdns.org
核心作用 反向代理 raw.githubusercontent.com,解决国内 DNS 污染、图片无法直连问题;搭载 Cloudflare 边缘缓存,加速图片访问

2. Cloudflare Worker 最终运行代码

export default {
async fetch(request) {
const url = new URL(request.url);
const path = url.pathname;

const REPO = 'zzc-blog/img-bed';
const BRANCH = 'main';
const backend = `https://raw.githubusercontent.com/${REPO}/${BRANCH}${path}`;

const cache = caches.default;
let cachedResp = await cache.match(request);
if (cachedResp) {
return cachedResp;
}

try {
const resp = await fetch(backend, {
headers: { 'User-Agent': 'Cloudflare-Worker img-proxy' }
});

if (!resp.ok) {
return new Response('Not Found', { status: 404 });
}

const headers = new Headers(resp.headers);
// 缓存30天;如需一年有效期:2592000 修改为 31536000
headers.set('Cache-Control', 'public, max-age=2592000, s-maxage=2592000, immutable');
headers.set('Access-Control-Allow-Origin', '*');

const newResp = new Response(resp.body, {
status: resp.status,
headers
});

// 独立try-catch处理缓存写入,避免缓存异常导致Worker抛出1101错误崩溃
(async () => {
try {
await cache.put(request, newResp.clone());
} catch (e) {}
})();

return newResp;
} catch (err) {
return new Response('Proxy Error: ' + err.message, { status: 502 });
}
}
};

3. Worker 部署操作流程

  1. 登录 Cloudflare 控制台,找到【Workers & Pages】,点击【创建应用程序】→【创建 Worker】
  2. 自定义 Worker 名称,完成初始部署,进入【快速编辑】页面
  3. 删除编辑器内全部默认代码,粘贴上方完整代码
  4. 点击【保存并部署】
  5. 进入【设置】→【触发器】→【添加自定义域】,绑定域名:img-proxy.zzc.dpdns.org

4. 问题踩坑汇总

报错 Error 1101 Ray ID

  • 原因:原始代码使用 ctx.waitUntil,在自定义域名环境容易触发运行异常。
  • 解决方案:移除 ctx.waitUntil,缓存逻辑单独封装异常捕获,缓存失败不中断图片正常访问。

访问返回 502 Proxy Error

  • 原因:Cloudflare 边缘节点无法连通 raw.githubusercontent.com,属于网络限制,代码无法彻底解决。

⚠️ raw.githubusercontent.com 原生链接国内无法打开,必须依靠代理或者 CDN 中转。


5. PicGo 图床配置(GitHub)

配置项 值
图床类型 GitHub
仓库名 zzc-blog/img-bed
分支 main
Token GitHub Classic Token,权限仅勾选 repo
存储路径 images/
自定义域名 https://img.zzc.dpdns.org

⚠️ 重要提醒:自定义域名末尾不要添加斜杠 /


6. 图片链接转换规则

原生 GitHub raw 链接:

https://raw.githubusercontent.com/zzc-blog/img-bed/main/images/xxx.png

代理加速访问链接:

https://img.zzc.dpdns.org/images/xxx.png

7. 使用注意事项

  • 长效缓存生效后,不要在 GitHub 仓库直接覆盖同名图片,CDN 不会主动刷新缓存;建议修改文件名重新上传
  • Cloudflare Worker 免费方案配额:每日 100,000 次请求,个人博客图床场景足够使用

8. 应急备用方案(Worker 频繁 502 时切换)

  • 使用 jsDelivr CDN,PicGo 自定义域名填写:
    https://cdn.jsdelivr.net/gh/zzc-blog/img-bed@main
  • ⚠️ 仓库必须设置为公开仓库,私有仓库无法通过代理读取图片资源

9. 图床验证记录(图片链接备份)

图床搭建过程中的验证图片(涉及 img.nesthub.dpdns.org、minio.nesthub.dpdns.org 与代理域名 img-proxy.zzc.dpdns.org):

https://img.nesthub.dpdns.org/test/719118e9bb665c4e258928762c01f95f.png
https://img.nesthub.dpdns.org/test/c855253be02db7584a7837b474bfea84.png
https://minio.nesthub.dpdns.org/bedimg/56e24ce4588c2a67e316cf66efed3ae3.png
https://minio.nesthub.dpdns.org/bedimg/266f06e1b0bba224bfba8e385638cbb7.png
https://img.nesthub.dpdns.org/test/0fea5dd36164165cef219d140bc5124e.png
https://img.zzc.dpdns.org/test/0fea5dd36164165cef219d140bc5124e.png
https://img.zzc.dpdns.org/test/03c0ec4660307cc42b9b54121c2ee9b5.png