GZip Compression On IIS 7.5 is not working

.Netasp.netIisGzipIis 7.5

.Net Problem Overview


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?

.Net Solutions


Solution 1 - .Net

After a lot of searching, I finally found what got compression working on my IIS 7.5. To start with, IIS will not compress a file unless it loaded often enough. That brings up the question "what does IIS consider often enough?" Well, the defaults are 2 times every 10 seconds. Yikes!

This setting can be changed in web.config, but the section needs to be unlocked first in applicationHost.config. Here are the commands:

First unlock the section:

>C:\Windows\System32\inetsrv\appcmd.exe unlock config > /section:system.webServer/serverRuntime > >Unlocked section "system.webServer/serverRuntime" at configuration path "MACHINE/WEBROOT/APPHOST".

Now that is done, edit the web.config file and add the serverRuntime element:

><?xml version="1.0" encoding="UTF-8"?>
>  <configuration>
>    <system.webServer>
>      <serverRuntime frequentHitThreshold="1" frequentHitTimePeriod="10:00:00" />
> ...

In this case, I set it to hit the file once in a 10 hour period. You can adjust the values as necessary. Here is the document that explains the serverRuntime element:

http://www.iis.net/configreference/system.webserver/serverruntime

I hope this helps get your compression working as well.

Note: you can also set the serverRuntime element up in the applicationHost.config file, but I chose to change it in the web.config because we have a number of servers and farms with various sites, and it is easier for me to control it from this level of granularity.

Solution 2 - .Net

One thing to keep in mind is that the first hit usually is returned uncompressed immediately, but spins up a thread to compress the file in the background in order to service the response with compression for future requests.

Also, have you tried using a different client (e.g. IE)?

Solution 3 - .Net

Make sure you install Dynamic Compression on the server. Add/Remove Features under IIS.

Solution 4 - .Net

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!

Solution 5 - .Net

"system.webServer configuration does not allows httpCompression at the Web site level" https://serverfault.com/questions/125139/iis7-dynamic-compression-not-success-reason-12

Why do you use configuration files? Just try to create new dummy web site with some txt file more than 2700 bytes. Also you may try to install dynamic compression module and turn it on for server ant this dummy site.

Solution 6 - .Net

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontugberkView Question on Stackoverflow
Solution 1 - .NetBrain2000View Answer on Stackoverflow
Solution 2 - .NetEricLawView Answer on Stackoverflow
Solution 3 - .NetChris KolenkoView Answer on Stackoverflow
Solution 4 - .NetDale AndersonView Answer on Stackoverflow
Solution 5 - .NetNastya KholodovaView Answer on Stackoverflow
Solution 6 - .NetMarcusView Answer on Stackoverflow