你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:使用 Azure CLI 添加和自定义 Azure Front Door 标准版/高级版(预览版)的传递规则

Azure Front Door 标准版/高级版(预览版)是一种快速且安全的新式云 CDN。 Azure Front Door 使用 Microsoft 全局边缘网络并与智能威胁防护集成。 Azure Front Door 标准版侧重于内容交付。 Azure Front Door 高级版添加了各种安全功能和自定义。 本教程重点介绍如何创建 Azure Front Door 配置文件,然后添加传递规则以更精细地控制 Web 应用行为。

注意

本文档适用于 Azure Front Door 标准版/高级版(预览版)。 正在寻找有关 Azure Front Door 的信息? 请查看 Azure Front Door 文档

本教程介绍以下操作:

  • 创建 Azure Front Door 配置文件。
  • 创建 Web 应用的两个实例。
  • 创建新的安全策略。
  • 验证与 Web 应用的连接。
  • 创建规则集。
  • 创建规则并将其添加到规则集。
  • 将操作或条件添加到规则中。

如果没有 Azure 订阅,请在开始之前创建一个 Azure 免费帐户

先决条件

创建 Azure Front Door

创建资源组

对于本快速入门,你将需要两个资源组。 一个在“美国中部”,另一个在“美国东部”。

若要创建资源组,请运行 az group create

az group create \
    --name myRGFDCentral \
    --location centralus

az group create \
    --name myRGFDEast \
    --location eastus

创建 Azure Front Door 配置文件

若要创建 Azure Front Door 配置文件,请运行 az afd profile create

az afd profile create \
    --profile-name contosoafd \
    --resource-group myRGFDCentral \
    --sku Premium_AzureFrontDoor \
    --subscription mysubscription

创建 Web 应用的两个实例

对于本教程,你需要两个在不同 Azure 区域中运行的 Web 应用程序实例。 这两个 Web 应用程序实例都以“主动/主动”模式运行,因此其中的任何一个实例都可以为流量提供服务。

如果还没有 Web 应用,请使用以下脚本设置两个示例 Web 应用。

创建应用服务计划

在创建 Web 应用之前,你将需要两个应用服务计划,一个在“美国中部”,另一个在“美国东部”。

若要创建应用服务计划,请运行 az appservice plan create

az appservice plan create \
    --name myAppServicePlanCentralUS \
    --resource-group myRGFDCentral

az appservice plan create \
    --name myAppServicePlanEastUS \
    --resource-group myRGFDEast

创建 Web 应用

若要在上一步的每个应用服务计划中创建一个 Web 应用,请运行 az webapp create。 Web 应用名称必须全局独一无二。

若要查看 Web 应用的内置堆栈列表,请运行 az webapp list-runtimes

az webapp create \
    --name WebAppContoso-001 \
    --resource-group myRGFDCentral \
    --plan myAppServicePlanCentralUS \
    --runtime "DOTNETCORE|2.1"

az webapp create \
    --name WebAppContoso-002 \
    --resource-group myRGFDEast \
    --plan myAppServicePlanEastUS \
    --runtime "DOTNETCORE|2.1"

记下每个 Web 应用的默认主机名,以便在下一步部署 Front Door 时可以定义后端地址。

添加终结点

若要在配置文件中创建终结点,请运行 az afd endpoint create。 完成创建体验后,可以在配置文件中创建多个终结点。

az afd endpoint create \
    --resource-group myRGFDCentral \
    --endpoint-name contoso-frontend \
    --profile-name contosoafd \
    --origin-response-timeout-seconds 60 \
    --enabled-state Enabled

创建源组

若要创建包含两个 Web 应用的源组,请运行 az afd origin-group create

az afd origin-group create \
    --resource-group myRGFDCentral \
    --origin-group-name og1 \
    --profile-name contosoafd \
    --probe-request-type GET \
    --probe-protocol Http \
    --probe-interval-in-seconds 120 \
    --probe-path /test1/azure.txt \
    --sample-size 4 \
    --successful-samples-required 3 \
    --additional-latency-in-milliseconds 50

将源添加到组

若要将源添加到源组,请运行 az afd origin create

az afd origin create \
    --resource-group myRGFDCentral \
    --host-name webappcontoso-1.azurewebsites.net
    --profile-name contosoafd \
    --origin-group-name og1 \
    --origin-name contoso1 \
    --origin-host-header webappcontoso-1.azurewebsites.net \
    --priority 1 \
    --weight 1000 \
    --enabled-state Enabled \
    --http-port 80 \
    --https-port 443

重复此步骤并添加第二个源。

