Sunday, January 11, 2009

Asp.net & IIS Tricks:- Using GZip Compression with IIS 6.0 to compress your dynamic content such as your aspx pages and increase the client side experience by downloading the pages fast.

Introduction

In this article i will discuss how to enable gzip compression for the dynamic content.After following these steps you can enable compression for your aspx pages and other dynamic content.

But you have to be careful while compressing the dynamic content.I would suggest you to use the dynamic compression only when although you are using asp.net but most of your webpages are more likely to be static with only few of them changing now and then.

This is due to the  reason that compression brings in overhead on the server CPU and memory.So if the content is so much dynamic that it keeps on changing with every request its not at all advisable to use the compression for your dynamic pages.

Instead you can use gzip compression for your static pages such as html,css and js files.

But if your dynamic content is also near about static then you can use gzip compression provided as a built in feature of windows and IIS 6,by default it is not enabled here in this article we will go through the steps to enable the gzip compression scheme.

Enabling this compression can also be useful for developers who are running and testing their websites projects on their dev machines and are frustrated of long loading time of the content of their test websites and web applications.

Case Study

Here the case study consists of one of my recent projects which was a public facing website build up on Telerik's Sitefinity Content Management System.

So here in my case as the client was using Sitefinity so my pages were all .aspx pages but they were rather static as we CMS enabled their existing HTML content which was almost static.

So i will provide you with the reduction in size without any significant load on the server which was achieved using the gzip compression scheme with IIS

Lets analyze the page HTML,CSS,JS before enabling the compression.Below are the images which are self explanatory.

 

So it is now quite obvious that there is a lot of reduction in the actual page size which will be transferred across the wires.

So in specific situations you should enable compression for all the content and in most of the cases at least you should enable the gzip compression for the static content.

Enabling the GZip Compression

Now let us walkthrough the steps to enable gzip compression within the IIS.

  • To enable gzip compression first open the IIS Manager using "inetmgr"  command at your run command prompt.
  • After opening the IIS Manager navigate to the website properties by right clicking the website node.(Remeber here  all images are shown from Windows Server 2003)
  • Now go to the service tab and inside the http compression section enable compress application files and compress static files both and then click ok.

 

 

    After this just navigate to the Web Service Extension section and in this section add a new extension for gzip compression.Just Click Add new Web Service Extension and name the extension as Http Compressor or anything and in the Add section select the file gzip.dll which is inside C:\Windows\System32\Inetsrv\gzip.dll and then click OK.

     

    
   After adding the web service extension now we have to modify the IIS Metabase.xml file to achieve compression for dynamic pages    such  as aspx pages.


    IIS Metabase.xml file can be found in the directory C:\Windows\System32\Inetsrv\Metabase.xml


    Make sure to have another copy of this file as it is very important and if anything goes wrong here IIS will crash on your system.So make a backup of this file somewhere in your disk.      


    Now open this file in a simple editor either Notepad or Notepad++ and search for the entry called ICompressionScheme.There may be 6 occurrences of this word so one of them is for 'deflate" another one for "gzip" and the last one "Parameters" So choose the section having gzip as given below and add the required extension in the "HcScriptFileExtensions" as shown below we have added aspx. 


    Now save this file if your editor says that this file is opened in some other application or some other application is using your file then either stop the IIS service and the word wide web publishing service and then again try it, another method is to edit the metabase.xml file while it is being opened and used by the other applications.


    So for the second scenario we have to make some changes in the IIS properties which are given below


    After enabling the direct metabase edit you can now modify the metabase.xml file without stopping the IIS.After you are done you just have to restart the IIS using "iisreset /noforce"


    Now look for the changes now you should receive compressed content from the server.


    For checking you can use various online websites and tools such as pipeboost.

http://www.pipeboost.com


    Also there is a list of websites which can do this in one of my posts

    http://smallworkarounds.blogspot.com/2008/10/websites-to-check-whether-your-pages.html


Conclusion

