Saturday, February 28, 2009

Enabling HTTP Compression in IIS 6.0

HTTP Compression is when a Web server receives a request for a file and instead of just serving the file to the client, it checks to see if the client browser (or application) is "Compression Enabled". If so, the Web server does a check on what type of file is being requested (this is determined by the file’s extension). If the file is marked as a static file, such as an HTML file, IIS will check the Compression Cache Temporary Directory.

Tip: To find the Compression Cache Temporary Directory, open up IIS and right-click on the Web Sites node and go to the Service tab. There is a text box that has a label next to it marked Temporary Directory, although it may not yet be enabled.

If a compressed version isn’t found, IIS will send an uncompressed version of the file to the client and a compressed version is placed in the temporary directory (IIS will only serve to the client from the temp directory). If the compressed version is found, IIS will send the file directly to the requesting client. If the requested file is a dynamic file, such as an ASP.NET Web form, then the response is dynamically compressed and sent to the requesting client (no temp directory access is ever done).

Enabling HTTP Compression on your Windows 2003 Server

There are quite a few steps to enabling HTTP Compression on your server. If you follow the steps in this article, you shouldn’t have any issues.

First, open up IIS and right-click on the Web Sites node and go to Properties. Click on the Service tab. As shown in FIGURE 1, you’ll see two options: Isolation mode and HTTP compression. If the Run WWW service in IIS 5.0 isolation mode check box is checked, IIS will run almost exactly like IIS 5.0. This means you won’t be able to take advantage of things such as Application Pools, which in my opinion are worth the upgrade to IIS 6.0 by themselves.



FIGURE 1: The Web Sites Properties dialog box

We’ll utilize the options within HTTP compression in this article:

Compress application files. Check this to compress application files. If you do select this, you must also have Compress static files checked, although you won't be warned of this need.
Compress static files. Check this to compress static files. After you do so, the Temporary directory text box is active.
Temporary directory. You can leave this at the default, which is %windir%\IIS Temporary Compressed Files, or set it to a custom folder. This is where temporary compressed static files will be stored.
Maximum temporary directory size. This option enables you to set the maximum size of the temporary directory. After the size is met, items are removed based on duration; the older files are removed and the new files are put in.
Next, go to the Web Service Extensions node. Right-click in the right pane, and click Add a new Web service extension. The New Web Service Extension dialog box appears, as shown in FIGURE 2. You can enter any name for the extension, but what others, including myself, recommend is HTTP Compression.



FIGURE 2: The Web Service Extension dialog box

Click on Add. Choose C:\WINDOWS\system32\inetsrv\gzip.dll (your path may be different, but that is doubtful), and click OK. Check the Set extension status to Allowed check box, and click OK.

IIS 6.0 Metabase Configuration - MetaBase.xml
Open up Windows Explorer and go to C:\Windows\System32\inetsrv. Find MetaBase.xml and make a copy (you can just highlight it and do a Ctrl-C, then a Ctrl-P to make a copy of MetaBase.xml). Now open up MetaBase.xml in a text editor. Find the section. Be careful, there are two sections here: one for deflate and one for gzip. We want gzip so the Location attribute of the element will have the following value:

Location ="/LM/W3SVC/Filters/Compression/gzip"

Look for the HcScriptFileExtensions section. Your default should have: asp, dll, and exe. This is where you add any extensions you want to compress for dynamic files. In my case, I added aspx.

You’ll notice many other attributes in FIGURE 3. These are the ones I find most important:

HcDoDynamicCompression. Specifies whether dynamic content should be compressed. This is important because dynamic content is by definition always changing, and IIS does not cache compressed versions of dynamic output. Thus, if dynamic compression is enabled, each request for dynamic content causes the content to be compressed. Dynamic compression consumes considerable CPU time and memory resources, and should only be used on servers that have slow network connections, but CPU time to spare.
HcDoStaticCompression. Specifies whether static content should be compressed.
HcDoOnDemandCompression: Specifies whether static files, such as .htm and .txt files, are compressed if a compressed version of the file does not exist. If set to True and a file doesn't exist, the user will be sent an uncompressed file while a background thread creates a compressed version for the next request.
HcDynamicCompressionLevel. VAL(1-10) specifies the compression level for the compression scheme, when the scheme is compressing dynamic content. Low compression levels produce slightly larger compressed files, but with lower overall impact on CPU and memory resources. Higher compression levels generally result in smaller compressed files, but with higher CPU and memory usage.
HcFileExtensions. Indicates which file name extensions are supported by the compression scheme. Only static files with the specified file extensions are compressed by IIS. If this setting is empty, no static files are compressed.
HcScriptFileExtensions. Indicates which file name extensions are supported by the compression scheme. The output from dynamic files with the file extensions specified in this property are compressed by IIS.




FIGURE 3: Essential attributes

For this example, just add aspx to the HcScriptFileExtensions section as I did, but don’t try to save. It won’t work because the file is locked by default if IIS is running.

Tip: To change this default behavior, open IIS and right-click on the top node, Internet Information Services, and check Enable Direct Metabase Edit.

Warning: The help documents state that you should use a space delimited list for the file extensions, and I have found this to be incorrect. Instead, use new lines and tabs like the following:

HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
HcCreateFlags="1"
HcDoDynamicCompression="TRUE"
HcDoOnDemandCompression="TRUE"
HcDoStaticCompression="TRUE"
HcDynamicCompressionLevel="10"
HcFileExtensions="htm
html
txt"
HcOnDemandCompLevel="10"
HcPriority="1"
HcScriptFileExtensions="asp
dll
exe
aspx">


The final step is to do an IIS shutdown and restart by right-clicking in Internet Information Services node and then click All Tasks, Restart IIS.