{"slug":"random-url-gen","title":"完全免费！从架构，开发到部署，一条龙实打实的教你做一言/随机图等随机URL的最佳实践","description":"我曾于2024搭建了第一个随机图网站，最近几周又深度研究了类似项目，发现这类项目有很多坑也有很多神秘的捷径，并且某些架构还可以做到“永生”...","coverImage":"https://b.acofork.com/img/random-url-gen-random-url-gen.webp","coverWidth":1551,"coverHeight":1239,"date":"2025-12-29","tags":["开发"],"pinned":false,"wordCount":2599,"html":"<h2 id=\"探索架构\">探索架构</h2><p>我们先不讲一个抽象的概念，我们首先来做一个小项目</p>\n<p><strong>一个随机图API，每次请求都返回不同的图片</strong></p>\n<p>你会怎么做？</p>\n<p>有非常多的解决方案，就拿最简单的一说，我们可以先搞来一个服务器，然后往里面塞图片，最后写一个脚本创建一个Web服务器，接收客户端请求，每一个请求都从图片库里抽一张图返回</p>\n<p>能实现吗？</p>\n<p>当然可以！这是你的流程图！</p>\n<div class=\"markdown-code\" data-language=\"mermaid\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path></svg><span>mermaid</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-mermaid\">flowchart TD\n    A[客户端请求&lt;br/&gt;GET /random]\n    B[Web 服务器接收请求]\n    C[从图片目录读取图片列表]\n    D[随机选择一张图片]\n    E[读取图片文件内容]\n    F[构造 HTTP 响应&lt;br/&gt;Content-Type: image/*]\n    G[返回图片给客户端]\n\n    A --&gt; B\n    B --&gt; C\n    C --&gt; D\n    D --&gt; E\n    E --&gt; F\n    F --&gt; G</code></pre></div><p>但也会带来一些问题，比如，图片存在本地，给客户端响应图片的时候走的是你机子的流量，那么你就需要一个 <strong>高带宽</strong> 的服务器，这无疑是一个 <strong>高昂</strong> 的成本</p>\n<p>那可能你会有新的方案： <strong>前后端分离</strong> （逻辑与资产分离），只将返回这个图片的逻辑存放在服务器上，而图片存到其他地方，如对象存储（Cloudflare R2）、IPFS等等</p>\n<div class=\"markdown-code\" data-language=\"mermaid\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path></svg><span>mermaid</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-mermaid\">flowchart TD\n    A[客户端请求&lt;br/&gt;GET /random]\n    B[随机图逻辑服务器]\n    C[读取图片索引 / 元数据]\n    D[随机选择一张图片]\n    E[生成图片访问地址&lt;br/&gt;R2 / IPFS / 对象存储]\n    F[返回 302 重定向&lt;br/&gt;Location: 图片URL]\n    G[客户端请求图片资源]\n    H[对象存储 / IPFS / CDN]\n    I[返回图片数据给客户端]\n\n    A --&gt; B\n    B --&gt; C\n    C --&gt; D\n    D --&gt; E\n    E --&gt; F\n    F --&gt; G\n    G --&gt; H\n    H --&gt; I</code></pre></div><p>那么问题又来了，假如说你的项目太多人用了，那你的服务器性能可能不够，在后期，你仍然需要一个 <strong>高昂</strong> 的 <strong>维护成本</strong> </p>\n<p>那么那么那么，现在是 <strong>2025</strong> 年，传统的架构已经无法满足我们了，我们不妨可以试试 <strong>云函数</strong> \n仍然是前后端分离，我们现在将逻辑放到一个函数上面，如Cloudflare Worker、EdgeOne Function、Vercel Function等等</p>\n<div class=\"markdown-code\" data-language=\"mermaid\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path></svg><span>mermaid</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-mermaid\">flowchart LR\n\n    Client[客户端]\n\n    subgraph Logic[逻辑层（云函数 / API）]\n        A[接收 /random 请求]\n        B[读取图片索引]\n        C[随机选择图片]\n        D[生成访问地址]\n        E[返回 302 重定向]\n    end\n\n    subgraph Assets[资产层（静态资源）]\n        F[CDN / IPFS Gateway]\n        G[对象存储&lt;br/&gt;R2 / S3 / COS]\n    end\n\n    Client --&gt; A\n    A --&gt; B\n    B --&gt; C\n    C --&gt; D\n    D --&gt; E\n    E --&gt; Client\n\n    Client --&gt; F\n    F --&gt; G</code></pre></div><p>那么现在是不是无敌了？</p>\n<p>并非，虽然前端因为使用了 <strong>云函数</strong> 也就是直接接入了CDN，高并发已经不是问题了，但是由于资产并不直接托管在 <strong>云函数</strong> 中，<strong>云函数</strong> 仍然需要创建一个长连接从你的后端，如对象存储获取图片，这样一折腾，你的服务可能并不算快</p>\n<p>有人就会说了，那既然现在我都把我服务器丢了，前端在云，后端也在云，为什么不直接让前端的云直接存储后端的资产呢？</p>\n<p>当然可以！你已经非常接近最佳实践了！</p>\n<p>绝大部分 <strong>云函数</strong> 都支持动静结合，也就是支持你在他们的云存放一些动态脚本，再顺便 <strong>存放静态资产</strong> </p>\n<p>那么接下来，你就得到了一个完全不需要你买服务器托管，也不需要你担心存储爆仓导致天价账单的随机图...了吗？</p>\n<div class=\"markdown-code\" data-language=\"mermaid\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path></svg><span>mermaid</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-mermaid\">flowchart TB\n\n    C1[客户端1]\n    C2[客户端2]\n    C3[客户端3]\n\n    A1[请求API]\n    A2[请求API]\n    A3[请求API]\n\n    F1[云函数节点1]\n    F2[云函数节点2]\n    F3[云函数节点3]\n\n    R1{抽选一张图片}\n    R2{抽选一张图片}\n    R3{抽选一张图片}\n\n    S[云函数静态资产]\n\n    C1 --&gt; A1 --&gt; F1 --&gt; R1\n    C2 --&gt; A2 --&gt; F2 --&gt; R2\n    C3 --&gt; A3 --&gt; F3 --&gt; R3\n\n    R1 --&gt; S\n    R2 --&gt; S\n    R3 --&gt; S</code></pre></div><h2 id=\"探索随机图-随机url-的本质\">探索随机图（随机URL）的本质</h2><p>我们刚刚只是在抽象的说明某种架构 <strong>好像</strong> 可行，<strong>好像</strong> 又有什么问题，然后又有一种什么新思路 <strong>好像</strong> 可以解决这个问题</p>\n<p>但我们要走的路才刚刚开始，我们不妨思考一下，随机图，又或者说随机URL这类项目，服务器（如果有）究竟发送了什么给客户端，客户端又对服务器发回的报文执行了什么动作</p>\n<p>你肯定知道，如果想要客户端每次请求同一个URL，都返回不同的东西，那肯定是服务器针对每一个请求都返回了不同的响应，它可以是内部的，比如直接在响应体塞图片，又或者也可以是 <strong>重定向</strong></p>\n<p>直接在响应体塞图片很简单，在客户端是不可见的，当客户端请求API时，服务器直接将选中的图片作为响应体发出，在客户端看来，就好像是请求了一张图片，只不过每次刷新都不一样</p>\n<p>而响应 <strong>重定向</strong> 就更简单了，只需要让服务器发送一个 <strong>临时重定向</strong> 的状态码，比如 <strong>302</strong> </p>\n<p>有人就会说了，为什么必须要 <strong>临时重定向</strong> ？</p>\n<p>因为你肯定是想要客户端每次刷新都返回不同的图，一旦你使用了 <strong>永久重定向</strong> 如 <strong>301</strong> ，客户端在收到 <strong>301</strong> 的那一刻就会在浏览器里写一个记录：<strong>下次访问这个URL直接重定向，不再请求服务器</strong> ，这就会导致你的随机图API真的就变成一张图片了</p>\n<p>那么，这两种方法哪种更好呢？</p>\n<p>各有利弊，一句话说明：<strong>直接返回MIME类型是连请求复用，仅需一次请求即可得到图片。而返回302重定向至少需要客户端请求两次</strong> </p>\n<p>这得看你的实际架构，如果说你是前后端分离，即逻辑和资产不在一个地方，肯定是 302 好，因为如果你直接在响应体塞图片，就相当于你的服务器作为 <strong>代理</strong> 让客户端访问你的资产，流量全部走你服务器</p>\n<p>而如果说你前后端都在一起，正常情况下来说，一次请求复用肯定是比两次连接更快的，不过为了方便管理和统计，我的大部分API仍然使用 <strong>302 重定向</strong> </p>\n<p><a href=\"https://github.com/afoim/EdgeOne_Function_PicAPI\" target=\"_blank\" rel=\"noopener noreferrer\">afoim/EdgeOne_Function_PicAPI</a></p>\n<p>上线的API： <a href=\"https://eopfapi.acofork.com/pic?img=ua\" target=\"_blank\" rel=\"noopener noreferrer\">https://eopfapi.acofork.com/pic?img=ua</a></p>\n<h2 id=\"奇技淫巧1-利用cloudflare-origin-rules实现无计费的随机url\">奇技淫巧1：利用Cloudflare Origin Rules实现无计费的随机URL</h2><blockquote>\n<p>Video: <a href=\"https://www.bilibili.com/video/BV19ZBzB8EDQ/\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.bilibili.com/video/BV19ZBzB8EDQ/</a>\n起因于有一天一位粉丝在我视频下留言\n<img src=\"https://b.acofork.com/img/random-url-gen-random-url-gen-9.webp\" alt=\"\" width=\"977\" height=\"137\" loading=\"eager\" decoding=\"async\" /></p>\n</blockquote>\n<p>他提到的仓库为</p>\n<p><a href=\"https://github.com/Mabbs/cf-hitokoto\" target=\"_blank\" rel=\"noopener noreferrer\">Mabbs/cf-hitokoto</a></p>\n<p>大致为，Cloudflare在规则提供一个方法，该方法可以在规则层生成一个UUID，而UUID每次都是随机的，我们可以依据此来在规则层做随机URL</p>\n<p>理论可行，实践开始</p>\n<p>首先，我们要知道UUID是一串带有连字符的随机数，而每一位有16种可能，我们可以仅截取前4位，也就是 16^4 ，共能存储65536张图，每一张图可以分配到一个唯一的UUID，接下来让CF边缘在收到请求的时候，生成UUID，然后直接拼接URL请求静态资产，如 <code>/img/0000.webp</code> ，再返回给客户端即可</p>\n<p>那如果说我图比这多呢？加一位，16^5 = 1048576，够用了吧\n那如果说我图比这少呢？那我们可以让图片填充，说个极端的例子，假如你只有2张图，每张图创建32768个副本即可，依此类推</p>\n<div class=\"markdown-code\" data-language=\"mermaid\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path></svg><span>mermaid</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-mermaid\">flowchart TD\n\nA[客户端请求&lt;br/&gt;GET /h]\nB[Cloudflare 边缘节点]\nC{Transform Rules&lt;br/&gt;是否匹配?}\n\nD[Rewrite URL&lt;br/&gt;动态重写 Path]\nE[按原路径处理]\n\nF[生成随机字符串&lt;br/&gt;uuidv4 + random_seed]\nG[取前三位十六进制字符&lt;br/&gt;substring 0~3]\nH[拼接新路径&lt;br/&gt;/h/abc.jpg]\n\nI[Cloudflare Pages 静态站点]\nJ[命中 dist/h/abc.jpg]\nK[直接返回图片资源]\nL[客户端接收随机图片]\n\nA --&gt; B\nB --&gt; C\n\nC -- 是 --&gt; D\nC -- 否 --&gt; E\n\nD --&gt; F\nF --&gt; G\nG --&gt; H\nH --&gt; I\nI --&gt; J\nJ --&gt; K\nK --&gt; L</code></pre></div><p><a href=\"https://github.com/afoim/cf-rule-random-url\" target=\"_blank\" rel=\"noopener noreferrer\">afoim/cf-rule-random-url</a></p>\n<p>上线的API： <a href=\"https://img.072103.xyz/h\" target=\"_blank\" rel=\"noopener noreferrer\">https://img.072103.xyz/h</a> | <a href=\"https://img.072103.xyz/v\" target=\"_blank\" rel=\"noopener noreferrer\">https://img.072103.xyz/v</a></p>\n<h2 id=\"奇技淫巧2-丢掉后端-让前端js自己拼url\">奇技淫巧2：丢掉后端，让前端JS自己拼URL</h2><blockquote>\n<p>Video: <a href=\"https://www.bilibili.com/video/BV1tNB4BEEaE/\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.bilibili.com/video/BV1tNB4BEEaE/</a> \nVideo2: <a href=\"https://www.bilibili.com/video/BV1mMBKBREkB/\" target=\"_blank\" rel=\"noopener noreferrer\">https://www.bilibili.com/video/BV1mMBKBREkB/</a></p>\n</blockquote>\n<p>把思路打开，我们真的需要一个 <strong>请求一个端点，返回一个随机内容</strong> 的东西吗</p>\n<p>如果只是想在我们的网站上用上随机图，那是不是可以让客户端JavaScript代劳呢</p>\n<p>大致原理为编写一个客户端JS，生成一个随机数，然后拼接URL得到最终的随机图，然后寻找需要替换为随机图的img容器或者背景图容器，替换其中内容</p>\n<div class=\"markdown-code\" data-language=\"mermaid\"><div class=\"markdown-code-header\"><span class=\"markdown-code-language\"><svg class=\"code-language-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path></svg><span>mermaid</span></span><button type=\"button\" class=\"code-block-copy\" aria-label=\"复制代码\" title=\"复制代码\"><svg class=\"code-block-copy-icon\" aria-hidden=\"true\" width=\"16\" height=\"16\" viewbox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" xmlns=\"http://www.w3.org/2000/svg\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"></rect><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"></path></svg></button></div><pre><code class=\"hljs language-mermaid\">flowchart LR\n  subgraph build[\"构建期\"]\n    direction LR\n    src[\"原始图片\"] --&gt; buildjs[\"build.js\"]\n    buildjs --&gt; static[\"静态图片库\"]\n    buildjs --&gt; randomjs[\"random.js\"]\n  end\n\n  subgraph runtime[\"运行期（浏览器）\"]\n    direction LR\n    exec[\"random.js 执行\"] --&gt; pick[\"随机选图\"] --&gt; dom[\"DOM 注入\"]\n  end\n\n  randomjs --&gt; exec\n</code></pre></div><p><a href=\"https://github.com/afoim/Static_RandomPicAPI\" target=\"_blank\" rel=\"noopener noreferrer\">afoim/Static_RandomPicAPI</a></p>\n<p>上线的 API： <a href=\"https://pic1.acofork.com\" target=\"_blank\" rel=\"noopener noreferrer\">https://pic1.acofork.com</a></p>\n<h2 id=\"总结\">总结</h2><p>我们共探索了三种流派</p>\n<ul>\n<li>传统派：中规中矩，在边缘函数找图，取图</li>\n<li>极客派：通过CF的规则实现在边缘找图，取图，但是不计费</li>\n<li>环保派：通过客户端JS直接在浏览器上实现找图，取图，改图</li>\n</ul>\n","toc":[{"id":"探索架构","text":"探索架构","depth":2},{"id":"探索随机图-随机url-的本质","text":"探索随机图（随机URL）的本质","depth":2},{"id":"奇技淫巧1-利用cloudflare-origin-rules实现无计费的随机url","text":"奇技淫巧1：利用Cloudflare Origin Rules实现无计费的随机URL","depth":2},{"id":"奇技淫巧2-丢掉后端-让前端js自己拼url","text":"奇技淫巧2：丢掉后端，让前端JS自己拼URL","depth":2},{"id":"总结","text":"总结","depth":2}]}