HTTP Compression in IIS

Ok ok, you want to compress your ASPX files in IIS, but you don’t want to do it for every site, and you want to exclude certain directories. You don’t want to edit the metabase by hand, either.

No problem! Copy the below into a BAT file, run it from a command prompt, and voila! Note that you should edit the site number and directory numbers.

But before you run the below BAT, back up your metabase and make sure you know how to restore it! IIS Manager->Right-click server->Tasks -> Backup/Restore Configuration.

If you don’t feel comfortable administering IIS or running the below batch file, don’t! And even though I hate disclaimers, this script is provided AS-IS with no warranty in any way.

REM HTTP Compression Script
REM
REM Enables compression of static and dynamic files
REM Turns off compression for all sites except site #123
REM Also turns off compression for site #123's /SomeDirectory/Directory2 directory
REM
REM Copyright 2007 Ben Strackany
REM This script is provided AS-IS, no warranty implied or provided, Ben Strackany and
REM DevelopmentNow are not responsible for any damage your site, server, or love life
REM may incur as a result of running this batch file.
REM
REM BACK UP YOUR METABASE BEFORE RUNNING THIS SCRIPT!

REM compress static files
CSCRIPT.EXE C:InetpubAdminScriptsADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcFileExtensions "htm" "html" "js" "txt"
CSCRIPT.EXE C:InetpubAdminScriptsADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcFileExtensions "htm" "html" "js" "txt"

REM compress dynamic files
CSCRIPT.EXE C:InetpubAdminScriptsADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcScriptFileExtensions "asp" "asmx" "aspx"
CSCRIPT.EXE C:InetpubAdminScriptsADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions "asp" "asmx" "aspx"

REM set compression level
CSCRIPT.EXE C:InetpubAdminScriptsADSUTIL.VBS SET W3Svc/Filters/Compression/GZIP/HcDynamicCompressionLevel "9"
CSCRIPT.EXE C:InetpubAdminScriptsADSUTIL.VBS SET W3Svc/Filters/Compression/DEFLATE/HcDynamicCompressionLevel "9"


REM turn off global compression
cscript.exe C:InetpubAdminScriptsadsutil.vbs set w3svc/root/DoStaticCompression False
cscript.exe C:InetpubAdminScriptsadsutil.vbs set w3svc/root/DoDynamicCompression False

REM turn on compression for a specific site
REM ****** EDIT THE NUMBER TO BE THE SITE # YOU WANT TO TURN COMPRESSION ON FOR ****
cscript.exe C:InetpubAdminScriptsadsutil.vbs set w3svc/123/root/DoStaticCompression True
cscript.exe C:InetpubAdminScriptsadsutil.vbs set w3svc/123/root/DoDynamicCompression True

REM turn off compression for a given dir
REM note that the dir needs to exist in the metabase: in IIS, create the vdir, then optionally
REM open properties for it and click "Remove" next to the application
REM ****** EDIT THE NUMBER AND DIRECTORY TO BE THE SITE AND DIRECTORY YOU WANT TO TURN COMPRESSION OFF FOR ****
cscript.exe c:inetpubadminscriptsadsutil.vbs set w3svc/123/root/SomeDirectory/Directory2/DoStaticCompression false
cscript.exe c:inetpubadminscriptsadsutil.vbs set w3svc/123/root/SomeDirectory/Directory2/DoDynamicCompression false

REM restart IIS
iisreset.exe

 

The above is pretty self-explanatory. In the top half, we’re setting which file types should be compressed, along with the compression level. In the bottom half, we’re adjusting compression settings globally, for various sites, and for a specific directory.

The script is not only to use, but to help you understand how the HTTP Compression configuration works, so you can adjust it to suit. For example, you can include other extensions to compress (CFM, PHP), or turn on & off compression for other webs, directories, or files.

To test your settings, get yourself a copy of Fiddler and install it. Run Fiddler, and browse your site. In the left pane, click a request that you think should be compressed, then click the Session Inspector tab on the right and look in the HTTP Compression area. If your page is compressed, GZIP or DEFLATE Encoding will be selected.

fiddlergzip.png

Thanks go to posts from BlueDog, KB234497, and Scott Forsyth.

0