az afd origin create \
    --resource-group myRGFDCentral \
    --host-name webappcontoso-2.azurewebsites.net
    --profile-name contosoafd \
    --origin-group-name og1 \
    --origin-name contoso2 \
    --origin-host-header webappcontoso-2.azurewebsites.net \
    --priority 1 \
    --weight 1000 \
    --enabled-state Enabled \
    --http-port 80 \
    --https-port 443

添加路由

若要将前端终结点映射到源组,请运行 az afd route create。 此路由会将来自终结点的请求转发到 og1。

az afd route create \
    --resource-group myRGFDCentral \
    --endpoint-name contoso-frontend \
    --profile-name contosoafd \
    --route-name route1 \
    --https-redirect Enabled \
    --origin-group og1 \
    --supported-protocols Https \
    --link-to-default-domain Enabled \
    --forwarding-protocol MatchRequest

创建新的安全策略

创建 WAF 策略

若要为资源组之一创建 WAF 策略,请运行 az network front-door waf-policy create

为 Front Door 创建新的 WAF 策略。 以下示例创建一个已启用且处于阻止模式的策略。

az network front-door waf-policy create
    --name contosoWAF /
    --resource-group myRGFDCentral /
    --sku Premium_AzureFrontDoor
    --disabled false /
    --mode Prevention

注意

如果选择 Detection 模式,则 WAF 不会阻止任何请求。

创建安全策略

若要将 WAF 策略应用到终结点的默认域,请运行 az afd security-policy create

az afd security-policy create \
    --resource-group myRGFDCentral \
    --profile-name contosoafd \
    --security-policy-name contososecurity \
    --domains /subscriptions/mysubscription/resourcegroups/myRGFDCentral/providers/Microsoft.Cdn/profiles/contosoafd/afdEndpoints/contoso-frontend.z01.azurefd.net \
    --waf-policy /subscriptions/mysubscription/resourcegroups/myRGFDCentral/providers/Microsoft.Network/frontdoorwebapplicationfirewallpolicies/contosoWAF

验证 Azure Front Door

创建 Azure Front Door 标准版/高级版配置文件时,需要花费几分钟时间才能全局部署完配置。 完成后,即可访问你创建的前端主机。 在浏览器中转到 contoso-frontend.z01.azurefd.net。 请求将从源组中指定的服务器自动路由到最靠近的服务器。

我们将使用以下步骤来测试即时全局故障转移:

  1. 如上文所述打开浏览器并转到前端地址:contoso-frontend.azurefd.net

  2. 在 Azure 门户中,搜索并选择“应用服务”。 向下滚动找到你的 Web 应用之一,在本示例中为 WebAppContoso-1。

  3. 选择该 Web 应用,选择“停止”,然后选择“是”以确认操作 。

  4. 刷新浏览器。 应会看到相同的信息页。

    提示

    这些操作会略有延迟。 可能需要再次刷新。

  5. 找到另一个 Web 应用,同样将其停止。

  6. 刷新浏览器。 此时,应会看到一条错误消息。

    Both instances of the web app stopped

创建规则集

创建规则集以自定义在边缘处理 HTTP 请求的方式。 添加到规则集中的传递规则可以更好地控制 Web 应用程序行为。 若要在 Azure Front Door 配置文件中创建规则集,请运行 az afd rule-set create

az afd rule-set create \
    --profile-name contosoafd \
    --resource-group myRGFDCentral \
    --rule-set-name contosorules

创建传递规则并将其添加到规则集

在规则集中创建新的传递规则。 若要在规则集中创建传递规则,请运行 az afd rule create。 对于此示例,我们将为 http 到 https 重定向创建规则。

az afd rule create \
    --resource-group myRGFDCentral \
    --rule-set-name contosorules \
    --profile-name contosoafd \
    --order 1 \
    --match-variable RequestScheme \
    --operator Equal \
    --match-values HTTP \
    --rule-name "redirect" \
    --action-name "UrlRedirect" \
    --redirect-protocol Https \
    --redirect-type Moved

将操作或条件添加到传递规则

你可能会发现需要进一步自定义新的传递规则。 可以在创建后按需添加操作或条件。 若要更新规则,请运行 az afd rule action addaz afd rule condition add

添加操作

az afd rule action add \
    --resource-group myRGFDCentral \
    --rule-set-name contosorules \
    --profile-name contosoafd \
    --rule-name redirect \
    --action-name "CacheExpiration" \
    --cache-behavior BypassCache

添加条件

az afd rule condition add \
    --resource-group myRGFDCentral \
    --rule-set-name contosorules \
    --profile-name contosoafd \
    --rule-name redirect \
    --match-variable RemoteAddress \
    --operator GeoMatch \
    --match-values "TH"

清理资源

如果不需要 Front Door 的资源,请删除这两个资源组。 删除资源组也会删除 Front Door 及其所有相关资源。

运行 az group delete

az group delete \
    --name myRGFDCentral

az group delete \
    --name myRGFDEast