Creating a CDN with Backblaze B2 and Cloudflare for Free
There are plenty of CDN’s out there that deliver our assets such as jquery, bootstrap, etc. What if we want our not so popular or custom libraries on their own CDN without having to spend any money?
At the time of writing this, Backblaze B2 is offering 10gb of storage for free and combining this with Cloudflare will give you a simple to use CDN.
Setting Up B2
First thing we need to do is create a bucket in B2. Set the bucket to public as your web servers and users will need to be able to access the content within.
After you’ve created a bucket you’ll want to upload a file.
After file is uploaded
You need to get some information about your bucket before we can move on to Cloudflare. Click the i
icon on the right of your file
You are looking for the base url of your bucket which can be found here
For this example our base url is f001.backblazeb2.com
. You will need this for use in Cloudflare. Also, keep in mind the full url as you’ll need that for testing later.
Cloudflare Setup
After you’ve logged into Cloudflare and are in the DNS settings of the domain you want to use we’ll be adding a CNAME to your DNS records.
Remember that url we saved from the last step f001.backblazeb2.com?
Set that as a CNAME with Cloudflare enabled and click “Add Record”
In our example we are using blogcdn.darovi.co
as our CDN url. We need to set a few page rules to ensure that our files cache at Cloudflare’s edge locations.
Go to “Page Rules” in your Cloudflare control panel and click “Create Page Rule”.
You’ll have to setup your CDN url as a wildcard url to ensure all files are set to cache and be delivered from edge locations.
Input your CDN url in the same format as this example
You’ll also want to set some additional settings in the page rules. The following are the settings I suggest. These will ensure that your CDN is always online and caching resources to edge locations.
Sidenote
Setting the SSL setting to Full will ensure a secure connection between Cloudflare and Backblaze B2. This will also ensure that you will not receive SSL errors in your applications.
Testing your CDN
In your headers when you access a resource you will see a new value:
cf-cache-status
This header item has two status’: hit
or miss
After setting up your CDN it may take a few minutes for your resources to be cached and return cf-cache-status: HIT
Remember the full url you needed to keep when you setup your B2 bucket?
https://f001.backblazeb2.com/file/darovidemo/upload.PNG
This is our full url for our custom CDN now
https://blogcdn.darovi.co/file/darovidemo/upload.PNG
Additional Info
So far the only downfall I’ve seen from using this method is that once an item is cached if you update it you’ll need to purge it from the Cloudflare cache.
To do this go to the “Caching” settings in Cloudflare and click “Purge Individual Files”
Provide the URL that you want to purge and within a few seconds you should see the item update.