URL 压缩 <urlCompression>

概述

<urlCompression> 元素为 IIS 7 及更高版本中的静态和动态内容压缩指定以下设置:

  • <urlCompression> 元素的 doDynamicCompression 属性在站点、应用程序或文件夹级别启用或禁用动态内容压缩。
  • <urlCompression> 元素的 doStaticCompression 属性在站点、应用程序或文件夹级别启用或禁用静态内容压缩。
  • dynamicCompressionBeforeCache 属性指定 IIS 是否会动态压缩尚未缓存的内容。 当 dynamicCompressionBeforeCache 属性为 true 时,IIS 会在首次发出请求时动态压缩响应,并将内容排入队列以进行压缩。 在压缩响应被添加到缓存目录之前,会动态处理后续请求。 将压缩响应添加到缓存目录后,缓存响应将发送到客户端以获取后续请求。 当 dynamicCompressionBeforeCachefalse 时,IIS 将返回未压缩的响应,直到压缩的响应已添加到缓存目录。

注意

如果在刷新输出缓存响应后 dynamicCompressionBeforeCache 属性为 true,则在将响应放入输出缓存之前不会执行动态压缩。 但是,如果 doDynamicCompression 属性为 true,即使输出缓存已填充响应,动态压缩仍然会发生。

兼容性

版本 说明
IIS 10.0 <urlCompression> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <urlCompression> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <urlCompression> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 在 IIS 7.5 中,doDynamicCompression 属性的默认值从 false 更改为 true
IIS 7.0 <urlCompression> 元素是在 IIS 7.0 中引入的。
IIS 6.0 <urlCompression> 元素替换以下 IIS 6.0 元数据库属性:
  • DoStaticCompression
  • DoDynamicCompression
  • HcDoOnDemandCompression

安装

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

Windows Server 2012 或 Windows Server 2012 R2

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

Windows 8 或 Windows 8.1

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

Windows Server 2008 或 Windows Server 2008 R2

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

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 扩展“Internet Information Services”,展开“万维网服务”,然后展开“性能功能”。
  4. 如果要安装动态压缩,请选择“HTTP 压缩动态”,如果要安装静态压缩,请选择“静态内容压缩”。
    Screenshot of the Windows Features dialog with Http Compression Dynamic selected.
  5. 单击“确定”。

操作方式

如何为站点或应用程序启用或禁用静态和动态压缩

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,转到要为其启用压缩的连接、站点、应用程序或目录。

  3. 在“开始”窗格中,双击“压缩”
    Screenshot of the Internet Information Services Manager with Compression selected in the Home pane.

  4. 在“压缩”窗格中,勾选复选框以启用静态或动态压缩,或取消勾选复选标记以禁用静态或动态压缩。
    Screenshot of the Compression pane with dynamic content compression and static content compression enabled.

  5. 完成上述相关步骤后,单击“操作”窗格中的“应用”。

如何为服务器启用或禁用静态和动态压缩

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,突出显示服务器的名称。

  3. 在服务器的“主页”窗格中,双击“压缩”
    Screenshot of the Internet Information Services Manager with Compression selected in the server's Home pane.

  4. 在“压缩”窗格中,勾选复选框以启用静态或动态压缩,或取消勾选复选标记以禁用静态或动态压缩。
    Screenshot of the server's Compression pane with dynamic content compression and static content compression enabled.

  5. 完成上述相关步骤后,单击“操作”窗格中的“应用”。

如何配置 staticCompressionIgnoreHitFrequency

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中选择服务器,然后双击“配置编辑器”。

  3. 在“配置编辑器”中,对于“部分”,选择“system.webServer”,然后选择“httpCompression”

  4. 对于 staticCompressionIgnoreHitFrequency,请输入 True 禁用仅静态文件在一段时间内达到特定次数时才进行压缩的行为,或输入 False 以启用该行为。
    Screenshot of the Configuration Editor pane with static Compression Ignore Hit Frequency set to True.

  5. 在“操作”窗格中,单击“应用”

配置

特性

属性 说明
doDynamicCompression 可选布尔属性。

指定是否为 URL 启用动态压缩。

注意:IIS 7.0 中,由于处理器利用率增加可能降低 Web 服务器的整体性能,因此默认禁用动态压缩的使用。 在 IIS 7.5 中,对动态压缩进行了更改,从而提高了性能,因此在 IIS 7.5 及更高版本中默认启用动态压缩。

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

指定是否为 URL 启用静态压缩。

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

制定是否在将当前可用的响应放入输出缓存之前对其进行动态压缩。

默认值为 false

子元素

无。

配置示例

以下配置示例为默认网站启用静态压缩并禁用动态压缩。

<configuration>
   <system.webServer>
      <urlCompression doStaticCompression="true" doDynamicCompression="false" />
   </system.webServer>
</configuration>

代码示例

以下代码示例为默认网站启用静态压缩并禁用动态压缩。

AppCmd.exe

appcmd.exe set config "Default Web Site" -section:system.webServer/urlCompression /doDynamicCompression:"False" 

appcmd.exe set config "Default Web Site" -section:system.webServer/urlCompression /doStaticCompression:"True"

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.GetWebConfiguration("Default Web Site");

         ConfigurationSection urlCompressionSection = config.GetSection("system.webServer/urlCompression");
         urlCompressionSection["doStaticCompression"] = true;
         urlCompressionSection["doDynamicCompression"] = false;

         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.GetWebConfiguration("Default Web Site")

      Dim urlCompressionSection As ConfigurationSection = config.GetSection("system.webServer/urlCompression")
      urlCompressionSection("doStaticCompression") = True
      urlCompressionSection("doDynamicCompression") = False

      serverManager.CommitChanges()
   End Sub

End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site";

var urlCompressionSection = adminManager.GetAdminSection("system.webServer/urlCompression", "MACHINE/WEBROOT/APPHOST/Default Web Site");
urlCompressionSection.Properties.Item("doStaticCompression").Value = true;
urlCompressionSection.Properties.Item("doDynamicCompression").Value = false;

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Default Web Site"

Set urlCompressionSection = adminManager.GetAdminSection("system.webServer/urlCompression", "MACHINE/WEBROOT/APPHOST/Default Web Site")
urlCompressionSection.Properties.Item("doStaticCompression").Value = True
urlCompressionSection.Properties.Item("doDynamicCompression").Value = False

adminManager.CommitChanges()