重写 Azure 应用程序网关中的 HTTP 请求和响应标头 - Azure 门户Rewrite HTTP request and response headers with Azure Application Gateway - Azure portal

本文介绍如何使用 Azure 门户配置应用程序网关 v2 SKU 实例,重写请求和响应中的 HTTP 标头。This article describes how to use the Azure portal to configure an Application Gateway v2 SKU instance to rewrite the HTTP headers in requests and responses.

如果没有 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 不支持重写标头。Rewriting headers 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.

创建所需对象Create required objects

若要配置 HTTP 标头重写,需要完成以下步骤。To configure HTTP header rewrite, you need to complete these steps.

  1. 创建 HTTP 标头重写所需的对象:Create the objects that are required for HTTP header rewrite:

    • 重写操作:用于指定要重写的请求和请求标头字段,以及标头的新值。Rewrite action: Used to specify the request and request header fields that you intend to rewrite and the new value for the headers. 可将一个或多个重写条件关联到一个重写操作。You can associate one or more rewrite conditions with a rewrite action.

    • 重写条件:一个可选配置。Rewrite condition: An optional configuration. 重写条件评估 HTTP(S) 请求和响应的内容。Rewrite conditions evaluate the content of HTTP(S) requests and responses. 如果 HTTP(S) 请求或响应与重写条件匹配,则会发生重写操作。The rewrite action will occur if the HTTP(S) request or response matches the rewrite condition.

      如果将多个条件关联到一个操作,仅当满足所有条件时,才会发生该操作。If you associate more than one condition with an action, the action occurs only when all the conditions are met. 换言之,操作属于逻辑 AND 运算。In other words, the operation is a logical AND operation.

    • 重写规则:包含多个重写操作/重写条件的组合。Rewrite rule: Contains multiple rewrite action / rewrite condition combinations.

    • 规则顺序:帮助确定重写规则的执行顺序。Rule sequence: Helps determine the order in which the rewrite rules execute. 在一个重写集中使用多个重写规则时,此配置非常有用。This configuration is helpful when you have multiple rewrite rules in a rewrite set. 规则顺序值较小的重写规则最先运行。A rewrite rule that has a lower rule sequence value runs first. 如果为两个重写规则分配了相同的规则顺序值,则执行顺序是不确定的。If you assign the same rule sequence value to two rewrite rules, the order of execution is non-deterministic.

    • 重写集:包含要与请求路由规则关联的多个重写规则。Rewrite set: Contains multiple rewrite rules that will be associated with a request routing rule.

  2. 将重写集附加到路由规则。Attach the rewrite set to a routing rule. 重写配置将通过路由规则附加到源侦听器。The rewrite configuration is attached to the source listener via the routing rule. 使用基本路由规则时,标头重写配置与源侦听器相关联,并且是全局标头重写。When you use a basic routing rule, the header rewrite configuration is associated with a source listener and is a global header rewrite. 使用基于路径的路由规则时,将在 URL 路径映射中定义标头重写配置。When you use a path-based routing rule, the header rewrite configuration is defined on the URL path map. 在这种情况下,该规则只会应用到站点的特定路径区域。In that case, it applies only to the specific path area of a site.

可以创建多个 HTTP 标头重写集,并将每个重写集应用到多个侦听器。You can create multiple HTTP header rewrite sets and apply each rewrite set to multiple listeners. 但是,对于一个特定的侦听器,只能应用一个重写集。But you can apply only one rewrite set to a specific listener.

登录 AzureSign in to Azure

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

配置标头重写Configure header rewrite

在此示例中,我们将修改重定向 URL,方法是:重写由后端应用程序发送的 HTTP 响应中的 location 标头。In this example, we'll modify a redirection URL by rewriting the location header in the HTTP response sent by a back-end application.

  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:

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

    • 选择在“关联的路由规则”列表中列出的一个或多个规则。 Select one or more of the rules listed in the Associated routing rules list. 只能选择尚未与其他重写集关联的规则。You can select only rules that haven't been associated with other rewrite sets. 已经与其他重写集关联的规则处于灰显状态。The rules that have already been associated with other rewrite sets are dimmed.

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

      添加名称和关联

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

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

      添加重写规则

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

      添加重写规则名称

  6. 在此示例中,只有在 location 标头包含对 chinacloudsites.cn 的引用时,我们才重写它。In this example, we'll rewrite the location header only when it contains a reference to chinacloudsites.cn. 为此,请添加一个条件,用于评估响应中的 location 标头是否包含 chinacloudsites.cn:To do this, add a condition to evaluate whether the location header in the response contains chinacloudsites.cn:

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

      添加条件

    • 在“要检查的变量的类型”列表中,选择“HTTP 标头”。 In the Type of variable to check list, select HTTP header.

    • 在“标头类型”列表中选择“响应”。 In the Header type list, select Response.

    • 由于在此示例中我们要评估 location 标头,该标头是一个常用标头,因此请在“标头名称”下选择“常用标头”。 Because in this example we're evaluating the location header, which is a common header, select Common header under Header name.

    • 在“常用标头”列表中选择“Location”。 In the Common header list, select Location.

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

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

    • 输入正则表达式模式。Enter a regular expression pattern. 在此示例中,我们将使用 (https?):\/\/.*chinacloudsites\.cn(.*)$ 模式。In this example, we'll use the pattern (https?):\/\/.*chinacloudsites\.cn(.*)$.

    • 选择“确定” 。Select OK.

      配置 If 条件

  7. 添加操作来重写 location 标头:Add an action to rewrite the location header:

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

    • 在“标头类型”列表中选择“响应”。 In the Header type list, select Response.

    • 在“标头名称”下选择“常用标头”。 Under Header name, select Common header.

    • 在“常用标头”列表中选择“Location”。 In the Common header list, select Location.

    • 输入标头值。Enter the header value. 在此示例中,我们将使用 {http_resp_Location_1}://contoso.com{http_resp_Location_2} 作为标头值。In this example, we'll use {http_resp_Location_1}://contoso.com{http_resp_Location_2} as the header value. 该值会在 location 标头中将 chinacloudsites.cn 替换为 contoso.comThis value will replace chinacloudsites.cn with contoso.com in the location header.

    • 选择“确定” 。Select OK.

      添加操作

  8. 选择“创建”以创建重写集: Select Create to create the rewrite set:

    选择“创建”

  9. 此时会打开“重写集”视图。The Rewrite set view will open. 验证已创建的重写集是否位于重写集列表中:Verify that the rewrite set you created is in the list of rewrite sets:

    “重写集”视图

后续步骤Next steps

若要详细了解如何设置某些常见用例,请参阅常见标头重写方案To learn more about how to set up some common use cases, see common header rewrite scenarios.