使用 Azure CLI 基于 URL 对 Web 流量进行路由Route web traffic based on the URL using the Azure CLI

作为管理 Web 流量的 IT 管理员,你希望帮助客户或用户尽快获取其需要的信息。As an IT administrator managing web traffic, you want to help your customers or users get the information they need as quickly as possible. 若要优化其体验,一种方法是将不同类型的 Web 流量路由到不同的服务器资源。One way you can optimize their experience is by routing different kinds of web traffic to different server resources. 本文介绍如何使用 Azure CLI 为应用程序中的不同类型的流量设置和配置应用程序网关路由。This article shows you how to use the Azure CLI to set up and configure Application Gateway routing for different types of traffic from your application. 然后,路由会根据 URL 将流量定向到不同的服务器池。The routing then directs the traffic to different server pools based on the URL.

URL 路由示例

在本文中,学习如何:In this article, you learn how to:

  • 为所需的网络资源创建资源组Create a resource group for the network resources you’ll need
  • 创建网络资源Create the network resources
  • 为来自应用程序的流量创建应用程序网关Create an application gateway for the traffic coming from your application
  • 为不同类型的流量指定服务器池和路由规则Specify server pools and routing rules for the different types of traffic
  • 为每个池创建一个规模集,使池可以自动缩放Create a scale set for each pool so the pool can automatically scale
  • 运行测试,以便验证不同类型的流量是否进入正确的池Run a test so you can verify that the different types of traffic go to the correct pool

如果你愿意,可以使用 Azure PowerShellAzure 门户完成本过程中的步骤。If you prefer, you can complete this procedure using Azure PowerShell or the Azure portal.

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

根据本文的要求,如果选择在本地安装并使用 CLI,则需要运行 Azure CLI 2.0.4 或更高版本。If you choose to install and use the CLI locally, this article requires you to run the Azure CLI version 2.0.4 or later. 要查找版本,请运行 az --versionTo find the version, run az --version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container where Azure resources are deployed and managed. 使用 az group create 创建资源组。Create a resource group using az group create.

以下示例在“chinanorth2” 位置创建名为“myResourceGroupAG” 的资源组。The following example creates a resource group named myResourceGroupAG in the chinanorth2 location.

az group create --name myResourceGroupAG --location chinanorth2

创建网络资源Create network resources

使用 az network vnet create 创建名为 myVNet 的虚拟网络和名为 myAGSubnet 的子网。Create the virtual network named myVNet and the subnet named myAGSubnet using az network vnet create. 然后,使用 az network vnet subnet create 添加后端服务器所需的名为 myBackendSubnet 的子网。Then add a subnet named myBackendSubnet that's needed by the backend servers using az network vnet subnet create. 使用 az network public-ip create 创建名为 myAGPublicIPAddress 的公共 IP 地址。Create the public IP address named myAGPublicIPAddress using az network public-ip create.

az network vnet create \
  --name myVNet \
  --resource-group myResourceGroupAG \
  --location chinanorth2 \
  --address-prefix \
  --subnet-name myAGSubnet \

az network vnet subnet create \
  --name myBackendSubnet \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \

az network public-ip create \
  --resource-group myResourceGroupAG \
  --name myAGPublicIPAddress \
  --allocation-method Static \
  --sku Standard

使用 URL 映射创建应用网关Create the app gateway with a URL map

使用 az network application-gateway create 创建名为 myAppGateway 的应用程序网关。Use az network application-gateway create to create an application gateway named myAppGateway. 使用 Azure CLI 创建应用程序网关时,请指定配置信息,例如容量、sku 和 HTTP 设置。When you create an application gateway using the Azure CLI, you specify configuration information, such as capacity, sku, and HTTP settings. 将应用程序网关分配给 myAGSubnet 和 myAGPublicIPAddress 。The application gateway is assigned to myAGSubnet and myAGPublicIPAddress.

az network application-gateway create \
  --name myAppGateway \
  --location chinanorth2 \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \
  --subnet myAGsubnet \
  --capacity 2 \
  --sku Standard_v2 \
  --http-settings-cookie-based-affinity Disabled \
  --frontend-port 80 \
  --http-settings-port 80 \
  --http-settings-protocol Http \
  --public-ip-address myAGPublicIPAddress

创建应用程序网关可能需要几分钟时间。It may take several minutes to create the application gateway. 创建应用程序网关后,可以看到以下新功能:After the application gateway is created, you can see these new features:

FeatureFeature 说明Description
appGatewayBackendPoolappGatewayBackendPool 应用程序网关必须至少具有一个后端地址池。An application gateway must have at least one backend address pool.
appGatewayBackendHttpSettingsappGatewayBackendHttpSettings 指定将端口 80 和 HTTP 协议用于通信。Specifies that port 80 and an HTTP protocol is used for communication.
appGatewayHttpListenerappGatewayHttpListener 与 appGatewayBackendPool 关联的默认侦听器The default listener associated with appGatewayBackendPool
appGatewayFrontendIPappGatewayFrontendIP 将 myAGPublicIPAddress 分配给 appGatewayHttpListener。Assigns myAGPublicIPAddress to appGatewayHttpListener.
rule1rule1 与 appGatewayHttpListener 关联的默认路由规则。The default routing rule that is associated with appGatewayHttpListener.

添加映像及视频后端池和端口Add image and video backend pools and a port

