After integrating Amazon Cloudfront CDN with your current hosting, the next step you need to do probably to make use the benefit of compression as much as possible. Most of the modern browser is now supporting gzip-compression as standard feature. Amazon S3 Cloudfront CDN generally does not support on-the-fly gzip facility, but implementing gzip with Cloudfront could be easier than you think.
Store Compressed and uncompressed both files on the AMAZON s3 Cloud
- As we know Amazon doesnot support on-the-fly gzip-ing. What We can do is the gzip compression manually and upload it to cloudfront. Using 7zip(free) or many other compression utility, we can create the compressed version of any static file(css/js/etc..). So now we have to put 2 versions of static files on CDN the normal version (in case a browser ll not support compression) and the compressed(gzip) version.
- For example: i have a CSS file from my website: stylesheet.css. Now i ll have have 2 copies : stylesheet.css (normal version) and style.css.gz (compressed version produced using gzip with any gzip software).
- Now the point is that we have to upload this gzip compressed file onj Amazon Cloudfront CDN with a renaming.For example,rename stylesheet.css.gz above to stylesheet.gz.css.The reason is some applications only check for the last extension instead of checking “Content-Type” on the header. So, in this case the program will still know that the file is actually a css file to describe the stylesheet.
Note: “Content-Type” header is usually generated automatically by AmazonCloudfront based on the extension. - Now finally we have both versions (stylesheet.css and stylesheet.gz.css) uploaded on on Amazon S3 Cloudfront.
One extra Header only for compressed version (e.g:stylesheet.gz.css) with
“Content-Encoding” and the value is “gzip”Now Confirm the content of the header:
- Normal file(stylesheet.css): “Content-Type: text/css”
- Zipped file (stylesheet.gz.css) : “Content-Type: text/css” and “Content-Encoding: gzip”.
Note: Dont forget to set the appropriate access privilege (ACL setting)
Little change in Calling Static files from Amazon CDN Cloudfront:
Modify Application index file for calling either zipped or normal version. The Logic is if the browser supports gzip, then we will point to compressed version. If it doesn’t support compression, ll call the normal version. A few additional code below to have comperssed or normal version depends on whether browser supports compressed version or not
1 <?php $middle_fix_gzstr = (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], ‘gzip’)) ? ”.gz” : ”";2 3 //Calling for CSS changed a little depends on the condition4 ?>5 <link rel=”stylesheet” type=”text/css” href=”http://[AMAZZON CDN URL]/stylesheet<?php echo $middle_fix_gzstr;?>.css” media=”all” />
So, by using this modification, we can do overcome the limitation of gzip on-the-fly compression from Amazon CDN to deliver even better performance for website.
Let’s Spread the Technique !


Recent Activities