Wednesday, August 29, 2007

How to enable HTTP Compression in IIS 6.0 for ASP.NET

Here is my simple guide to setting it up.

Step 1: Configuring Web Service Extensions in IIS Manager

  1. Open Internet Information Services (IIS) Manager
  2. Right-click the Web Service Extensions
  3. Click on Add a new Web Service extension...
  4. Enter Extension Name (recommend Http Compression)
  5. Click Add...
  6. Select C:\WINDOWS\system32\inetsrv\gzip.dll (alter path to your windows install directory as appropriate)
  7. Check the Set extension status to Allowed
  8. Click Ok
  9. In Web Service Extensions you should now see Http Compression and its status of Allowed
Step 2: Configuring the IIS Metabase xml file

The metabase.xml file is located at c:\windows\system32\inetsrv\MetaBase.xml and contains the compression configuration.

  1. Stop IIS
  2. Run below commands from the command prompt OR edit the metabase.xml to match the below modified version of the configuration.
  3. Start IIS

The original configuration without changes looks like this:

<IIsCompressionScheme Location ="/LM/W3SVC/Filters/Compression/deflate"
HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
HcCreateFlags="0"
HcDoDynamicCompression="TRUE"
HcDoOnDemandCompression="TRUE"
HcDoStaticCompression="FALSE"
HcDynamicCompressionLevel="0"
HcFileExtensions="htm
html
txt"
HcOnDemandCompLevel="10"
HcPriority="1"
HcScriptFileExtensions="asp
dll
exe"
>
</IIsCompressionScheme>
<IIsCompressionScheme Location ="/LM/W3SVC/Filters/Compression/gzip"
HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
HcCreateFlags="1"
HcDoDynamicCompression="TRUE"
HcDoOnDemandCompression="TRUE"
HcDoStaticCompression="TRUE"
HcDynamicCompressionLevel="0"
HcFileExtensions="htm
html
txt"
HcOnDemandCompLevel="10"
HcPriority="1"
HcScriptFileExtensions="asp
dll
exe"
>
</IIsCompressionScheme>
<IIsCompressionSchemes Location ="/LM/W3SVC/Filters/Compression/Parameters"
HcCacheControlHeader="max-age=86400"
HcCompressionBufferSize="8192"
HcCompressionDirectory="%windir%\IIS Temporary Compressed Files"
HcDoDiskSpaceLimiting="FALSE"
HcDoDynamicCompression="FALSE"
HcDoOnDemandCompression="TRUE"
HcDoStaticCompression="FALSE"
HcExpiresHeader="Wed, 01 Jan 1997 12:00:00 GMT"
HcFilesDeletedPerDiskFree="256"
HcIoBufferSize="8192"
HcMaxDiskSpaceUsage="100000000"
HcMaxQueueLength="1000"
HcMinFileSizeForComp="1"
HcNoCompressionForHttp10="TRUE"
HcNoCompressionForProxies="TRUE"
HcNoCompressionForRange="FALSE"
HcSendCacheHeaders="FALSE"
>


The modified configuration to correctly configure compression looks like this:

<IIsCompressionScheme Location ="/LM/W3SVC/Filters/Compression/deflate"
HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
HcCreateFlags="0"
HcDoDynamicCompression="TRUE"
HcDoOnDemandCompression="TRUE"
HcDoStaticCompression="TRUE"
HcDynamicCompressionLevel="10"
HcFileExtensions="htm
html
txt
ppt
xls
xml
pdf
xslt
doc
xsl
htc
js
css"
HcOnDemandCompLevel="10"
HcPriority="1"
HcScriptFileExtensions="asp
dll
exe
aspx
asmx"
>
</IIsCompressionScheme>
<IIsCompressionScheme Location ="/LM/W3SVC/Filters/Compression/gzip"
HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
HcCreateFlags="1"
HcDoDynamicCompression="TRUE"
HcDoOnDemandCompression="TRUE"
HcDoStaticCompression="TRUE"
HcDynamicCompressionLevel="10"
HcFileExtensions="htm
html
txt
ppt
xls
xml
pdf
xslt
doc
xsl
htc
js
css"
HcOnDemandCompLevel="10"
HcPriority="1"
HcScriptFileExtensions="asp
dll
exe
aspx
asmx
ashx"
>
</IIsCompressionScheme>
<IIsCompressionSchemes Location ="/LM/W3SVC/Filters/Compression/Parameters"
HcCacheControlHeader="max-age=86400"
HcCompressionBufferSize="8192"
HcCompressionDirectory="%windir%\IIS Temporary Compressed Files"
HcDoDiskSpaceLimiting="FALSE"
HcDoDynamicCompression="TRUE"
HcDoOnDemandCompression="TRUE"
HcDoStaticCompression="TRUE"
HcExpiresHeader="Wed, 01 Jan 1997 12:00:00 GMT"
HcFilesDeletedPerDiskFree="256"
HcIoBufferSize="8192"
HcMaxDiskSpaceUsage="100000000"
HcMaxQueueLength="1000"
HcMinFileSizeForComp="1"
HcNoCompressionForHttp10="FALSE"
HcNoCompressionForProxies="FALSE"
HcNoCompressionForRange="FALSE"
HcSendCacheHeaders="FALSE"
>

Here is a couple of commands you can run to automatically update your compression configuration to get the above settings:

cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcFileExtensions "htm" "html" "txt" "ppt" "xls" "xml" "pdf" "xslt" "doc" "xsl" "htc" "js" "css"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "dll" "exe" "aspx" "asmx"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcDoDynamicCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcDoOnDemandCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcDoStaticCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/DEFLATE/HcDynamicCompressionLevel "10"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcFileExtensions "htm" "html" "txt" "ppt" "xls" "xml" "pdf" "xslt" "doc" "xsl" "htc" "js" "css"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "dll" "exe" "aspx" "asmx" "ashx"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcDoDynamicCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcDoOnDemandCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcDoStaticCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcDynamicCompressionLevel "10"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/Parameters/HcDoDynamicCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/Parameters/HcDoOnDemandCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/Parameters/HcDoStaticCompression "TRUE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/Parameters/HcNoCompressionForHttp10 "FALSE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/Parameters/HcNoCompressionForProxies "FALSE"
cscript.exe C:\Inetpub\AdminScripts\adsutil.vbs set W3Svc/Filters/Compression/Parameters/HcNoCompressionForRange "FALSE"


Note: I tried adding the .AXD extension for Web Resources and it works util I tried using AJAX.ASP.NET. The ScriptManager in AJAX library does its own Gzip compression of its .AXD files. This custom behavior causes the response to be double Gzip encoded resulting in a corrupted response. So far, I found no way to disable the ScriptManager from performing the compression of its own the resources. So sad. Seems like the ASP.NET team forgot to discuss with the IIS team what it was capable of doing.

Step 3: Check to make sure it works

I recommend using YSlow (FireFox Plug-in) and FireFox to check to see it your getting the compression for your web site and its components (css, js, etc.) You should now see that your page and its associations are now compressed. I did notice that small files (<3k) didn't seem to get compressed.

No comments:

There was an error in this gadget