Skip to content

Instantly share code, notes, and snippets.

@silversquirl
Last active July 2, 2025 22:57
Show Gist options
  • Save silversquirl/5c8d734ce8e5712eff8126295847e99f to your computer and use it in GitHub Desktop.
Save silversquirl/5c8d734ce8e5712eff8126295847e99f to your computer and use it in GitHub Desktop.
A guide to setting up a Zig mirror using Bunny.net's CDN

Set up a Zig mirror with bunny.net

Step 1: create a pull zone

In your bunny.net account, go to the Delivery -> CDN tab and create a new pull zone. Pick a name, set https://ziglang.org as the origin, and choose "High Volume Tier", as we're going to be serving relatively large files.

image

Step 2: create the middleware script

Next, we need to set up a middleware script to route requests to the correct location.

Switch away from the CDN page to the Edge Platform -> Scripting tab in the sidebar, and add a new script. Select "Deploy With bunny.net", as the script is very simple and doesn't need a full Git repo.

Enter a name, such as "Zig mirror middleware", and select the "Middleware" script type, then click "Add Script".

image

Replace the contents of the code editor with the middleware.ts in this Gist, replacing the value of CDN_BASE with the base URL for your mirror.

Click "Connect Pull Zone" and select the pull zone you created in step 1, then click "Publish", followed by "Publish Script".

Step 3: set up perma-caching

Finally, we need to enable perma-caching, so the CDN will store the zig archives for an extended period of time.

Navigate to the Delivery -> Storage tab, and add a new storage zone. Call it something like "zig-permacache", and select the Standard storage tier. Choose which regions you want to replicate to (the defaults are fine if you're not sure), and click "Add Storage Zone".

Once the storage zone is created, switch back to the CDN tab and select the pull zone you created in step 1. Do not use the "Connect Pull Zone" button in the storage zone settings.

In your CDN settings, select Caching -> Perma-Cache, then select the storage zone you created and click "Save Configuration".

Done!

You can test your mirror using curl, eg:

$ curl -I https://squirl-zig.b-cdn.net/zig-x86_64-linux-0.14.1.tar.xz
HTTP/2 200
date: Tue, 01 Jul 2025 03:18:51 GMT
content-type: application/octet-stream
content-length: 49086504
server: BunnyCDN-FR1-1187
cdn-pullzone: 4149629
cdn-uid: 06d8c2f4-3fdd-49b1-9680-d635eaa0ec3f
cdn-requestcountrycode: GB
cache-control: public, max-age=31919000
etag: "68308035-2ed0028"
last-modified: Fri, 23 May 2025 14:03:33 GMT
cdn-bess-version: 1.30.2
cdn-edgestorageid: 1187
cdn-proxyver: 1.31
cdn-requestpullsuccess: True
cdn-requestpullcode: 200
cdn-requesttime: 0
cdn-status: 200
cdn-cachedat: 07/01/2025 02:57:51
perma-cache: MISS
cdn-requestid: 8d840a94b714387a1a5bfb16f657a7e4
cdn-cache: HIT
accept-ranges: bytes

If you want, you can add a custom domain in your CDN settings, under General -> Hostnames.

import * as BunnySDK from "https://esm.sh/@bunny.net/[email protected]";
const CDN_BASE = "https://zig.squirl.dev/";
async function onOriginRequest(context: { request: Request }): Promise<Response> | Response | Promise<Request> | Request | void {
let path = decodeURIComponent(new URL(context.request.url).pathname);
if (path.startsWith("/builds/") || path.startsWith("/download/")) {
return;
}
let match = path.match(/^\/[^/]*-(\d+\.\d+\.\d+(-dev\.\d+\+[0-9a-f]+)?)(?:\.zip|\.tar\.xz)(?:\.minisig)?$/);
if (match === null) {
console.log(`${context.request.url} => 404`);
return new Response("404 Not Found", {
status: 404,
statusText: "Not Found",
});
}
let target = new URL(CDN_BASE);
let [filename, ver, dev] = match;
if (dev === undefined) {
// No -dev suffix; assume stable release
target.pathname = `/download/${ver}${filename}`;
} else {
target.pathname = `/builds${filename}`;
}
console.log(`${context.request.url} => ${target}`);
return new Request(target, {
headers: context.request.headers,
});
}
BunnySDK.net.http.servePullZone({ url: CDN_BASE })
.onOriginRequest(onOriginRequest);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment