{"slug":"static-redirect-group","title":"完全免费！搭建一个自己的短链服务！","description":"利用 Cloudflare Worker 和 GitHub，可以搭建一个纯静态、可自动维护的短链服务","coverImage":"https://b.acofork.com/img/static-redirect-group-static-redirect-group.webp","coverWidth":691,"coverHeight":702,"date":"2026-01-14","tags":["服务器"],"pinned":false,"wordCount":1012,"html":"<h2 id=\"前言\">前言</h2><p>原本这篇内容更适合写进仓库的 <code>README</code>，因为它本质上就是一个简洁的自部署教程。不过在实际整理时，我发现如果强行拆成 GitHub Pages + Cloudflare Worker 的前后端分离方案，反而会增加不少额外工作。实际上，这个项目只需要一个 Cloudflare Worker 就能跑起来，所以干脆单独写成一篇文章，方便说明整体思路。</p>\n<h2 id=\"项目原理\">项目原理</h2><p>这个项目和上一篇短链项目的思路比较接近，不过整体做得更精简一些。</p>\n<p>首先，这个项目把前后端逻辑合并到了同一套体系里。前端基本不做复杂校验，主要校验工作都放在后端处理，这样就不用在两个项目之间来回维护规则。</p>\n<p>由于前端部分非常简单，只有两个 HTML 页面（一个用于创建短链，一个用于跳转），因此合并后也不会显得臃肿。</p>\n<p>另外，这个项目不再依赖 Cloudflare 服务端提供的 301/302 重定向规则，因此也绕开了 2000 条静态重定向的数量限制。它的做法是：当 CDN 请求静态资源命中 404 时，回退到 <code>404.html</code>，再由其中的 JavaScript 负责查询短链规则并执行跳转，思路上有些类似 Nginx 的伪静态。</p>\n<p>如果某个 <code>pathname</code> 没有命中任何规则，也会统一落到默认回退源，这样就能兼容类似 <code>https://2x.nz/posts/pin/ --&gt; https://blog.acofork.com/posts/pin/</code> 这样的场景。</p>\n<p>创建短链的逻辑和上一个项目也比较相似：由 Worker 代理访问 GitHub，修改对应的 JS 文件并新增一条短链规则，然后推送到仓库。提交完成后会自动触发 Cloudflare Worker 的重新构建，稍等片刻后，新路径就能正常跳转。</p>\n<p>最后，这个项目还支持有效期。实现方式也很直接：前端在创建短链时把过期时间一并传给后端，后端将其写入规则文件，再借助 GitHub Action 定时巡检并清理过期短链。</p>\n<h2 id=\"在哪搞个短链\">在哪搞个短链</h2><p>我的 <code>2x.nz</code> 是在 <a href=\"https://porkbun.com\" target=\"_blank\" rel=\"noopener noreferrer\">https://porkbun.com</a> 购买的，价格大概是一年一百元左右。其他后缀也可以考虑，比如 <code>.im</code>、<code>.mk</code>。</p>\n<h2 id=\"正式搭建你的短链服务\">正式搭建你的短链服务</h2><p>首先，Fork仓库</p>\n<p><a href=\"https://github.com/afoim/Static_Redirect_Group\" target=\"_blank\" rel=\"noopener noreferrer\">afoim/Static_Redirect_Group</a></p>\n<p>接下来，先修改一些硬编码内容。由于 Cloudflare Worker 在处理静态资源时不能直接使用环境变量，因此部分信息是直接写在 HTML 里的。你可以在所有 HTML 文件中搜索 <code>afoim</code> 并替换成自己的内容；如果你愿意，也可以额外增加一层配置，并在构建阶段注入这些值。</p>\n<p>然后，请修改 <code>js</code> 文件夹中的短链规则，替换成你自己的配置。</p>\n<p>再接着，创建一个 GitHub Token，只需要授予 <code>repo</code> 权限即可。</p>\n<p>继续，绑定机密环境变量，使用 <code>wrangler secret put XXX</code></p>\n<table>\n<thead>\n<tr>\n<th>变量名</th>\n<th>值</th>\n<th>说明</th>\n</tr>\n</thead>\n<tbody><tr>\n<td><code>GITHUB_TOKEN</code></td>\n<td><code>ghp_xxxx...</code></td>\n<td>刚才申请的 Token</td>\n</tr>\n<tr>\n<td><code>GITHUB_OWNER</code></td>\n<td><code>你的GitHub用户名</code></td>\n<td>例如 <code>afoim</code></td>\n</tr>\n<tr>\n<td><code>GITHUB_REPO</code></td>\n<td><code>Static_Redirect_Group</code></td>\n<td>你的仓库名</td>\n</tr>\n<tr>\n<td><code>BASE_DOMAIN</code></td>\n<td><code>你的短链域名</code></td>\n<td>例如 <code>u.2x.nz</code> 或 Worker 的默认域名 <code>xxx.workers.dev</code></td>\n</tr>\n</tbody></table>\n<p>完成这些配置后，访问 <code>/_url</code> 就可以开始创建短链了。</p>\n<h2 id=\"防护\">防护</h2><p>建议重点保护创建短链的入口，避免被滥刷。你可以使用 Cloudflare Turnstile、速率限制或其他方式进行防护。</p>\n<p>在Cloudflare创建一个WAF规则</p>\n<p>当传入请求匹配时...</p>\n<div class=\"markdown-code\" data-language=\"sql\"><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><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path></svg><span>sql</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-sql\">(http.host eq \"你的域名\" <span class=\"hljs-keyword\">and</span> (\n  http.request.uri.path eq \"/_url\"\n  <span class=\"hljs-keyword\">or</span> http.request.uri.path wildcard \"/api/*\"\n))</code></pre></div><p>然后采取措施…</p>\n<p><strong>交互式质询</strong> </p>\n","toc":[{"id":"前言","text":"前言","depth":2},{"id":"项目原理","text":"项目原理","depth":2},{"id":"在哪搞个短链","text":"在哪搞个短链","depth":2},{"id":"正式搭建你的短链服务","text":"正式搭建你的短链服务","depth":2},{"id":"防护","text":"防护","depth":2}]}