您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:设置 Azure CDN 缓存规则Tutorial: Set Azure CDN caching rules

备注

Azure CDN 缓存规则仅适用于 Verizon 提供的标准 Azure CDNAkamai 提供的标准 Azure CDNAzure CDN caching rules are available only for Azure CDN Standard from Verizon and Azure CDN Standard from Akamai. 对于 Verizon 提供的高级 Azure CDN,可以在“管理”门户中使用 Azure CDN 规则引擎来获得类似的功能。For Azure CDN Premium from Verizon, use the Azure CDN rules engine in the Manage portal for similar functionality.

本教程介绍如何使用 Azure 内容分发网络 (CDN) 缓存规则,全局性地和根据自定义条件(例如 URL 路径和文件扩展名)设置或修改默认的缓存过期行为。This tutorial describes how you can use Azure Content Delivery Network (CDN) caching rules to set or modify default cache expiration behavior both globally and with custom conditions, such as a URL path and file extension. Azure CDN 提供两种类型的缓存规则:Azure CDN provides two types of caching rules:

  • 全局缓存规则:可以针对配置文件中的每个终结点设置一个全局缓存规则,该规则影响对该终结点发出的所有请求。Global caching rules: You can set one global caching rule for each endpoint in your profile, which affects all requests to the endpoint. 全局缓存规则会替代所有 HTTP 缓存指令标头(如果已设置)。The global caching rule overrides any HTTP cache-directive headers, if set.

  • 自定义缓存规则:可以针对配置文件中的每个终结点设置一个或多个自定义缓存规则。Custom caching rules: You can set one or more custom caching rules for each endpoint in your profile. 自定义缓存规则将匹配特定的路径和文件扩展名,按顺序进行处理,并替代全局缓存规则(如果已设置)。Custom caching rules match specific paths and file extensions, are processed in order, and override the global caching rule, if set.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 打开“缓存规则”页。Open the caching rules page.
  • 创建全局缓存规则。Create a global caching rule.
  • 创建自定义缓存规则。Create a custom caching rule.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

先决条件Prerequisites

在完成本教程中的步骤之前,必须先创建一个 CDN 配置文件,一个至少一个 CDN 终结点。Before you can complete the steps in this tutorial, you must first create a CDN profile and at least one CDN endpoint. 有关详细信息,请参阅快速入门:创建 Azure CDN 配置文件和终结点For more information, see Quickstart: Create an Azure CDN profile and endpoint.

打开 Azure CDN“缓存规则”页Open the Azure CDN caching rules page

  1. Azure 门户中,依次选择一个 CDN 配置文件和一个终结点。In the Azure portal, select a CDN profile, then select an endpoint.

  2. 在左窗格中的“设置”下,选择“缓存规则”。In the left pane under Settings, select Caching rules.

    CDN 缓存规则按钮

    “缓存规则”页随即出现。The Caching rules page appears.

    CDN“缓存规则”页

设置全局缓存规则Set global caching rules

按如下所述创建一个全局缓存规则:Create a global caching rule as follows:

  1. 在“全局缓存规则”下面,将“查询字符串缓存行为”设置为“忽略查询字符串”。Under Global caching rules, set Query string caching behavior to Ignore query strings.

  2. 将“缓存行为”设置为“缺少时设置”。Set Caching behavior to Set if missing.

  3. 对于“缓存过期持续时间”,请在“天”字段中输入 10。For Cache expiration duration, enter 10 in the Days field.

    全局缓存规则会影响对终结点发出的所有请求。The global caching rule affects all requests to the endpoint. 此规则遵循源缓存指令标头(如果存在,值为 Cache-ControlExpires);如果未指定此类标头,则将缓存设置为 10 天。This rule honors the origin cache-directive headers, if they exist (Cache-Control or Expires); otherwise, if they are not specified, it sets the cache to 10 days.

    全局缓存规则

设置自定义缓存规则Set custom caching rules

按如下所述创建一个自定义缓存规则:Create a custom caching rule as follows:

  1. 在“自定义缓存规则”下面,将“匹配条件”设置为“路径”,将“匹配值”设置为 /images/*.jpgUnder Custom caching rules, set Match condition to Path and Match value to /images/*.jpg.

  2. 将“缓存行为”设置为“替代”,并在“天”字段中输入 30。Set Caching behavior to Override and enter 30 in the Days field.

    此自定义缓存规则针对终结点的 /images 文件夹中的所有 .jpg 图像文件设置 30 天缓存持续时间。This custom caching rule sets a cache duration of 30 days on any .jpg image files in the /images folder of your endpoint. 它会替代源服务器发送的所有 Cache-ControlExpires HTTP 标头。It overrides any Cache-Control or Expires HTTP headers that are sent by the origin server.

    自定义缓存规则

清理资源Clean up resources

在前面的步骤中,已创建了缓存规则。In the preceding steps, you created caching rules. 如果不再想要使用这些缓存规则,则可以通过执行下列步骤来将其删除:If you no longer want to use these caching rules, you can remove them by performing these steps:

  1. 选择 CDN 配置文件,然后选择其缓存规则需要删除的终结点。Select a CDN profile, then select the endpoint with the caching rules you want to remove.

  2. 在左窗格中的“设置”下,选择“缓存规则”。In the left pane under Settings, select Caching rules.

  3. 在“全局缓存规则”下,将“缓存行为”设置为“未设置”。Under Global caching rules, set Caching behavior to Not set.

  4. 在“自定义缓存规则”下,选中要删除的规则旁边的复选框。Under Custom caching rules, select the check box next to the rule you want to delete.

  5. 选择“删除”。Select Delete.

  6. 从页面顶部,选择“保存”。From the top of the page, select Save.

后续步骤Next steps

本教程介绍了如何:In this tutorial, you learned how to:

  • 打开“缓存规则”页。Open the caching rules page.
  • 创建全局缓存规则。Create a global caching rule.
  • 创建自定义缓存规则。Create a custom caching rule.

转到下一篇文章,了解如何配置其他缓存规则设置。Advance to the next article to learn how to configure additional caching rule settings.