IIS - Enable GZIP compression

  • 打开IIS信息服务管理程序,IIS -> Sites -> 选择站点 -> 选择右侧的 IIS 栏目中的 Compression
Use this feature to configure settings for compression of responses. This can improve the perceived performance of a Web site greatly reduce bandwidth-related charges.

[√] Enable dynamic content compression
[√] Enable static content compression
  • 修改压缩等级

查看文件:C:\Windows\System32\inetsrv\config\applicationHost.config

<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/x-javascript" 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="application/atom+xml" enabled="true" />
        <add mimeType="application/xaml+xml" enabled="true" />
        <add mimeType="image/svg+xml" enabled="true" />
        <add mimeType="*/*" enabled="false" />
    </staticTypes>
</httpCompression>

用管理员运行CMD,运行以下命令提高压缩等级(记住:手工修改该文件不一定有效果)

$ cd %systemroot%\system32\inetsrv\
$ appcmd.exe set config -section:system.webServer/httpCompression /[name='gzip'].dynamicCompressionLevel:"9" /commit:apphost

Applied configuration changes to section "system.webServer/httpCompression" for "MACHINE/WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST"

修改完后的配置文件更新为

<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" dynamicCompressionLevel="9" />
  • 运行iisreset重启IIS,启动网站后台程序

客户端启动浏览器,启动Wireshark抓包检查,可以发现gzip正常启用,值得注意的是,默认情况下IIS对动态压缩的等级设置为最低级别0,这种设置下不仅不能压缩内容,相反还会增加传输的字节长度,10为最高级别,但是同时也消耗更多的 CPU。

compressed-packet!