I am building my own CDN in PHP and I am thinking that having Apache serving static files must be the optimal strategy, so a well crafted .htaccess file handling all headers correctly sounds like the optimal way - given I will make this myself. So how do I put this together?
For the sake of argument, I will think of a wordpress installation which have the assets located at //wpdomain.com/wp-content/uploads/...* (maindomain) and my CDN will be a second domain //cdn.wpdomain.com/wp-content/uploads/...* (cdndomain) mirroring the contents from the main domain.
Rewriting URLS for all assets in "/wp-content/uploads/" to CDN domain will move all traffic for the assets to CDN domain. A simple .htaccess setup on CDN domain I can rewrite all urls and match the actual asset, copy it from the original domains folder and serve it. Several ways of doing this, however I need to take it one step further and optimize the contents as well.
THIS IS WHAT I WANT TO DO
1a. rewrite assets urls from maindomain to cdndomain to offload traffic.
1b. rewrite should add device, so that I can serve mobile, tablet and desktop assets
2a. serve all assets from cdn optimal from server aspect meaning minimal server load, i assume this is static files from cookieless domain and apache.
2b. assets missing needs to be created instantaneously if asset exist on maindomain, based on device method asset will be retrieved optimized saved and finally served.
I made a flowchart for what I am thinking would be the right logic:
My question then would be:
What considerations should be taken for .htaccess and setup for apache to serve the assets by using minimal cpu cycles for processing, and how do I trigger the creation of assets that are missing?
I also made a repository on my github to track my progress here: github: My Own PHP CDN
- How do I configure .htaccess to serve assets for optimal performance given I want apache to serve the files with correct and best practise headers
- How do I trigger the process of creating the asset when it's requested the first time, did I cover it in my flowchart? My current .htaccess file looks like this: link to .htaccess file
- Optimizing the assets, what are the best way of pleasing google pagespeed requirements for image compression - what are the specs for "mobile" image usage?
Question for post processing of assets: Say I mirror the assets, I optimize them at best knowledge when mirroring but I want it a step further. Wordpress as example and many websites uses responsive images, so that the width and height are not known. It would be great to add theese spects aswell to the CDN for optimal delivery. How can this be done?
Anyone care to share some thoughts here? Note I am MIT licensing my CDN project aswell so your help will be rewarded with a free CDN in the end :)