GZip Compression On IIS 7.5 is not working

Viewed 49337

I am trying to support GZip compression for my static files under IIS (which should be enabled by default but not) but not working so far. Here is the the section under <system.webServer> node inside the web.config file of the web app;

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/atom+xml" enabled="true" />
    <add mimeType="application/xaml+xml" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
</httpCompression>

<urlCompression doStaticCompression="true" />

I tried it with Google Chrome. Here are the Request Headers;

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8

Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3

Accept-Encoding:gzip,deflate,sdch

Accept-Language:en-US,en;q=0.8

Cache-Control:no-cache

Connection:keep-alive

Host:my-website-url

Pragma:no-cache

User-Agent:Mozilla/5.0 (Windows NT 6.0) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.122 Safari/534.30

These are the Response Headers;

Accept-Ranges:bytes

Content-Length:232651

Content-Type:application/x-javascript

Date:Thu, 04 Aug 2011 08:58:19 GMT

ETag:"a69135734a50cc1:0"

Last-Modified:Mon, 01 Aug 2011 12:56:37 GMT

Server:Microsoft-IIS/7.5

X-Powered-By:ASP.NET

I check the applicationHost.config file and found some nodes like below;

----

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

----

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

----

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <staticTypes>
        <add mimeType="text/*" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/x-javascript" enabled="true" />
        <add mimeType="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>

----

<urlCompression />

What am I missing here?

7 Answers

Took me a while to figure this out too. Setting the frequentHitThreshold attribute to 1 on the system.webServer/serverRuntime node in the applicationHost.config file should do the trick, as documented at http://www.iis.net/ConfigReference/system.webServer/serverRuntime.

You can do this by executing the following command as an administrator:

%windir%\system32\inetsrv\appcmd set config /section:serverRuntime /frequentHitThreshold:1 /commit:apphost

A word of warning - the "frequent hit" concept does not seem specific to compression. I have no idea whether there are other consequences as a result of setting this!

One thing we found was that our Azure website was hitting it's max CPU usage due to having a high resource WebJob running. We had tried all the settings above and nothing worked. Then we checked the Resource CPU usage and found it was 80%+. At 80% CPU load the gzip stops working!

I believe it is wort adding this as response because it is only mentioned in comments but this is the real solution, if you want to always serve compressed resources. Fiddling with staticCompressionIgnoreHitFrequency is a viable solution only if you really don't care about having someone to download big files.

The above setting will always compress, as long as the CPU utilization didn't trigger staticCompressionDisableCpuUsage.

To be 100% sure that resources are compressed you should use:

  staticCompressionIgnoreHitFrequency true 
  staticCompressionDisableCpuUsage 100
  staticCompressionEnableCpuUsage 100

For example if you wish to change it using the web.config, you can use:

<configuration>
  <system.webServer>
      <urlCompression doStaticCompression="true" doDynamicCompression="false" />
      <httpCompression staticCompressionIgnoreHitFrequency="true" staticCompressionDisableCpuUsage="100" staticCompressionEnableCpuUsage="100"/>
  </system.webServer>
</configuration>

Doing so even the first request is going to be compressed, even when the CPU has reached 100% utilization.

Remember that IIS automatically recycles the app pool based on configuration so even if you set 10 hours but your app pool resets every 1 hour, this will cause your first call after the reset to be not compressed.

Make sure you know what you are doing as GZip is still o costly operation and depending on the type of traffic you have you might cause performance issues fiddling with these settings.

For more information: https://docs.microsoft.com/en-us/IIS/wmi-provider/httpcompressionsection-class?redirectedfrom=MSDN

Moreover if interest consider using Brotli instead of Gzip, it provides higher level of compression with a lower impact on CPU performance. https://docs.microsoft.com/en-us/iis/extensions/iis-compression/iis-compression-overview

Related