惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

奕奕 Blog

法国和西班牙能在世界杯决赛相遇吗? OpenViking 记忆提取挂了三天,最后发现是模型太聪明 奕奕 Blog: OpenViking 记忆提取挂了三天,最后发现是模型太聪明 Hermes Mini App 监控面板开发实录 OCI ARM 实例的 iowait 假象 奕奕 Blog: OCI ARM 实例的 iowait 假象 当“家”的重担,悄然落在肩头 奕奕 Blog: 当“家”的重担,悄然落在肩头 我的抖音号被“永封” 奕奕 Blog: 我的抖音号被“永封” 鱼缸炸缸风险评估 奕奕 Blog: 鱼缸炸缸风险评估 烟雨西北 奕奕 Blog: 烟雨西北 三色发糕制作教程 奕奕 Blog: 三色发糕制作教程 QQ官方机器人无需审核快速发布正式版 奕奕 Blog: QQ官方机器人无需审核快速发布正式版 win11代理设置劫持--自动打开“使用设置脚本” 奕奕 Blog: win11代理设置劫持--自动打开“使用设置脚本” AI模型的Token到底是什么 奕奕 Blog: AI模型的Token到底是什么 为什么我选择放弃"暗色模式切换按钮" 奕奕 Blog: 为什么我选择放弃"暗色模式切换按钮" QQ机器人插件 奕奕 Blog: QQ机器人插件 Blogger优化,解决 Blogger 动态插入外部资源的问题 个人主页代码 奕奕 Blog: 个人主页代码 隐藏Waline的登录按钮 奕奕 Blog: 隐藏Waline的登录按钮 谷歌云GCP 永久免费服务器申请教程 奕奕 Blog: 谷歌云GCP 永久免费服务器申请教程 当AI 看到你的博客 奕奕 Blog: 当AI 看到你的博客 决定恢复 Blogger 版权信息 奕奕 Blog: 决定恢复 Blogger 版权信息 学习戴隐形眼镜小窍门 奕奕 Blog: 学习戴隐形眼镜小窍门 让 Blogger 标签栏默认展开 利用CDN边缘函数缓存外部文件 孩子出院了 奕奕 Blog: 孩子出院了 操啊,女儿进了新生儿科 奕奕 Blog: 操啊,女儿进了新生儿科 迎接新生命:我的女儿锦瑶诞生记 奕奕 Blog: 迎接新生命:我的女儿锦瑶诞生记
奕奕 Blog: 利用CDN边缘函数缓存外部文件
奕奕Blog · 2025-02-21 · via 奕奕 Blog

因为一些特殊原因,我有一个比较小众的需求:希望利用CDN的边缘函数来缓存一个外部文件。可能大多数人用不到,但还是记录一下这个思路。

最开始的想法很简单,通过边缘函数实现一个跳转。当用户访问 a.com/logo.jpg 时,自动跳转到 b.com/logo.jpg。这样就能在不存储该文件的情况下,依然使用 a.com/logo.jpg 的路径访问它。

如果只是跳转,访问体验其实不太好,毕竟多了一次重定向。于是想到,是否可以直接让 CDN 缓存 b.com/logo.jpg,这样访问 a.com/logo.jpg 时,就能直接从缓存中获取,而不用每次都请求源站。

最终实现的代码如下:


// 目标图片地址
const targetUrl = 'https://b.com/logo.jpg';

async function handleRequest(request) {
    const url = new URL(request.url);

    if (url.pathname === '/logo.jpg') {
        // 构造新请求,避免原始请求的 headers 影响缓存
        const newRequest = new Request(targetUrl, {
            headers: new Headers({
                'User-Agent': request.headers.get('User-Agent')
            })
        });

        // 从源站获取图片
        const response = await fetch(newRequest);

        // 创建一个可缓存的响应
        const cachedResponse = new Response(response.body, {
            status: response.status,
            headers: {
                'Content-Type': response.headers.get('Content-Type'),
                'Cache-Control': 'public, max-age=86400' // 缓存 1 天
            }
        });

        return cachedResponse;
    }

    // 其他请求正常处理
    return fetch(request);
}

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
});

这样,每次访问 a.com/logo.png,CDN 都会从缓存中直接返回图片,提高访问速度。如果缓存过期,CDN 会自动从 b.com 获取最新的文件并重新缓存。

整体来说,这个方法适用于那些无法在源站存储文件,但又希望使用特定域名访问的场景。虽然是个小众需求,但或许对部分人来说有参考价值。

版权所属:奕奕博客 版权