在IIS中启用Gzip压缩

IIS默认并不支持HTTP压缩,需要进行简单的配置

打开Internet信息服务(IIS)管理器,右击”网站”->”属性”,选择”服务”。在”HTTP压缩”框中选中”压缩应用程序文件”和”压缩静态文件”,按需要设置”临时目录”和”临时目录的最大限制”;

在Internet信息服务(IIS)管理器,右击”Web服务扩展”->”增加一个新的Web服务扩展…”,在”新建Web服务扩展”框中输入扩展名”HTTP Compression”,添加”要求的文件”为C:\WINDOWS\system32\inetsrv\gzip.dll,其中Windows系统目录根据您的安装可能有所不同,选中”设置扩展状态为允许”;

使用文本编辑器打开C:\Windows\System32\inetsrv\MetaBase.xml(建议先备份),
找到Location =”/LM/W3SVC/Filters/Compression/gzip用于设置gzip压缩,
找到Location =”/LM/W3SVC/Filters/Compression/deflate”用于设置deflate压缩.
上面两个节点紧挨着.并且设置的属性相同.

如果需要压缩动态文件,则将HcDoDynamicCompression设置为”TRUE”,并在HcScriptFileExtensions中增加您要压缩的动态文件后缀名,如aspx;如果需要压缩静态文件,则将HcDoStaticCompression和HcDoOnDemandCompression设置为”TRUE”,并在HcFileExtensions中增加您需要压缩的静态文件后缀名,如xml、css等;HcDynamicCompressionLevel和HcOnDemandCompLevel表示需要的压缩率,数值在0-10, 默认为0.
HcDynamicCompressionLevel属性说明:HcDynamicCompressionLevel Metabase Property
HcOnDemandCompLevel 属性说明:HcOnDemandCompLevel Metabase Property

说明: 这两个属性值一般推荐设置为9, 具有最佳性价比.但是在我的window server 2003上, 压缩率无论如何设置, jQuery和jQuery UI两个文件(58k/188k)压缩后的大小一直相同.(20k/45k).

一下是我的配置

<IIsCompressionScheme	Location ="/LM/W3SVC/Filters/Compression/deflate"
		HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
		HcCreateFlags="0"
		HcDoDynamicCompression="TRUE"
		HcDoOnDemandCompression="TRUE"
		HcDoStaticCompression="FALSE"
		HcDynamicCompressionLevel="10"
		HcFileExtensions="htm
			html
			txt"
		HcOnDemandCompLevel="10"
		HcPriority="1"
		HcScriptFileExtensions="asp
php
xml
			dll
			exe"
	>
</IIsCompressionScheme>
<IIsCompressionScheme	Location ="/LM/W3SVC/Filters/Compression/gzip"
		HcCompressionDll="%windir%\system32\inetsrv\gzip.dll"
		HcCreateFlags="1"
		HcDoDynamicCompression="TRUE"
		HcDoOnDemandCompression="TRUE"
		HcDoStaticCompression="TRUE"
		HcDynamicCompressionLevel="10"
		HcFileExtensions="htm
			html
			txt"
		HcOnDemandCompLevel="10"
		HcPriority="1"
		HcScriptFileExtensions="asp
php
xml
			dll
			exe"
	>
</IIsCompressionScheme>

以下是测试结果