Enabling GZIP Compression on IIS 7/8.5 for use with Umbraco 7

Posted by on Feb 9, 2018 in IIS, Technology, Web Development

Part of website optimization includes compressing website contents where possible. This can take the form of compressing static files (css, js, images) and/or dynamic files (items generated on the fly, like PHP or C# pages). Putting this in place for IIS 7/8.5 seemed like it should have been a pretty pedestrian operation, but proved more challenging than expected. Hopefully the step-by-step instructions I’ve put together below (mostly for my own peace of mind) will save someone else the hassle of doing the ground research again.

If interested, here is some background reading for setting on the topic:

From the pieces: “It sounds like you’re looking for applicationHost.config , which is located in C:\Windows\System32\inetsrv\config. Yes, it’s an XML file, and yes, editing the file by hand will affect the IIS config after a restart. You can think of IIS Manager as a GUI front-end for editing applicationHost.config and web.config.”

Note: There are a few extra considerations for Umbraco 7, and those are mentioned below.


Install Dynamic Compression Module

Location: Server Manager

By default, the Dynamic Compression Module is NOT installed with IIS. However, it can easily be added. In Server Manager, go to Add Roles and Features > Web Server (IIS) > Web Server > Performance and select Dynamic Content Compression. No restart should be needed.

Installation will add the following line to the applicationHost.config’s globalModules directive:

<add name="DynamicCompressionModule" image="%windir%\System32\inetsrv\compdyn.dll" />

Installation will also automatically enable dynamic compression for certain mimeTypes by adding the following to the applicationHost.config’s httpCompression directive:

<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>

And it will also lock site-based overrides by adding the following line to the applicationHost.config’s modules directive:

<add name="DynamicCompressionModule" lockItem="true" />

applicationHost.config

Location: C:\Windows\System32\inetsrv\config

First, make a backup of the applicationHost.config. Always. Do it now. =) These changes will affect all IIS sites on the box.

Change the httpCompression setting inside the system.webServer sectionGroup directive to allow site-based overrides:

<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Allow" />

Change the serverRuntime setting inside the system.webServer sectionGroup directive to allow site-based overrides:

<section name="serverRuntime" overrideModeDefault="Allow" />

Turn off dynamic caching by default. We don’t want to break any sites on the server that might be affected by this.

<dynamicTypes>
<add mimeType="text/*" enabled="false" />
<add mimeType="message/*" enabled="false" />
<add mimeType="application/x-javascript" enabled="false" />
<add mimeType="application/javascript" enabled="false" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>

Unlock the StaticCompressionModule and DynamicCompressionModule settings inside the modules directive of applicationHost.config to allow site-based overrides:

<add name="StaticCompressionModule" lockItem="false" />
<add name="DynamicCompressionModule" lockItem="false" />

web.config

Location: (in the site’s document root folder)

Place the following inside the system.webServer directive of web.config:

<!-- turn on GZIP compression for SEO purposes; note: this is disabled in some Umbraco folders later in this config -->
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files\YOURSITE.com">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true"/>
<add mimeType="message/*" enabled="true"/>
<add mimeType="application/javascript" enabled="true"/>
<add mimeType="*/*" enabled="false"/>
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<!-- override default setting for how often to allow compression (defaulted to 2 times every 10 seconds, ugh!) -->
<serverRuntime enabled="true" frequentHitThreshold="1" frequentHitTimePeriod="10:00:00" />

To prevent gziping/caching of Umbraco back-office, place this inside the configuration directive of web.config (probably already there in an Umbraco site):

<location path="umbraco">
<system.webServer>
<urlCompression doStaticCompression="false" doDynamicCompression="false" dynamicCompressionBeforeCache="false" />
</system.webServer>
</location>
<location path="App_Plugins">
<system.webServer>
<urlCompression doStaticCompression="false" doDynamicCompression="false" dynamicCompressionBeforeCache="false" />
</system.webServer>
</location>