启用IIS7 gzip

时间:2009-03-31 17:23:30

标签: iis iis-7 compression gzip

如何启用IIS7以gzip静态文件(如js和css)以及如何在发送到客户端之前测试IIS7是否真正gziping它们?

10 个答案:

答案 0 :(得分:240)

配置

您可以在Web.config文件中完全启用GZIP压缩。如果您在共享主机上并且无法直接配置IIS,或者您希望配置在您定位的所有环境之间传输,则此功能特别有用。

<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="message/*" enabled="true"/>
      <add mimeType="application/javascript" 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="*/*" enabled="false"/>
    </staticTypes>
  </httpCompression>
  <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
</system.webServer>

测试

要测试压缩是否有效,请使用developer tools in ChromeFirebug for Firefox并确保已设置HTTP响应标头:

Content-Encoding: gzip

请注意,如果响应代码为304(未修改),则此标头将不存在。如果是这种情况,请执行完全刷新(按住刷新按钮或按住刷新按钮)并再次检查。

答案 1 :(得分:57)

您需要在Windows功能控制面板中启用该功能:

IIS feature screenshot

答案 2 :(得分:36)

HttpModule中的全局Gzip

如果您无权访问最终的IIS实例(共享主机...),您可以创建一个HttpModule,将此代码添加到每个HttpApplication.Begin_Request事件中:

HttpContext context = HttpContext.Current;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;

<强>测试

Kudos,没有测试就没有解决方案。我喜欢使用Firefox插件“Liveheaders”它显示浏览器和服务器之间每条http消息的所有信息,包括压缩,文件大小(可以与服务器上的文件大小进行比较)。

答案 3 :(得分:5)

在windows 2012 r2下,可以在这里找到:

enter image description here

答案 4 :(得分:1)

如果您将YSlow与Firebug一起使用并分析您的页面性能,YSlow肯定会告诉您页面上的工件不是gzip!

答案 5 :(得分:1)

如果您还尝试gzip动态页面(如aspx)并且它无法正常工作,可能是因为未启用该选项(您需要使用Windows功能安装动态内容压缩模块):

http://support.esri.com/en/knowledgebase/techarticles/detail/38616

答案 6 :(得分:1)

我只需要像查理提到的那样在Windows功能中添加该功能。对于在窗口10或服务器2012+上找不到它的人,请按以下方式查找。我有点挣扎

Windows 10

enter image description here

Windows Server 2012 R2

enter image description here

窗口服务器2016

enter image description here

答案 7 :(得分:0)

尝试安装Firebug插件的Firefox。我正在使用它; Web开发人员的绝佳工具。

我已经使用web.config在我的IIS7中启用了Gzip压缩。

答案 8 :(得分:0)

另一种简单的测试方法,无需安装任何东西,也不依赖于IIS版本。将您的网址粘贴到此link - SEO Checkup

test gzip

添加到web.config:http://www.iis.net/configreference/system.webserver/httpcompression

答案 9 :(得分:0)

对于所有不得不与德国/德国服务器抗争的穷人:)

auf deutsch bitte schön