以下配置对我有用。只需将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中,不要替换httpCompression部分,而是将其添加到您的网站的web.config文件中的system.webServer标记下。
此外,请确保为您的数据指定了正确的MIME类型。在我的情况下,它是JSON格式,因此我使用了以下配置:
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />