Reduce file sizes and increase response performance by enabling GZip compression of text-based responses. Can have significant impact on the perceived speed of website page loads.
You will need to have compression installed and configured on the IIS server before using this rule. If necessary, you can find instructions for installing compression here.
Bear in mind that the location of the configuration directives differ on IIS 10 and IIS versions below 10. For IIS 10, you configure compression in web.config. For IIS 7.x and 8.x you configure compression in applicationHost.config.
The following block enables compression for static assets and should be added inside the <system.webServer> node in the appropriate configuration file:
<system.webServer>
<httpCompression
directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="false" />
</system.webServer>
For a more complete treatment of compression in IIS, see this Microsoft IIS Compression Overview article.
In More Detail
Web servers utilize gzip to compress data returned to clients. When a gzip-enabled browser sends a request, it adds ‘gzip’ to its Accept-Encoding request header.
When the web server then receives the request it processes the response as it would normally and then checks the Accept-Encoding request header to determine how to encode the response. If the requesting browser supports gzip and the server also supports gzip, it compresses each compressible resource using the gzip algorithm and returns those assets with an added Content-Encoding header, specifying that the resource has been encoded using gzip. The browser then decompresses that compressed content into its original, uncompressed format before rendering it in the browser.
Compression comes at a cost, however. Compression is a CPU-intensive process, and the more you compress a response, the longer it will take. Given this size vs speed trade-off, gzip offers compression levels from 1 to 9, where 1 provides the fastest speed but with a lower compression ratio, and 9 offers the highest compression ratio but at a slower speed. By default, the setting is typically 6 to slightly prefer smaller file sizes over speed.