启用IIS7 gzip

我怎样才能启用IIS7 gzip静态文件,如js和css,以及如何testing如果IIS7真的gziping他们之前发送到客户端?

谢谢!

组态

您可以完全在您的Web.config文件中启用GZIP压缩。 如果您在共享主机上,并且无法直接configurationIIS,或者希望您的configuration在所有目标环境之间运行,这一点尤其有用。

 <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

要testing压缩是否正常工作,请使用Chrome或Firebug for Firefox中的开发人员工具,并确保已设置HTTP响应标头:

 Content-Encoding: gzip 

请注意,如果响应代码是304(未修改),则此标题将不存在。 如果是这种情况,请进行全面刷新(在按下刷新button时按住shift或control键),然后再次检查。

您将需要启用Windowsfunction控制面板中的function:

IIS功能截图

HttpModule中的全局Gzip

如果您无权访问最终的IIS实例(共享主机…),则可以创build一个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; 

testing

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

在windows 2012 r2下可以find这里:

在这里输入图像描述

如果你使用YSlow和Firebug分析你的页面性能,YSlow肯定会告诉你什么文件在你的页面上不被gzip'd!

如果你也试图dynamic页面(如aspx),它不工作,可能是因为该选项未启用(您需要安装dynamic内容压缩模块使用Windowsfunction):

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

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

Windows 10

在这里输入图像描述

Windows Server 2012 R2

在这里输入图像描述

窗口服务器2016

在这里输入图像描述

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

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

另一个简单的方法来testing而不安装任何东西,也不依赖于IIS版本。 粘贴您的url到这个链接 – SEO检查

测试gzip

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