GZip compression in IIS7 not working, but content-

2019-03-29 11:39发布

问题:

I have IIS 7.5 with static and dynamic compression enabled. It seems to work fine for dynamic files, but for static ones it behaves erratically, often sending a http header "Content-Encoding: gzip" when the content is not compressed. This causes browsers to attempt to uncompress, throwing an invalid magic number error. Here's my configuration:

  <httpCompression dynamicCompressionDisableCpuUsage="95" dynamicCompressionEnableCpuUsage="70" >
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="application/javascript" 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/javascript" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
  </staticTypes>

I thought some http module was uncompressing the content somewhere down the pipe, but none of them seem suspicious. Any ideas?

回答1:

Try to enable dynamic compression before cache which is disabled by default.

    <urlCompression dynamicCompressionBeforeCache="true" doDynamicCompression="true" doStaticCompression="true" />


回答2:

I have found out in my investigations that using HttpContext.RewritePath() on a static file causes this problem.



回答3:

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!