应用程序网关请求传递规则Application Gateway request routing rules

使用 Azure 门户创建应用程序网关时,可创建一个默认规则 (rule1)。When you create an application gateway using the Azure portal, you create a default rule (rule1). 此规则会将默认侦听器 (appGatewayHttpListener) 绑定到默认后端池 (appGatewayBackendPool) 和默认后端 HTTP 设置 (appGatewayBackendHttpSettings)。This rule binds the default listener (appGatewayHttpListener) with the default back-end pool (appGatewayBackendPool) and the default back-end HTTP settings (appGatewayBackendHttpSettings). 创建网关后,可以编辑该默认规则的设置,或创建新的规则。After you create the gateway, you can edit the settings of the default rule or create new rules.

规则类型Rule type

创建规则时,可以选择“基本”或“基于路径”When you create a rule, you choose between basic and path-based.

  • 若要将关联的侦听器(例如 blog .contoso.com/* )上的所有请求转发到单个后端池,请选择“基本”。Choose basic if you want to forward all requests on the associated listener (for example, blog.contoso.com/*) to a single back-end pool.
  • 若要将来自特定 URL 路径的请求路由到特定的后端池,请选择“基于路径”。Choose path-based if you want to route requests from specific URL paths to specific back-end pools. 路径模式仅应用到 URL 的路径,而不应用到该 URL 的查询参数。The path pattern is applied only to the path of the URL, not to its query parameters.

规则的处理顺序Order of processing rules

使用 v1 和 v2 SKU 时,将按照路径在基于路径的规则的 URL 路径映射中列出的顺序处理传入请求的模式匹配。For the v1 and v2 SKU, pattern matching of incoming requests is processed in the order that the paths are listed in the URL path map of the path-based rule. 如果某个请求与 URL 路径映射中的两个或更多个路径的模式相匹配,则会匹配最先列出的路径。If a request matches the pattern in two or more paths in the path map, the path that's listed first is matched. 请求将转发到与该路径关联的后端。And the request is forwarded to the back end that's associated with that path.

关联的侦听器Associated listener

将一个侦听器关联到该规则,以评估与该侦听器关联的请求路由规则,从而确定请求要路由到的后端池。Associate a listener to the rule so that the request-routing rule that's associated with the listener is evaluated to determine the back-end pool to route the request to.

关联的后端池Associated back-end pool

将规则关联到包含后端目标的后端池,该池为侦听器收到的请求提供服务。Associate to the rule the back-end pool that contains the back-end targets that serve requests that the listener receives.

  • 如果使用基本规则,则只允许一个后端池。For a basic rule, only one back-end pool is allowed. 关联的侦听器上的所有请求将转发到该后端池。All requests on the associated listener are forwarded to that back-end pool.

  • 如果使用基于路径的规则,请添加对应于每个 URL 路径的多个后端池。For a path-based rule, add multiple back-end pools that correspond to each URL path. 与输入的 URL 路径匹配的请求将转发到相应的后端池。The requests that match the URL path that's entered are forwarded to the corresponding back-end pool. 另请添加默认后端池。Also, add a default back-end pool. 与规则中的任何 URL 路径都不匹配的请求将转发到该池。Requests that don't match any URL path in the rule are forwarded to that pool.

关联的后端 HTTP 设置Associated back-end HTTP setting

为每个规则添加后端 HTTP 设置。Add a back-end HTTP setting for each rule. 系统使用此设置中指定的端口号、协议和其他信息,将请求从应用程序网关路由到后端目标。Requests are routed from the application gateway to the back-end targets by using the port number, protocol, and other information that's specified in this setting.

如果使用基本规则,则只允许一个后端 HTTP 设置。For a basic rule, only one back-end HTTP setting is allowed. 系统会使用此 HTTP 设置将关联的侦听器上的所有请求转发到相应的后端目标。All requests on the associated listener are forwarded to the corresponding back-end targets by using this HTTP setting.

如果使用基于路径的规则,请添加对应于每个 URL 路径的多个后端 HTTP 设置。For a path-based rule, add multiple back-end HTTP settings that correspond to each URL path. 系统使用对应于每个 URL 路径的 HTTP 设置,将与此设置中的 URL 路径匹配的请求转发到相应的后端目标。Requests that match the URL path in this setting are forwarded to the corresponding back-end targets by using the HTTP settings that correspond to each URL path. 另请添加默认 HTTP 设置。Also, add a default HTTP setting. 系统会使用默认 HTTP 设置,将与此规则中的任何 URL 路径都不匹配的请求转发到默认后端池。Requests that don't match any URL path in this rule are forwarded to the default back-end pool by using the default HTTP setting.

重定向设置Redirection setting

如果为基本规则配置了重定向,则关联的侦听器上的所有请求将重定向到目标。If redirection is configured for a basic rule, all requests on the associated listener are redirected to the target. 此过程称为全局重定向。This is global redirection. 如果为基于路径的规则配置了重定向,则只会重定向特定站点区域中的请求。If redirection is configured for a path-based rule, only requests in a specific site area are redirected. 区域的示例包括 /cart/* 表示的购物车区域。An example is a shopping cart area that's denoted by /cart/*. 此过程称为基于路径的重定向。This is path-based redirection.

有关重定向的详细信息,请参阅应用程序网关重定向概述For more information about redirects, see Application Gateway redirect overview.

重定向类型Redirection type

选择所需的重定向类型:Permanent(301)Temporary(307)Found(302)See other(303)Choose the type of redirection required: Permanent(301), Temporary(307), Found(302), or See other(303).

重定向目标Redirection target

选择另一个侦听器或外部站点作为重定向目标。Choose another listener or an external site as the redirection target.

侦听器Listener

选择侦听器作为重定向目标可将来自网关上的一个侦听器的流量重定向到另一个侦听器。Choose listener as the redirection target to redirect traffic from one listener to another on the gateway. 想要启用 HTTP 到 HTTPS 的重定向时,必须指定此设置。This setting is required when you want to enable HTTP-to-HTTPS redirection. 此设置将来自源侦听器(用于检查 HTTP 请求)的流量重定向到目标侦听器(用于检查传入的 HTTPS 请求)。It redirects traffic from the source listener that checks for incoming HTTP requests to the destination listener that checks for incoming HTTPS requests. 还可以选择在转发到重定向目标的请求中包含来自原始请求的查询字符串和路径。You can also choose to include the query string and path from the original request in the request that's forwarded to the redirection target.

应用程序网关组件对话框

有关 HTTP 到 HTTPS 的重定向的详细信息,请参阅:For more information about HTTP-to-HTTPS redirection, see:

外部站点External site

若要将与此类规则关联的侦听器上的流量重定向到外部站点,请选择外部站点。Choose external site when you want to redirect the traffic on the listener that's associated with this rule to an external site. 可以选择在转发到重定向目标的请求中包含来自原始请求的查询字符串。You can choose to include the query string from the original request in the request that's forwarded to the redirection target. 无法将原始请求中的路径转发到外部站点。You can't forward the path to the external site that was in the original request.

有关重定向的详细信息,请参阅:For more information about redirection, see:

重写 HTTP 标头和 URLRewrite HTTP headers and URL

通过使用重写规则,当请求和响应数据包通过应用程序网关在客户端和后端池之间移动时,你可以添加、删除或更新 HTTP(S) 请求和响应标头以及 URL 路径和查询字符串参数。By using rewrite rules, you can add, remove, or update HTTP(S) request and response headers as well as URL path and query string parameters as the request and response packets move between the client and backend pools via the application gateway.

这些标头和 URL 参数可以设置为静态值,也可以设置为其他标头和服务器变量。The headers and URL parameters can be set to static values or to other headers and server variables. 这有助于处理重要的用例,例如提取客户端 IP 地址、删除有关后端的敏感信息、添加更多安全性等。This helps with important use cases, such as extracting client IP addresses, removing sensitive information about the backend, adding more security, and so on. 有关详细信息,请参阅:For more information, see:

后续步骤Next steps