543 字
3 分钟
rustfs s3利用cf加速下载、初步解决套了cdn鉴权失败的问题(worker版)
前言
之前尝试用minio自建s3,但是现在的镜像版本好像变得简略了,比如密钥什么都没有了、对中文也不是很支持。偶然在1panel上看见了rustfs,原生支持中文,而且使用非常简单。
但是一直困扰我的问题还是没有解决。 1.有时候用nginx反代不需要其他特殊的设置就可以加速源站了,但是有时候不行。都是默认的bt面板配置。 2.套上cdn总是遇到鉴权失败的问题,有的cdn可行,有的不行。看了很多教程,都是修改请求头···看不懂。
我尝试用了s3兼容的cdn,比如esa、和腾讯cdn,大部分情况下可用,有时候会突然访问不了。
想到用ai解决我的问题,域名托管到了cf,不会修改nginx设置,最简单的办法就是创建worker
addEventListener('fetch', event => { // 推荐加上这一行,发生异常时直接穿透到源站(比较安全) // event.passThroughOnException();
event.respondWith(handleRequest(event.request));});
async function handleRequest(request) { // 只对 GET 请求做缓存处理 if (request.method !== 'GET') { return fetch(request); }
// 构造指向源站的 URL const url = new URL(request.url); url.hostname = 'origin-us-s3.xxmoe.com'; // ← 你的源站域名
const originRequest = new Request(url, { method: request.method, headers: request.headers, redirect: 'follow' });
// 获取缓存 const cache = caches.default; let response = await cache.match(originRequest);
if (response) { console.log('Cache hit for:', request.url); return response; }
console.log('Cache miss, fetching from origin:', url.toString());
try { response = await fetch(originRequest);
// 只缓存成功的二进制/大文件类型响应(可根据需要调整) if (response.ok && response.status === 200) { // 克隆一份用于缓存 const clonedResponse = response.clone();
// 这里不能直接用 event,要用 Promise 方式缓存 // 或者干脆不用 event.waitUntil 也可以(看需求) caches.default.put(originRequest, clonedResponse); // 或者用更安全的方式: // await caches.default.put(originRequest, clonedResponse); }
return response; } catch (err) { console.error('Fetch failed:', err); // 出错了就直接返回源站错误,或者自定义错误页面 return new Response('Origin fetch failed: ' + err.message, { status: 502 }); }}尾声
gemini思考的还是太复杂了,100多行的代码还是不能用。但是grok随便50行就解决了。可能grok搜素的比较好?
rustfs s3利用cf加速下载、初步解决套了cdn鉴权失败的问题(worker版)
https://blog.xomoe.cn/posts/2026-02-04/rustfs-cf/ 页面浏览量:0 次
💡 评论需审核