以下配置对我有用。只需将 applicationHost.config 中的 httpCompression 部分替换为下面给出的内容,然后重新启动 IIS。就是这样!!!
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"
staticCompressionDisableCpuUsage="95" staticCompressionEnableCpuUsage="60"
dynamicCompressionDisableCpuUsage="95" dynamicCompressionEnableCpuUsage="50">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
配置完成后,我得到了下面的标头作为响应,表明数据是使用 gzip 压缩进行压缩的
Cache-Control → no-cache
Content-Encoding → gzip
Content-Length → 4202
Content-Type → application/json; charset=utf-8
Date → Wed, 22 Jul 2015 07:40:17 GMT
Expires → -1
Pragma → no-cache
Vary → Accept-Encoding
X-Powered-By → ASP.NET
以上配置是针对整个IIS的。如果您想为单个网站配置此功能,请替换
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
与
<section name="httpCompression" overrideModeDefault="Allow" />
在 applicationHost.config 中,而不是替换 applicationHost.config 中的 httpCompression 部分,将其添加到您网站的 web.config 中的 system.webServer 标记下
另外,请确保您为数据指定了正确的 MIME 类型。就我而言,它是 JSON 格式,所以我使用了以下配置
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />