即使在applicationHost.config中允许,gzip压缩也无法正常工作

时间:2014-01-16 08:47:05

标签: asp.net web-config gzip http-compression applicationhost

我想为使用IIS7.5在ASP.NET4.5上运行的站点启用gzip压缩,但无法压缩它。

我在共享主机上,所以我不能直接在IIS中设置它。

applicationHost.config

我将其从Deny更改为Allow(我在此处读到我不应更改allowDefinition设置:How do you change the allowDefinition section attribute using appcmd in IIS 7?

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

我的网站的web.config

<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="message/*" enabled="true"/>
    <add mimeType="application/javascript" enabled="true"/>
    <add mimeType="application/x-javascript" enabled="true"/>
    <add mimeType="application/javascript; charset=utf-8" 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="application/javascript; charset=utf-8" enabled="true"/>
    <add mimeType="*/*" enabled="false"/>
  </staticTypes>
</httpCompression>    

作为上述的替代方案,我也尝试将其添加到我的web.config:

<configuration>
   <system.webServer>
      <urlCompression doStaticCompression="true" doDynamicCompression="false" />
   </system.webServer>
</configuration>

我在Windows 2008服务器管理器中看到安装了静态内容压缩,但未安装Dynamic。

然后,当我访问IIS到我的站点并进行模块压缩时,我现在看到Enable Dynamic content compression已启用(即使显然未安装)但灰显,并且检查了静态内容压缩。

enter image description here

尽管如此:即使启用了静态和动态内容压缩,我也看不到使用Fiddler进行压缩。

Fiddler中未启用Decode按钮。我还查看了http://www.whatsmyip.org/http-compression-test/http://www.gidnetwork.com/tools/gzip-test.php

但无论我做什么,当我与Fiddler核实时,我看到没有gzip压缩: enter image description here

我已经查过这些帖子了:

http://blog.arvixe.com/how-to-enable-gzip-on-iis7/

Enable IIS7 gzip

1 个答案:

答案 0 :(得分:3)

IIS上的压缩有点不可思议,因为它不会立即采取行动。 IIS在内容经常被点击之前不会压缩内容,所以看起来内容在最终被点击几次后实际上不会被压缩。

另外,请确保列出的mime类型与您从代码中传回的内容类型完全匹配。

例如对于JavaScript:

<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/javascript" enabled="true" />
    <add mimeType="application/javascript; charset=utf-8" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="application/json; charset=utf-8" 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>

可能是必需的。

以下是我几年前写的博客文章中的更多信息: http://weblog.west-wind.com/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x

另一个谈论听起来与你的相似的一些问题(请参阅评论): http://weblog.west-wind.com/posts/2007/Jun/22/IIS-7-and-JavaScript-Compression-not-working-consistently