0

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.

SIMPLE MIRRORING

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:

draw.io flowchart

Delivery logic v1.0

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

Questions:

  1. 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
  2. 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
  3. 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?

At the top of my head I have one idea at the moment: When rewriting assets URL in the HTML, I add an extra identifier for each image. The CDN will then mirror/populate the asset on page load, so that the page is loaded. Then when the page is loaded, javascript will meassure the image size and give my CDN a second call with the dimensions - now I can reprocess the asset for the optimal compression and width resize? The problem here is that the first page load will not have the best images, however from second visit the images will be super perfect I am guessing and giving me the possibility of making google pagespeed extremely impressed?

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.