共用方式為

通过在 Azure Front Door 中压缩文件来提高性能

Applies to: ✔️ Front Door Standard ✔️ Front Door Premium

文件压缩是提高文件传输速度和增加页面加载性能的有效方法。 服务器会在发送文件之前对其进行压缩,来减少其大小。 文件压缩可以减少带宽成本,并为用户提供更好的体验。

有两种方法可启用文件压缩:

  • 在源服务器上启用压缩。 Azure Front Door 传递压缩文件,将其交付给发出请求的客户端。
  • 直接在 Azure Front Door POP 服务器上启用压缩(“快速压缩”)。 在这种情况下,Azure Front Door 会压缩文件并将其发送到最终用户。

Note

范围请求可以压缩成不同的大小。 Azure Front Door 要求任何 GET HTTP 请求的 Content-Length 响应头值都相同。 如果客户端发送带有 Accept-Encoding 标头的字节范围请求,导致源以不同的内容长度响应,则 Azure Front Door 会返回 503 错误。 可以在源/Azure Front Door 上禁用压缩,也可以创建规则引擎规则以从字节范围请求中移除 Accept-Encoding 标头。

Important

Azure Front Door 配置更改最多需要 10 分钟才能在整个网络中传播。 如果首次为 CDN 终结点设置压缩,请考虑等待 1-2 个小时,确保将压缩设置传播到所有 POP 之后再排查问题。

Enabling compression

Note

In Azure Front Door, compression is part of Enable Caching in Route. Only when you Enable Caching, can you take advantage of compression in Azure Front Door.

可通过以下方式启用压缩:

  • 快速创建期间 - 启用缓存时,可以启用压缩。
  • 自定义创建期间 - 添加路由时启用缓存和压缩。
  • 在 Front Door 管理器中。
  • 在“优化”页面上。

在 Front Door 管理器中启用压缩

  1. 在“Azure Front Door 标准版/高级版”配置文件页面上,转到 Front Door 管理器,然后选择要启用压缩的终结点。

  2. Within the endpoint, select the route you want to enable compression on.

  3. Ensure Enable caching is checked, then select the checkbox for Enable compression.

  4. Select Update to save the configuration.

在“优化”中启用压缩

  1. From the Azure Front Door Standard/Premium profile page, go to Optimizations under Settings. 展开端点,查看路由列表。

  2. Select the three dots next to the route that has compression Disabled. Then select Configure route.

    “优化”页的屏幕截图。

  3. Ensure Enable caching is checked, then select the checkbox for Enable compression.

  4. Select Update.

修改压缩内容类型

可以在“优化”页面上修改 MIME 类型的默认列表。

  1. From the Azure Front Door Standard/Premium profile page, go to Optimizations under Settings. Then select the route that has compression Enabled.

  2. Select the three dots next to the route that has compression Enabled. 然后选择“查看压缩的文件类型”。

  3. Delete default formats or select Add to add new content types.

    “自定义文件压缩”页面的屏幕截图。

  4. Select Save to update the compression configuration.

Disabling compression

可通过以下方式禁用压缩:

  • 在 Azure Front Door 管理器路由中禁用压缩。
  • 在“优化”页中禁用压缩。

在 Azure Front Door 管理器中禁用压缩

  1. 在“Azure Front Door 标准版/高级版”配置文件页面上,转到“设置”下的 Front Door 管理器。

  2. Select the route you want to disable compression on. Uncheck the Enable compression box.

  3. Select Update to save the configuration.

在“优化”中禁用压缩

  1. From the Azure Front Door Standard/Premium profile page, go to Optimizations under Settings. Then select the route that has compression Enabled.

  2. Select the three dots next to the route that has compression Enabled, then select Configure route.

  3. Uncheck the Enable compression box.

    用于禁用压缩的“更新路由”页面的屏幕截图。

  4. Select Update to save the configuration.

Compression rules

在 Azure Front Door 中,仅压缩符合条件的文件。 要符合压缩条件,文件必须:

  • 属于 MIME 类型
  • 大于 1 KB
  • 小于 8 MB

这些配置文件支持以下压缩编码:

  • gzip (GNU zip)
  • brotli

如果请求支持多个压缩类型,那么 brotli 压缩优先。

如果对资产的请求指定了 gzip 压缩并且请求导致缓存失误,则 Azure Front Door 将直接在 POP 服务器上对资产执行 gzip 压缩。 此后,将从缓存提供压缩的文件。

如果源使用分块传输编码 (CTE) 将数据发送到 Azure Front Door POP,则不支持压缩。

Next steps