如何启用IIS7对静态文件(如JS和CSS)进行gzip压缩,以及如何测试IIS7是否确实在将它们发送到客户端之前进行了gzip压缩?
如何启用IIS7对静态文件(如JS和CSS)进行gzip压缩,以及如何测试IIS7是否确实在将它们发送到客户端之前进行了gzip压缩?
您可以在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>
为了测试压缩是否有效,使用Chrome浏览器中的开发工具或Firefox浏览器中的 Firebug插件并确保HTTP响应头已设置:
Content-Encoding: gzip
注意,如果响应代码为304(未修改),则该标头将不存在。如果是这种情况,请进行全面刷新(按住Shift或Control键,同时单击刷新按钮),然后再次检查。
在 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消息的所有信息,包括压缩、文件大小(可以与服务器上的文件大小进行比较)。
http://support.esri.com/en/knowledgebase/techarticles/detail/38616
另一种无需安装任何东西的测试方法,也不依赖于IIS版本。将您的URL粘贴到此链接 - SEO检查中即可。
添加到 web.config 文件中:http://www.iis.net/configreference/system.webserver/httpcompression
尝试安装了Firebug插件的Firefox浏览器。我正在使用它,这是一个非常适合Web开发人员的工具。
我还在我的IIS7中使用web.config启用了Gzip压缩。
appcmd set config /section:urlCompression /doStaticCompression:True
。 - j0ffe<httpCompression>
和<urlCompression>
部分是不够的,因为只有当文件经常被访问时,IIS才会考虑对其进行压缩。因此,您需要调整此设置<serverRuntime frequentHitThreshold="some_val_here" frequentHitTimePeriod="some_val_here" />
。更多信息请参见:https://dev59.com/K2w05IYBdhLWcg3w72M0#15626981 <br>如果您决定通过Notepad++或任何其他32位编辑器在x64位Windows上编辑%windir%\System32\inetsrv\config\applicationHost.config
,则可能需要使用另一个路径,请参见:http://forums.iis.net/t/1151982.aspx - Alec