User gzip compression over the default deflate compression every time for your all static pages and content leaving some special cases and scenarios.

And also try for compression of your dynamic content if it is more of a static type.

Getting file reduction upto 70-95% is a lot when we are talking about transferring data on net.

 

Happy Programming!!!!....

Stay tuned for more on upcoming posts..........

Submit this story to DotNetKicks

del.icio.usSave Total0 users

20 comments:

Anonymous said...

what about IIS7?

KM Rashid said...

How Can I do this when I'm hosting my site with a web hosting company, and I dont have the control of the IIS ?

Anonymous said...

Interesting, this might come in useful. But, to play devil's advocate, did you notice that the loading times were all longer for the compressed pages above?

Giamm said...

what about overhead for zipping files?

maybe timespan gain compressing response could be almost equal to time spent for zipping(server) an unzipping (client) files.

Aashish said...

@Anonymous
"Interesting, this might come in useful. But, to play devil's advocate, did you notice that the loading times were all longer for the compressed pages above?"

Yes you are correct in saying that,but the thing is that these images are for the first time load after compression been applied so they are taking time while getting compressed on the server.For the next call the time figures will also reduce as the IIS will server the already compressed content in case of static pages and also if dynamic pages have not changed then the same reason applies to the dynamic pages also.
Also i have already mentioned that when it is advisable to use the dynamic compression and when it is not advisable to implement this dynamic compression scheme.

@Giamm
"maybe timespan gain compressing response could be almost equal to time spent for zipping(server) an unzipping (client) files"
This can be the case in some situations but not in all so its at your own sweet will,test and match the requirement of your application then implement it.

Sashi said...

I have used this in one of my projects earlier.
There are a few things to note before making a decision on using this technique.

1) Compression works fine for dynamic pages only when the content is not frequently changing.

2) The first call takes more time to fetch all the pages and compress. Subsequent requests can serve from the cache and will be faster.

3) This is efficient only for pages with lot of content and/or images. (Big page size). If used for smaller pages, the overhead in compression/decompression of the files will over shadow the quick download times.

Hope this helps.

Raj said...

Trackback from dotnetjunkies
http://www.dotnetjunkies.ddj.com/Article/16267D49-4C6E-4063-AB12-853761D31E66.dcik


What Ashish has forgotten to mention is that the two Matabase Properties (HcOnDemandCompLevel and HcDynamicCompressionLevel) can be tweakd to set the level of compression you desire. Higher compression levels generally result in small compressed files but with higher CPU and memory usage. So, one has a beefy server then by all means bump these values up, else you can experiment a little bit to get the optimum balance.

The Valid compression levels for both properties range from 1 to 10. (10 being max compression and 1 being lowest)

HcOnDemandCompLevel Metabase Property (IIS 6.0)
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/c63788cc-70b4-4a44-a9a3-329fa8fb3afb.mspx?mfr=true


HcDynamicCompressionLevel Metabase Property (IIS 6.0)
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/0d785bb2-8b9b-46db-96a5-a8343970324b.mspx?mfr=true

Also, that these compression settings apply at IIS level and will be applied to all the websites hosted under it.

Hope this helps.

Spud said...

gZip is something we looked into but shied away from as IE6 isn't fully gZip compliant. What do you do about the large % of unfortunate IE6 clients?

Otherwise, great article :)

Nishaanth said...

Nice article. You could have added the browsers compatibility list for gzip too..

Anonymous said...

Can it compress swf files. I have a flash site and i wants to know , if this can compress .flv and .swf files.

Anonymous said...

Hi I found few good tutorails on this blog.

you can see them if you like

