Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Utilize gzip Compression in IIS

0.00/5 (No votes)
25 Apr 2011 1  
Describes the importance of enabling GZIP compression in Web application

GZIP format is developed by GNU Project and standardized by IETF in RFC 1952, which MUST be considered by web developers to improve their websites' performance, there are several Quintessential articles documented using gzip compression. They are:

A gzip compressed HTTP package can significantly save bandwidth, thus speed up browser rendering after use hitting enter, and user experience got improved finally, nowadays most of the popular browsers such as Internet Explorer, Firefox, Chrome, Opera support gzip encoded content (please refer to this link).

PS: the other compression encoding is deflate, "but it's less effective and less popular" (refer to this link). Yahoo uses gzip compression and suggests developers do that:

Compression in IIS

For ASP.NET developers who host website$More$ on IIS like me, to achieve this is fairly easy, open IIS manager and select your website, then go to Compression Module. (Apache admins refer here.)

Double click and you will see:

IIS supports two kinds of compression:

  • Static Compression IIS compress a specific file at first time and only the first time, afterward every time IIS receives request on this file, it will return the compressed data. This is usually used on the files not frequently changing such as a static HTML file, a rarely changed XML, a Word document or any file doesn't change frequently.
  • Dynamic Compress IIS will do compression EVERY time a client's request on one specific file, this usually is used in some content that often changes, for example, there is a large CSV file generating engine located on the server back end, and suppose to transfer to the client side, we can use dynamica compress.

Scott Forsyth's pointed out:"Compression is a trade-off of CPU for Bandwidth.", one of the new features in IIS 7 is web masters can customize IIS compression strategy based on the actual need, you can modify the applicationHost.config under "%windir%\System32\inetsrv\config", below is my sample:

<httpCompression staticCompressionEnableCpuUsage="80" 

    dynamicCompressionDisableCpuUsage="80" 

    directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
    <dynamicTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </dynamicTypes>
</httpCompression>

More details are described here. P.S.: Dynamic Compress is not installed by default, we need to install it by turning on Windows Features:

My server host provider uses IIS 6.0 and does NOT enabling compression, I checked compression status for http://wayneye.com by using the free tool provided by port80software and the result is:

WOW, I must convince them to enable gzip compression!!

Programmatically Compress using C#

We can also programmatically compress the ASP.NET http response, for example I want to transfer a large CSVfile to the client, a simple ASPX page named ReturnGzipPackage.aspx:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Headers.Add("Content-Disposition", "attachment; filename=IAmLarge.csv");
    Response.ContentType = "text/csv";
    Response.TransmitFile("D:\\IAmLarge.csv");
    Response.End();
}

If the request was submitted by a client browser, browser will automatically decompress the Http package, but in the Windows client application or Windows Service, we developers can also adopt gzip compression to save bandwidth, once received gzip Http package from the server, we can programmatically decompress. I wrote a client console application to submit the Http request and receive/decompress the Http response package.

/* Submit Http request to server with Accept-Encoding: gzip */
WebClient client = new WebClient();
// It is mandatory by the client, if this header information is not specified, 
// server will return the original content type, in my case, it is: text/csv
//client.Headers.Add("Accept-Encoding", "gzip");

using(Stream gzipData = client.OpenRead(
    "http://localhost/StudyASPNET/gzipHttp/ReturnGzipPackage.aspx"))
{
    WebHeaderCollection responseHeaders = client.ResponseHeaders;

    using(GZipStream gzip = new GZipStream(gzipData, CompressionMode.Decompress))
    {
        using(StreamReader reader= new StreamReader(gzip))
        {
            String content = reader.ReadToEnd();
            File.WriteAllText("D:\\Downloaded.csv", content);
        }
    }
}

Please be aware of one thing: "Accept-Encoding: gzip" is supported by all browsers by default, i.e., browser will automatically decompresses compressed Http package, so in the code, we MUST explicitly specify "Accept-Encoding: gzip", below is what I investigated: First time, I explicitly set "Accept-Encoding: gzip", the Http response header contains "Content-Encoding: gzip", and the depression/file saving operations complete without any issues.

Second time, I commented out the code, the result is, received Http headers does NOT contain content encoding information, since the server deemed you don't accept gzip encoded content, it won't return you compressed file, instead, it returned the original file, in my case, the CSV file itself.

Conclusion & Hints

Using Http Compression is one of the best practices to speed up the web sites, usually consider compress files like below:

  1. Doesn't change frequently
  2. Has a significant compress-rate such as HTML, XML, CSV, etc.
  3. Dynamically generated and the server CPU has availability

Please be aware that you do NOT compress JPG, PNG, FLV, XAP and those kind of image/media files, since they are already compressed, compress them will waste CPU resources and you got a compressed copy with few KBs reduced. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here