I recently moved the heavy static files of a browser game off a small VPS and onto a Bunny CDN pull zone. The page sends Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Embedder-Policy: require-corp, which makes the browser strict about every cross-origin file it loads, and the same VPS also serves a login API and a session-gated config file that must never be reachable through the CDN. The first version worked in a browser, but an audit afterwards found ways it could still go wrong: anyone could invent URLs that the CDN would cache, a loose URL pattern let stray paths through to the origin, and a one-word mistake in an edge rule made the CDN cache error responses for a year. This guide shows the setup I ended up with, in the order I would build it again, so you can skip the audit.
Everything below uses example.com for the origin and static.example.com for the CDN hostname. The origin runs nginx, DNS is on Cloudflare, and the CDN is a Bunny pull zone. The ideas carry over to other CDNs, but the edge rule details are Bunny's.
Why a pull zone with versioned URLs
A pull zone fetches files from your origin the first time someone asks for them and caches them at the edge. There is no storage zone to keep in sync, so your deploy keeps copying files to the one server it already knows. The price is that the CDN will happily cache whatever your origin answers, including mistakes.
The way to make that safe is to put a version in the URL and never change what a version contains. I hash every file that is allowed to be served, together with its path, and take the first 12 hex characters. The CDN path is /v<hash>/.local/generated/.... Because a given hash always maps to the same bytes, the CDN may cache it for a year with immutable, and a new deploy simply gets a new prefix. Rolling back means pointing the page at the old prefix, which is still cached.
python
# File: tools/assets_version.pydefversion(files, generated):
digest = hashlib.sha256()
for path in files:
digest.update(path.relative_to(generated).as_posix().encode() + b'\0')
digest.update(hashlib.sha256(path.read_bytes()).digest())
return digest.hexdigest()[:12]
The script only hashes files that nginx actually serves, which it learns by reading the location blocks in static.conf. That matters because research dumps and backups often sit in the same folder, and a version that changes whenever one of them does would defeat the point of caching.
DNS and the pull zone
Create the pull zone with your origin URL, https://example.com, and set the origin host header to example.com so nginx picks the right virtual host. Then add a custom hostname, static.example.com, to the zone and create a CNAME for it pointing at the zone's b-cdn.net host. If your DNS is on Cloudflare, switch that record to DNS only (grey cloud). With the orange cloud on, requests go through Cloudflare first and you stack two CDNs, which breaks the cache behaviour you are trying to control.
Turn on Force SSL for the hostname and request the free certificate from the dashboard. You can check the result from a terminal.
bash
dig +short @1.1.1.1 static.example.com
# CNAME to the b-cdn.net host, then a Bunny IP, not a 104.21.x or 172.67.x Cloudflare IP
curl -sI http://static.example.com/v123456789abc/.local/generated/netfields.json | head -1
# HTTP/1.1 301 Moved Permanently
Two details here surprised me. Force SSL only redirects URLs that pass your edge rules, so a plain http:// request to a blocked path returns the block response instead of a redirect. Check the certificate dates and the subject alternative name as well, so you know the hostname is served with its own certificate.
Making the origin accept only versions it knows
The pull zone cannot know which hash is current, so the origin has to. My first nginx rule stripped any /v followed by twelve hex characters and served the file underneath. That works, and it is also wrong: /v000000000000/.local/generated/netfields.json returned 200 and Bunny cached a separate copy of it. Anyone can mint cache keys this way, and every one costs you an origin fetch and egress.
The fix is a registry. A small file on the server lists the accepted versions, and nginx returns 404 for any versioned URL that is not in it.
nginx
# File: deploy/vps/static.conf (above the server block)
map $uri $cdn_version { "~^/v(?<v>[0-9a-f]{12})/\.local/generated/" $v; default ""; }
map $cdn_version $cdn_version_ok { "" ""; default 0; include /srv/site/asset-versions*.map; }
server {
# ...
if ($cdn_version_ok = 0) { return 404; }
rewrite "^/v[0-9a-f]{12}(/\.local/generated/.+)$" $1 last;
# ... the normal allow-list of locations follows
}
The first map pulls the 12 characters out of the path when the URL is versioned, and yields an empty string for everything else. The second map looks that string up in the registry. A registered hash gives 1, an unknown one gives 0, and a non-versioned URL gives an empty string, so the if only fires for versioned URLs whose hash was never registered. The registry is included with a glob, so the site still starts when the file does not exist yet.
The registry file is one line per version, 07279d7f21cb 1;, and it lives next to the site on the server, not in Git. I keep the last three versions so a browser holding an older page can still resolve its assets after a deploy.
Edge rules that block everything else and set the headers
The pull zone sits in front of your whole origin, including the login API. The first edge rule makes the CDN useless for anything except the assets. It blocks every request that does not match the versioned asset path, using the condition mode NONE.
text
Edge rule 1: Block Request
Condition: match NONE of
Request URL https://static.example.com/v*/.local/generated/*
Anchor the pattern with the hostname. I first used */v*/.local/generated/*, and a path like /foo/v/.local/generated/netfields.json slipped through to the origin, which then answered 404 and gave Bunny something to cache. With the host in front, that path is a 403 at the edge.
The second rule adds the headers the page needs. Because of COEP require-corp, every cross-origin file must say it may be embedded, and cross-origin fetches need CORS. The year-long cache header goes here too, since your origin probably sends no-cache for everything else.
text
Edge rule 2: Set Response Header (create it twice, once per status code)
Cross-Origin-Resource-Policy: cross-origin
Access-Control-Allow-Origin: *
Cache-Control: public, max-age=31536000, immutable
Conditions: match ALL of
Request URL */v*/.local/generated/*
Status Code 200 (second copy: 206)
The word that cost me the most is ALL. When I first wrote this rule the match mode was ANY, which means OR, so the URL pattern alone was enough and the status condition did nothing. A 404 for an unknown hash then received the immutable header too, and Bunny cached it for a year. Switching to ALL fixed it: an error response keeps the origin's no-cache and is never stored. The second copy for status 206 is needed because range requests answer with 206, and without it they lose the CORP header and the browser refuses them under COEP.
In the pull zone's caching settings, leave "Cache error response" off, as a second guard, and set a bandwidth warning. Mine is 10 GB a month, against roughly 2.8 GB if twenty players each load the full 140 MB once.
Deploying a version and switching the page to it
The page does not know about the CDN until you tell it. It reads one small file, /assets-base.json, before it loads any asset. If the file is missing or broken, every path stays same-origin.
Only paths under the generated folder are rewritten. Code, the WASM file and anything session-gated stay on the origin, which is exactly what the block rule enforces at the edge. Every place in the client that builds an asset URL goes through assetUrl, so switching is a data change, not a code change.
The deploy script does the switching in a fixed order. It registers the new version with nginx, reloads it, checks that the origin serves the new hash with a 200, checks that the CDN serves it with the right headers, and only then writes the file.
The order is the safety net. The CDN only ever sees a URL the origin has already answered with a 200, so it cannot cache a 404 for the version you are about to use. Rolling back is deleting assets-base.json, and the next page load fetches everything from the origin again.
Verifying the setup and cleaning up a bad cache
After a change, I check the zone from a terminal with a handful of requests, using the live hash from assets-base.json. The real file should return 200 with one copy of each of the three headers, and a repeat should say cdn-cache: HIT.
Compare the bytes of a few files from the origin and from the CDN with shasum, since a pull zone that quietly serves a stale or truncated copy looks fine until a model fails to parse. Finally, open the page in a real browser and read the resource list: every request under the generated folder should go to static.example.com/v<hash>/, none to the origin, and crossOriginIsolated should be true.
If you fixed a rule after something was already cached, the fix does not reach the entries that exist. Purge the exact URLs, or the whole pull zone, from the dashboard. A purge is cheap here, because the cache refills from the origin on the next request.
Conclusion
The problem was getting heavy assets onto a CDN from a cross-origin isolated page without exposing the rest of the origin or letting the cache be poisoned. The setup that solved it is a pull zone with content-hashed URLs, an origin that accepts only registered versions, a block rule anchored to the CDN hostname, and header rules that apply only to 200 and 206 responses with the match mode set to ALL. You can now build the same thing, activate a version in the right order, roll back by deleting one file, and prove each piece with a short list of requests.
Let me know in the comments if you have questions, and subscribe for more practical development guides.