使用 az network application-gateway address-pool create 向应用程序网关添加名为 imagesBackendPoolvideoBackendPool 的后端池。Add backend pools named imagesBackendPool and videoBackendPool to your application gateway by using az network application-gateway address-pool create. 使用 az network application-gateway frontend-port create 添加池的前端端口。You add the frontend port for the pools using az network application-gateway frontend-port create.

az network application-gateway address-pool create \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name imagesBackendPool

az network application-gateway address-pool create \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name videoBackendPool

az network application-gateway frontend-port create \
  --port 8080 \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name port8080

添加后端侦听器Add a backend listener

使用 az network application-gateway http-listener create 添加路由流量所需的名为 backendListener 的后端侦听器。Add the backend listener named backendListener that's needed to route traffic using az network application-gateway http-listener create.

az network application-gateway http-listener create \
  --name backendListener \
  --frontend-ip appGatewayFrontendIP \
  --frontend-port port8080 \
  --resource-group myResourceGroupAG \
  --gateway-name myAppGateway

添加 URL 路径映射Add a URL path map

URL 路径映射可确保将特定的 URL 路由到特定的后端池。URL path maps ensure that specific URLs are routed to specific backend pools. 使用 az network application-gateway url-path-map createaz network application-gateway url-path-map rule create 创建名为 imagePathRulevideoPathRule 的 URL 路径映射。Create URL path maps named imagePathRule and videoPathRule using az network application-gateway url-path-map create and az network application-gateway url-path-map rule create.

az network application-gateway url-path-map create \
  --gateway-name myAppGateway \
  --name myPathMap \
  --paths /images/* \
  --resource-group myResourceGroupAG \
  --address-pool imagesBackendPool \
  --default-address-pool appGatewayBackendPool \
  --default-http-settings appGatewayBackendHttpSettings \
  --http-settings appGatewayBackendHttpSettings \
  --rule-name imagePathRule

az network application-gateway url-path-map rule create \
  --gateway-name myAppGateway \
  --name videoPathRule \
  --resource-group myResourceGroupAG \
  --path-map-name myPathMap \
  --paths /video/* \
  --address-pool videoBackendPool

添加路由规则Add a routing rule

路由规则可将 URL 映射与所创建的侦听器相关联。The routing rule associates the URL maps with the listener that you created. 使用 az network application-gateway rule create 添加名为 rule2 的规则。Add a rule named rule2 using az network application-gateway rule create.

az network application-gateway rule create \
  --gateway-name myAppGateway \
  --name rule2 \
  --resource-group myResourceGroupAG \
  --http-listener backendListener \
  --rule-type PathBasedRouting \
  --url-path-map myPathMap \
  --address-pool appGatewayBackendPool

创建虚拟机规模集Create virtual machine scale sets

在本文中,你将创建三个虚拟机规模集以支持所创建的三个后端池。In this article, you create three virtual machine scale sets that support the three backend pools you created. 你将创建名为 myvmss1myvmss2myvmss3 的规模集。You create scale sets named myvmss1, myvmss2, and myvmss3. 每个规模集都包含两个虚拟机实例,将在其上安装 NGINX。Each scale set contains two virtual machine instances where you install NGINX.

for i in `seq 1 3`; do

  if [ $i -eq 1 ]

  if [ $i -eq 2 ]

  if [ $i -eq 3 ]

  az vmss create \
    --name myvmss$i \
    --resource-group myResourceGroupAG \
    --image UbuntuLTS \
    --admin-username azureuser \
    --admin-password Azure123456! \
    --instance-count 2 \
    --vnet-name myVNet \
    --subnet myBackendSubnet \
    --vm-sku Standard_DS2 \
    --upgrade-policy-mode Automatic \
    --app-gateway myAppGateway \
    --backend-pool-name $poolName


for i in `seq 1 3`; do
  az vmss extension set \
    --publisher Microsoft.Azure.Extensions \
    --version 2.0 \
    --name CustomScript \
    --resource-group myResourceGroupAG \
    --vmss-name myvmss$i \
    --settings '{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"], "commandToExecute": "./install_nginx.sh" }'

测试应用程序网关Test the application gateway

若要获取应用程序网关的公共 IP 地址,请使用 az network public-ip show。To get the public IP address of the application gateway, use az network public-ip show. 复制该公共 IP 地址,并将其粘贴到浏览器的地址栏。Copy the public IP address, and then paste it into the address bar of your browser. 例如, as,,, or

az network public-ip show \
  --resource-group myResourceGroupAG \
  --name myAGPublicIPAddress \
  --query [ipAddress] \
  --output tsv

在应用程序网关中测试基 URL

将 URL 更改为 http://<ip-address>:8080/images/test.html(请将 <ip-address> 替换为自己的 IP 地址),应会看到如以下示例所示的内容:Change the URL to http://<ip-address>:8080/images/test.html, replacing your IP address for <ip-address>, and you should see something like the following example:

在应用程序网关中测试映像 URL

将 URL 更改为 http://<ip-address>:8080/video/test.html(请将 <ip-address> 替换为自己的 IP 地址),应会看到如以下示例所示的内容。Change the URL to http://<ip-address>:8080/video/test.html, replacing your IP address for <ip-address>, and you should see something like the following example.

在应用程序网关中测试视频 URL

清理资源Clean up resources

当不再需要资源组、应用程序网关以及所有相关资源时,请将其删除。When they're no longer needed, remove the resource group, application gateway, and all related resources.

az group delete --name myResourceGroupAG

后续步骤Next steps

创建支持基于 URL 路径进行重定向的应用程序网关Create an application gateway with URL path-based redirection