方案 <scheme>

概述

<httpCompression> 元素的 <scheme> 元素为 Internet Information Services (IIS) 7 中的 GNU zip (Gzip) 和 Deflate 压缩方案指定以下设置:

  • namedll 属性分别指定压缩方案的名称和提供程序 DLL。 IIS 7 同时支持 Gzip 和 Deflate 压缩,并且这两种压缩方案目前在 %windir%\system32\inetsrv\gzip.dll 中实现。

  • doStaticCompression 属性指定是否为 name 指定的压缩方案启用静态压缩。 将 doStaticCompression 设置为 true 可指定客户端请求时将压缩静态内容。 如果启用了静态压缩,则只要 IIS 中启用了标头中指定的压缩方案,那么对包含“Accept-Encoding: gzip”或“Accept-Encoding: deflate”标头的静态内容的请求都会导致内容压缩。 如果请求不包含此标头,或标头指定在 IIS 中未启用的压缩方案,则不会压缩内容。

    如果 IIS 确定应压缩响应,IIS 会检查由 <httpCompression> 元素的 directory 属性指定的压缩目录。 如果在目录中找到了静态文件的全新压缩版本,则会将其发送到客户端浏览器。 如果未找到文件的最新压缩版本,并且使用 <urlCompression> 元素的 dynamicCompressionBeforeCache 属性启用了按需压缩,则 IIS 将以未压缩的形式发送请求的文件,并将该文件添加到后台压缩队列中。

  • doDynamicCompression 属性指定是否为 name 指定的压缩方案启用动态压缩。 将 doDynamicCompression 设置为 true 指定客户端请求时将压缩动态内容。 如果启用了动态压缩,则只要 IIS 中启用了标头中指定的压缩方案,那么对包含“Accept-Encoding: gzip”或“Accept-Encoding: deflate”标头的动态内容的请求都会导致内容压缩。 如果请求不包含此标头,或标头指定在 IIS 中未启用的压缩方案,则不会压缩内容。

    注意

    根据定义,动态内容总是在不断变化,因此 IIS 不会缓存动态内容的压缩版本。 动态压缩会消耗大量的 CPU 时间和内存资源,只应在网络连接速度较慢且有多余 CPU 时间的服务器上使用。

  • 当 IIS 分别压缩动态或静态内容时,dynamicCompressionLevelstaticCompressionLevel 属性指定压缩方案的压缩级别。 压缩级别越低,产生的压缩文件越大,但低压缩级别产生的总体影响较小,对 CPU 和内存资源的影响也较小。 压缩级别越高,产生的压缩文件通常也越小,但 CPU 和内存使用率较高。

注意

默认情况下,IIS 7 中仅提供 Gzip 压缩方案。 若要以编程方式启用 Deflate 压缩方案,请参阅本文档的代码示例部分。

兼容性

版本 说明
IIS 10.0 <scheme> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <scheme> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <scheme> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <scheme> 元素在 IIS 7.5 中未进行修改。
IIS 7.0 IIS 7.0 中引入了 <httpCompression> 元素的 <scheme> 元素。
IIS 6.0 <scheme> 元素替换以下 IIS 6.0 元数据库属性:
  • HcCompressionDll
  • HcDoDynamicCompression
  • HcDoStaticCompression
  • HcDynamicCompressionLevel

安装

IIS 7 及更高版本的默认安装中通常提供了 HTTP 压缩功能。 但默认情况下仅安装静态压缩。 若要安装静态或动态压缩,请遵循以下步骤。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。
  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
  4. 在“服务器角色”页上,展开“Web 服务器 (IIS)”,展开“Web 服务器”,展开“性能”,然后选择“静态内容压缩”和/或“动态内容压缩”。 单击 “下一步”
    Screenshot of Performance pane expanded with Static Content Compression selected and Dynamic Content Compression highlighted.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 展开“Internet Information Services”,展开“万维网服务”,展开“性能功能”,然后选择“动态内容压缩”和/或“静态内容压缩”。
    Screenshot shows Performance Features pane in Turn Windows features on or off page expanded and Static Content Compression highlighted.
  4. 单击“确定”。
  5. 单击“关闭” 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
  3. 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
  4. 在“添加角色服务”向导的“选择角色服务”页上,如果要安装动态压缩,请选择“动态内容压缩”,如果要安装静态压缩,则选择“静态内容压缩”然后单击“下一步”。
    Screenshot shows Select Role Services page with Performance node expanded and Dynamic Content Compression highlighted.
  5. “确认安装选择”页中,单击“安装”
  6. 在“结果” 页面中单击“关闭”

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 扩展“Internet Information Services”,展开“万维网服务”,然后展开“性能功能”。
  4. 如果要安装动态压缩,请选择“HTTP 压缩动态”,如果要安装静态压缩,请选择“静态内容压缩”。
    Screenshot of Turn Windows features on or off page showing Performance Features node expanded and H T T P Common Dynamics selected.
  5. 单击“确定”。

操作方式

没有用于设置 IIS 7 的压缩方案的用户界面。 有关如何以编程方式设置压缩方案的示例,请参阅本文档的代码示例一节。

配置

特性

属性 说明
dll 必需的字符串属性。

