Assumed audience: Folks who run (or are interested in running) their own websites and know a tiny bit about DNS.
A bit of context: A reader and regular correspondent emailed me asking about how I set up cdn.chriskrycho.com—the subdomain I use for photos, important PDFs, and so on. I realized I had never written it up, so here’s what I sent back!
The short version is: I use Backblaze B2 for actually hosting the content (because it’s super cheap storage and ingress/egress), Cloudflare for DNS (as I do with everything presently), and a Cloudflare URL Rewrite Rule rewrite to map inbound traffic to that Backblaze bucket.
I use a CNAME record to map the cdn. subdomain mapped to the Backblaze bucket URL. Then I use a rewrite rule to map inbound requests to the path for files in the bucket if they are not already pointing to that path. So cdn.chriskrycho.com/some-file.pdf should map to <bucket url>/file/<bucket-name>/some-file.pdf, but cdn.chriskrycho.com/file/<bucket-name>/some-file.pdf should be left alone. That makes sure there is no infinite loop in the redirect endlessly adding file/<bucket-name> to the front of the path.
The rewrite rule I use is: If incoming requests match… > Custom filter expression. The expression I use is:
http.host eq "cdn.chriskrycho.com" and
(not starts_with(http.request.uri.path, "/file/"))
Then I set Then… > Rewrite to… > Dynamic with the value concat("/file/<bucket-name>", http.request.uri.path). I also set Query to Preserve, and then the Place at is First but in this case it doesn’t matter because it’s also the only rule I have for the domain at present!