http://aspnetcsharp4.blogspot.com/2009/05/first-look-vs2010.html
http://aspnetcsharp4.blogspot.com/2009/05/c40-new-features-list.html
http://aspnetcsharp4.blogspot.com/2009/05/variance-in-c-40.html
http://aspnetcsharp4.blogspot.com/2009/05/whats-new-in-aspnet-40.html
http://aspnetcsharp4.blogspot.com/2009/05/aspnet-40-core-services.html
http://aspnetcsharp4.blogspot.com/2009/05/auto-start-web-applications.html
http://aspnetcsharp4.blogspot.com/2009/05/ajax-and-new-template-engine-in.html
http://aspnetcsharp4.blogspot.com/2009/05/shrinking-session-state.html
http://aspnetcsharp4.blogspot.com/2009/05/routing-of-urls-capability-is-added.html

http://aspnetcsharp4.blogspot.com/2009/06/access-twitter-api-using-c.html
http://aspnetcsharp4.blogspot.com/2009/06/free-aspnet40-hosting-available.html
http://aspnetcsharp4.blogspot.com/2009/06/lazy-evaluation-feature-in-c.html
http://aspnetcsharp4.blogspot.com/2009/06/difference-between-shadowing-and.html

http://aspnetcsharp4.blogspot.com/2009/07/microsoft-aspnet-40-data-access.html
http://aspnetcsharp4.blogspot.com/2009/07/microsoft-aspnet-40-whats-next.html
http://aspnetcsharp4.blogspot.com/2009/07/ajax-client-side-templating-in-aspnet.html
http://aspnetcsharp4.blogspot.com/2009/07/jquery-in-aspnet.html
http://aspnetcsharp4.blogspot.com/2009/07/c-interview-questions.html
http://aspnetcsharp4.blogspot.com/2009/07/using-ajax-control-toolbox-with-jquery.html
http://aspnetcsharp4.blogspot.com/2009/07/microsoft-interview-questions.html
http://aspnetcsharp4.blogspot.com/2009/07/json-and-aspnet.html
http://aspnetcsharp4.blogspot.com/2009/07/aspnet-questions-with-detailed-answers.html

Thanks

Gabe Sumner said...

Very helpful article! Thanks so much for taking the time to write this.

chang said...

i like ur website. thanks for this comment posting...
Add

Anonymous said...

Hi,
My website is rewrited without using file extensions like:

http://www.domain.com/dir1/
http://www.domain.com/dir2/
http://www.domain.com/dir3/dir4/

Internally pages are .aspx, but gzip compression doesn't work.

It works if pages are accessed directly:

http://www.domain.com/page.aspx


Do you know how can I configure IIS?

Thank's!

venkat said...

Hi,

This is artical is very nice and very helpfull to me.

after I set this I found an error
"Permission denied for to call method Location.toString on"

If you any idean about this please advice.

Thanks,
Venkat

Jason Prothero said...

Thanks for the article. Worked great for me although I had the same issue above where I'm using extension-less URLs and couldn't get the dynamic part to work. However, it really helped with my CSS and JS files.

As for the IE6 issue, this article may give you some leads on a solution: http://sebduggan.com/posts/ie6-gzip-bug-solved-using-isapi-rewrite

James B Helms said...

how do you do this for something that doesn't have an extension, like JSON calls?

Boxing Equipment said...

Thank you so much!

I managed to follow this tutorial with some tweaking using Fasthosts dedicated Windows server we have.

Our sites are on classic ASP, mySQL odbc and some .NET and PHP applications. I really want to thank you for this as my site is now compressed according to Google Pagespeed for SEO and a few other sites including Pipeboost report.

http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/

Is a good article but the article has broken to links to IIS6 and IIS7. Although I enabled it, Microsoft do not go into your details.

Thanks so much! I currently get 55/100 under "Enable Compression" under the speed tool!

jeeshenlee said...

Hello,

To those who do not have access to their IIS configuration panel (like using Windows Shared Hosting at GoDaddy), you can enable GZIP compression using web.config.

Check out my blog post below on how to do so.
http://jeeshenlee.wordpress.com/2010/08/01/how-to-gzip-on-asp-net-and-godaddy/

Thanks!

asp net programmer said...

That was really informative post. I am learning programming and i found this post very helpful.

Post a Comment