启用IIS7 gzip

问题描述 投票:224回答:10

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

iis iis-7 compression gzip
10个回答
240
投票

Configuration

您可以在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>

Testing

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

Content-Encoding: gzip

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


0
投票

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

auf deutsch bitte schön


57
投票

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


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;

测试

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


5
投票

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

enter image description here


1
投票

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


1
投票

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

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


1
投票

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

Windows 10

enter image description here

Windows Server 2012 R2

enter image description here

窗口服务器2016

enter image description here


0
投票

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

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


0
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.