My Expierience In Using A Cdn With Yii

I dont know it this might be usfull to any1 but after some questions in the #yii freenode channel over the past few months, i decided to mock up a post about it:

CDN is usually set up to cache you content with compression and expire dates of your choice,

they should pull resources out of folders serving static content,

what i usually do is setting up 4 cdn rules, for css, js, images and html (or static pages)

i wont go into the 4th one as it depends on the situation intirely,

I extended the assets manager so the type of file (js/css/image) ends up in


/assets/css/<hash>/file.css

/assets/js/<hash>/file.js

/assets/img/<hash>/image.png

of i also server them on a different server, but thats an interly different issue

i also extended it (cause i didnt find a good extention for it) to combine and minify css and js files

I remember that i implemented this extension not so long ago.

now for rules 1, i setted up CNAME cdn.1.domain.tdl

for rule 2, CNAME cdn.2.domain.tdl

for rule 3, CNAME cdn.[3-8].domain.tdl (you usually serve tons of images vs a few js/css)

cdn.9.domain.tdl would be for static html and cdn.0.domain.tdl has nothing to do with cdn, but is a direct call to my assets path, so it ignores the caching server

so basicly, with a proper cdn, you shouldn’t have to do anything, if a file was requested and not found it the cdn, it would request the file over cdn.0.domain.tdl and cache it with the proper settings you’ve set

1 more thing, i have done this in a hacky way atm but my assets manager also stores wich image is loaded from wich cdn domain, so the browser doesnt load the file from different domains all the time creating a overhead of stored data

the different subdomains help to have your browser load images faster (there was a time browsers could only load about 4 files from 1 domain, its better now but in my expirience it still works better)

i should probably make an extension out of this all but this might depend from cdn to cdn and not sure any1 will use it, if requested ill make an extention

My static file server uses Varnish to serve cached data, its not really a CDN, but that should not make the difference.

Edit:

Added a link for the minify extension.