基于 URL 路径的路由概述URL Path Based Routing overview

基于 URL 路径的路由可让你根据请求的 URL 路径,将流量路由到后端服务器池。URL Path Based Routing allows you to route traffic to back-end server pools based on URL Paths of the request.

方案之一是将针对不同内容类型的请求路由到不同的后端服务器池。One of the scenarios is to route requests for different content types to different backend server pools.

在以下示例中,应用程序网关针对 contoso.com 从三个后端服务器池提供流量,例如:VideoServerPool、ImageServerPool 和 DefaultServerPool。In the following example, Application Gateway is serving traffic for contoso.com from three back-end server pools for example: VideoServerPool, ImageServerPool, and DefaultServerPool.

imageURLroute

对 http://contoso.com/video/* 的请求会路由到 VideoServerPool,对 http://contoso.com/images/* 的请求会路由到 ImageServerPool。Requests for http://contoso.com/video/* are routed to VideoServerPool, and http://contoso.com/images/* are routed to ImageServerPool. 如果没有任何路径模式匹配,则选择 DefaultServerPool。DefaultServerPool is selected if none of the path patterns match.

Important

规则将按照门户中的列出顺序进行处理。Rules are processed in the order they are listed in the portal. 我们强烈建议先配置多站点侦听器,然后再配置基本侦听器。It is highly recommended to configure multi-site listeners first prior to configuring a basic listener. 这确保将流量路由到适当的后端。This ensures that traffic gets routed to the right back end. 如果基本侦听器先列出并且与传入的请求匹配,则该侦听器将处理该请求。If a basic listener is listed first and matches an incoming request, it gets processed by that listener.

UrlPathMap 配置元素UrlPathMap configuration element

urlPathMap 元素用于指定后端服务器池映射的路径模式。The urlPathMap element is used to specify Path patterns to back-end server pool mappings. 以下代码示例是模板文件中 urlPathMap 元素的代码片段。The following code example is the snippet of urlPathMap element from template file.

"urlPathMaps": [{
    "name": "{urlpathMapName}",
    "id": "/subscriptions/{subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/urlPathMaps/{urlpathMapName}",
    "properties": {
        "defaultBackendAddressPool": {
            "id": "/subscriptions/    {subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/backendAddressPools/{poolName1}"
        },
        "defaultBackendHttpSettings": {
            "id": "/subscriptions/{subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/backendHttpSettingsList/{settingname1}"
        },
        "pathRules": [{
            "name": "{pathRuleName}",
            "properties": {
                "paths": [
                    "{pathPattern}"
                ],
                "backendAddressPool": {
                    "id": "/subscriptions/{subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/backendAddressPools/{poolName2}"
                },
                "backendHttpsettings": {
                    "id": "/subscriptions/{subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/backendHttpsettingsList/{settingName2}"
                }
            }
        }]
    }
}]

PathPatternPathPattern

PathPattern 是要匹配的路径模式列表。PathPattern is a list of path patterns to match. 每个模式必须以 / 开头,只允许在后接“/”的末尾处添加“*”。Each must start with / and the only place a "*" is allowed is at the end following a "/." 提供给路径匹配器的字符串在第一个“?”The string fed to the path matcher does not include any text after the first ? 或“#”之后不包含任何文本,而且这些字符不允许在这里。or #, and those chars are not allowed here. 否则,URL 中允许的任何字符在 PathPattern 中都是允许的。Otherwise, any characters allowed in a URL are allowed in PathPattern.

受支持的模式取决于是部署应用程序网关 v1 还是 v2:The supported patterns depend on whether you deploy Application Gateway v1 or v2:

v1v1

路径规则不区分大小写。Path rules are case insensitive.

v1 路径模式v1 path pattern 是否支持?Is supported?
/images/* yes
/images* no
/images/*.jpg no
/*.jpg no
/Repos/*/Comments/* no
/CurrentUser/Comments/* yes

v2v2

路径规则不区分大小写。Path rules are case insensitive.

v2 路径模式v2 path pattern 是否支持?Is supported?
/images/* yes
/images* yes
/images/*.jpg no
/*.jpg no
/Repos/*/Comments/* no
/CurrentUser/Comments/* yes

有关详细信息,可以查看使用基于 URL 的路由的 Resource Manager 模板You can check out a Resource Manager template using URL-based routing for more information.

PathBasedRouting 规则PathBasedRouting rule

PathBasedRouting 类型的 RequestRoutingRule 可用于将侦听器绑定到 urlPathMap。RequestRoutingRule of type PathBasedRouting is used to bind a listener to a urlPathMap. 此侦听器收到的所有请求将根据 urlPathMap 中指定的策略进行路由。All requests that are received for this listener are routed based on policy specified in urlPathMap. PathBasedRouting 规则的代码片段:Snippet of PathBasedRouting rule:

"requestRoutingRules": [
    {

"name": "{ruleName}",
"id": "/subscriptions/{subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/requestRoutingRules/{ruleName}",
"properties": {
    "ruleType": "PathBasedRouting",
    "httpListener": {
        "id": "/subscriptions/{subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/httpListeners/<listenerName>"
    },
    "urlPathMap": {
        "id": "/subscriptions/{subscriptionId}/../microsoft.network/applicationGateways/{gatewayName}/ urlPathMaps/{urlpathMapName}"
    },

}
    }
]

后续步骤Next steps

了解基于 URL 的内容路由之后,请转到使用基于 URL 的路由创建应用程序网关,使用 URL 路由规则创建应用程序网关。After learning about URL-based content routing, go to create an application gateway using URL-based routing to create an application gateway with URL routing rules.