Your Own Origin Pull Content Delivery Network
This post is very off-topic but I hope my readers will nevertheless forgive me. It has to do with the numerous technical experiments I am doing behind the scenes for this website. Since our funding is something around zero, we have to do these things in our “garage workshop”. Below I’d like to share some of the results, which – I hope – may be useful for others. Although I’m not a professional programmer, others can start from here and develop the idea further, the way I was inspired by others.
While the cheapest form of a Content Delivery Network is probably Coral, this doesn’t actually help you to increase the speed of loading websites but merely distributes the traffic on the Internet. So I thought about a simple way to have at least one additional server for images etc. Even if the content is not stored all around the globe and close to a user, it can still help your primary server and leverage the ability of browsers to open parallel connections. All you need to do is keep copies of the files in the same folder structure on a second server and then access them instead of the original files.
Setting up the server is no problem. But how to synchronize the files? I have experimented with FTP to upload content to the second web space, however, to no avail. So I ended up with cobbling together my own origin pull CDN. Actually, it is extremely simple but it does it’s job. And it’s free. This is how it worked for me:
First, I created a subdomain on my server – let’s assume it has the URL cdn.example.com, while the main site can be found at www.example.com. For better performance you may want to host both on different servers. PHP must be enabled, as well as URL rewriting.
Then, I created a .htaccess file and saved it on the CDN server. The content looks like that:
Besides .htaccess, I put there two additional files: One, cache.php, does the actual creation of cached files into a folder named “cache” (needs to have the necessary permissions, try 0755 or 0777). The second, purge.php, needs to be launched from time to time and will purge expired files. cache.php: This code was inspired by and and it’s initial form copied from here. Don’t forget to modify your actually main URL.
purge.php: The code is a slightly modified version of this one. This file needs to be called every x minutes or hours and will iterate through the cache directory and clean files that have expired. You can launch it with a cron job, or from one of your web pages (perhaps through an invisible iframe). If you like you can modify the expiry time.
You can use your new CDN server with WordPress in connection with plugins like W3 Super Cache or Total Cache (I haven’t tried the latter.)
Ah yes, one more thing to mention: Of course, I don’t take any responsibility whatsoever for these scripts. Use at your own risk.
EDIT: Meanwhile I have found a solution that is easier to set up and looks really good: At Speedy Mirror you can experiment with 5GB free traffic before you buy anything (currently 1TB for US$35).