使用 Azure 应用程序网关重写 URL - Azure 门户(预览版)Rewrite URL with Azure Application Gateway - Azure portal (Preview)

本文介绍如何使用 Azure 门户来配置应用程序网关 v2 SKU 实例以重写 URL。This article describes how to use the Azure portal to configure an Application Gateway v2 SKU instance to rewrite URL.

备注

URL 重写功能为预览版,仅适用于 Standard_v2 和 WAF_v2 SKU 版应用程序网关。URL rewrite feature is in preview and is available only for Standard_v2 and WAF_v2 SKU of Application Gateway. 不建议在生产环境中使用。It is not recommended for use in production environment. 若要了解关于预览的详细信息,请参阅此处的使用条款To learn more about previews, see terms of use here.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial before you begin.

准备阶段Before you begin

若要完成本文中的步骤,需要有应用程序网关 v2 SKU 实例。You need to have an Application Gateway v2 SKU instance to complete the steps in this article. v1 SKU 不支持重写 URL。Rewriting URL isn't supported in the v1 SKU. 如果没有 v2 SKU,请在开始之前创建应用程序网关 v2 SKU 实例。If you don't have the v2 SKU, create an Application Gateway v2 SKU instance before you begin.

登录 AzureSign in to Azure

使用 Azure 帐户登录到 Azure 门户Sign in to the Azure portal with your Azure account.

配置 URL 重写Configure URL rewrite

在以下示例中,每当请求 URL 包含 /article 时,URL 路径和 URL 查询字符串就会重写In the below example whenever the request URL contains /article, the URL path and URL query string are rewritten

contoso.com/article/123/fabrikam -> contoso.com/article.aspx?id=123&title=fabrikam

  1. 选择“所有资源”,然后选择你的应用程序网关****。Select All resources, and then select your application gateway.

  2. 在左窗格中,选择“重写”。****Select Rewrites in the left pane.

  3. 选择“重写集”****:Select Rewrite set:

    添加重写集

  4. 为重写集提供一个名称,并将其与路由规则相关联:Provide a name for the rewrite set and associate it with a routing rule:

    a.a. 在“名称”框中输入重写集的名称。****Enter the name for the rewrite set in the Name box.

    b.b. 选择在“关联的路由规则”列表中列出的一个或多个规则。****Select one or more of the rules listed in the Associated routing rules list. 这用于通过传递规则将重写配置关联到源侦听器。This is used to associate the rewrite configuration to the source listener via the routing rule. 只能选择尚未与其他重写集关联的传递规则。You can select only those routing rules that haven't been associated with other rewrite sets. 已经与其他重写集关联的规则处于灰显状态。The rules that have already been associated with other rewrite sets are greyed out.

    c.c. 选择“下一步”。Select Next.

    关联到规则

  5. 创建重写规则:Create a rewrite rule:

    a.a. 选择“添加重写规则”。****Select Add rewrite rule.

    添加重写规则

    b.b. 在“重写规则名称”框中输入重写规则的名称。****Enter a name for the rewrite rule in the Rewrite rule name box. 在“规则顺序”框中输入一个数字。****Enter a number in the Rule sequence box.

  6. 在此示例中,仅当路径包含 /article 时,我们才会重写 URL 路径和 URL 查询字符串。In this example, we'll rewrite URL path and URL query string only when path contains /article. 为此,请添加条件用于评估 URL 路径是否包含 /articleTo do this, add a condition to evaluate whether the URL path contains /article

    a.a. 选择“添加条件”,**** 然后选择包含 If 指令的框将其展开。Select Add condition and then select the box containing the If instructions to expand it.

    b.b. 由于我们在此示例中要检查 URL 路径中的模式 /article,请在“要检查的变量的类型”列表中,选择“服务器变量”。Since in this example we want to check the pattern /article in the URL path, in the Type of variable to check list, select Server variable.

    c.c. 在“服务器变量”列表中,选择“uri_path”In the Server variable list, select uri_path

    d.d. 在“区分大小写”下选择“否”。********Under Case-sensitive, select No.

    e.e. 在“运算符”列表中选择“等于(=)”。********In the Operator list, select equal (=).

    f.f. 输入正则表达式模式。Enter a regular expression pattern. 在此示例中,我们将使用 .*article/(.*)/(.*) 模式In this example, we'll use the pattern .*article/(.*)/(.*)

    ( ) 用于捕获 substring,以供稍后在编写用于重写 URL 路径的表达式时使用。( ) is used to capture the substring for later use in composing the expression for rewriting the URL path. 有关详细信息,请参阅此文For more information, see here.

    g.g. 选择“确定”。Select OK.

    Condition Condition

  7. 添加操作以重写 URL 和 URL 路径Add an action to rewrite the URL and URL path

    a.a. 在“重写类型”列表中,选择“URL”。In the Rewrite type list, select URL.

    b.b. 在“操作类型”列表中,选择“设置”。********In the Action type list, select Set.

    c.c. 在“组件”下,选择“URL 路径和 URL 查询字符串”Under Components, select Both URL path and URL query string

    d.d. 在“URL 路径值”中,输入路径的新值。In the URL path value, enter the new value of the path. 在此示例中,我们将使用“/article.aspx”In this example, we will use /article.aspx

    e.e. 在“URL 查询字符串值”中,输入 URL 查询字符串的新值。In the URL query string value, enter the new value of the URL query string. 在此示例中,我们将使用“id={var_uri_path_1}&title={var_uri_path_2}”In this example, we will use id={var_uri_path_1}&title={var_uri_path_2}

    {var_uri_path_1}{var_uri_path_1} 用于在计算表达式 .*article/(.*)/(.*) 中的条件时获取捕获的子字符串{var_uri_path_1} and {var_uri_path_1} are used to fetch the substrings captured while evaluating the condition in this expression .*article/(.*)/(.*)

    f.f. 选择“确定”。Select OK.

    操作

  8. 单击“创建”以创建重写集。Click Create to create the rewrite set.

  9. 验证新的重写集是否在重写集列表中显示Verify that the new rewrite set appears in the list of rewrite sets

    添加重写规则

通过访问日志验证 URL 重写Verify URL rewrite through access logs

观察访问日志中的以下字段,以验证是否按照预期进行 URL 重写。Observe the below fields in access logs to verify if the URL rewrite happened as per your expectation.

  • originalRequestUriWithArgs:此字段包含原始请求 URLoriginalRequestUriWithArgs: This field contains the original request URL
  • requestUri:在应用程序网关上执行重写操作后,此字段将包含 URLrequestUri: This field contains the URL after the rewrite operation on Application Gateway

有关访问日志中的所有字段的详细信息,请参阅此处For more information on all the fields in the access logs, see here.

后续步骤Next steps

有关如何设置某些常见用例的重写的详细信息,请参阅常见重写方案To learn more about how to set up rewrites for some common use cases, see common rewrite scenarios.