指定与压缩方案关联的压缩 DLL 的完全限定文件系统路径和文件名。 在对此属性进行任何更改生效之前,必须重启万维网发布服务(WWW 服务)。
doDynamicCompression 可选布尔属性。

指定是否压缩对动态内容请求的响应,例如 Active Server Pages (ASP) 和 ISAPI 扩展中的脚本。 如果在个别压缩方案级别更改此值,则必须重启 WWW 服务才能让更改生效。

默认值为 true
doStaticCompression 可选布尔属性。

指定 IIS 是否压缩对静态内容请求的响应。 由于该属性不能被继承,因此如果 name 属性设置为 Gzip,则必须在全局级别l (<httpCompression>) 和单个方案级别将其设置为 true。 如果在个别压缩方案级别更改此值,则必须重启 WWW 服务才能让更改生效。

注意:静态压缩存储需要位于使用 NTFS 格式化的磁盘分区或远程共享中。 如果文件存储不是 NTFS,则会禁用静态压缩。 出于安全原因,不会从 FAT 缓存存储提供压缩响应。

默认值为 true
dynamicCompressionLevel 可选 uint 属性。

指定在压缩动态内容时压缩方案的压缩级别。 级别从 0(压缩级别最低,CPU 使用率最低)到 10(压缩级别最高,CPU 使用率最高)。 必须重启 WWW 服务才能让此属性的任何更改生效。

注意:由于动态压缩占用大量 CPU 时间和内存资源,因此仅在网络连接缓慢且 CPU 时间不足的服务器上使用。 压缩的静态响应可以被缓存,因此不会像动态响应那样影响 CPU 资源。

默认值为 0
name 必需的字符串属性。

指定压缩方案的名称。 例如,GzipDeflate
staticCompressionLevel 可选 uint 属性。

指定在压缩静态内容时压缩方案的压缩级别。 级别从 0(压缩级别最低,CPU 使用率最低)到 10(压缩级别最高,CPU 使用率最高)。 0 表示禁用压缩。 必须重启 WWW 服务才能让此属性的任何更改生效。

默认值为 7

子元素

元素 说明
remove 可选元素。

从 HTTP 压缩方案集合中移除对 HTTP 压缩方案的引用。
clear 可选元素。

从 HTTP 压缩方案集合中移除对 HTTP 压缩方案的所有引用。

配置示例

以下默认 <httpCompression> 元素是在 IIS 7 的 ApplicationHost.config 文件中配置的。 除非使用 <clear> 元素,否则此配置节将继承默认配置设置。

<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>

代码示例

以下代码示例将 deflate 压缩方案添加到 IIS 7。

AppCmd.exe

appcmd.exe set config -section:system.webServer/httpCompression /+"[name='deflate',doStaticCompression='True',doDynamicCompression='True',dll='%Windir%\system32\inetsrv\gzip.dll']" /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection httpCompressionSection = config.GetSection("system.webServer/httpCompression");
         ConfigurationElementCollection httpCompressionCollection = httpCompressionSection.GetCollection();

         ConfigurationElement schemeElement = httpCompressionCollection.CreateElement("scheme");
         schemeElement["name"] = @"deflate";
         schemeElement["doStaticCompression"] = true;
         schemeElement["doDynamicCompression"] = true;
         schemeElement["dll"] = @"%Windir%\system32\inetsrv\gzip.dll";
         httpCompressionCollection.Add(schemeElement);

         serverManager.CommitChanges();
      }
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample

   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim httpCompressionSection As ConfigurationSection = config.GetSection("system.webServer/httpCompression")
      Dim httpCompressionCollection As ConfigurationElementCollection = httpCompressionSection.GetCollection

      Dim schemeElement As ConfigurationElement = httpCompressionCollection.CreateElement("scheme")
      schemeElement("name") = "deflate"
      schemeElement("doStaticCompression") = True
      schemeElement("doDynamicCompression") = True
      schemeElement("dll") = "%Windir%\system32\inetsrv\gzip.dll"
      httpCompressionCollection.Add(schemeElement)

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var httpCompressionSection = adminManager.GetAdminSection("system.webServer/httpCompression", "MACHINE/WEBROOT/APPHOST");
var httpCompressionCollection = httpCompressionSection.Collection;

var schemeElement = httpCompressionCollection.CreateNewElement("scheme");
schemeElement.Properties.Item("name").Value = "deflate";
schemeElement.Properties.Item("doStaticCompression").Value = true;
schemeElement.Properties.Item("doDynamicCompression").Value = true;
schemeElement.Properties.Item("dll").Value = "%Windir%\\system32\\inetsrv\\gzip.dll";
httpCompressionCollection.AddElement(schemeElement);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set httpCompressionSection = adminManager.GetAdminSection("system.webServer/httpCompression", "MACHINE/WEBROOT/APPHOST")
Set httpCompressionCollection = httpCompressionSection.Collection

Set schemeElement = httpCompressionCollection.CreateNewElement("scheme")
schemeElement.Properties.Item("name").Value = "deflate"
schemeElement.Properties.Item("doStaticCompression").Value = True
schemeElement.Properties.Item("doDynamicCompression").Value = True
schemeElement.Properties.Item("dll").Value = "%Windir%\system32\inetsrv\gzip.dll"
httpCompressionCollection.AddElement(schemeElement)

adminManager.CommitChanges()