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

可以使用 Azure PowerShell 基于用来访问应用程序的 URL 配置到特定的可缩放服务器池的 Web 流量路由。You can use Azure PowerShell to configure web traffic routing to specific scalable server pools based on the URL that is used to access your application. 在本文中,你将使用虚拟机规模集创建包含三个后端池的 Azure 应用程序网关In this article, you create an Azure Application Gateway with three backend pools using Virtual Machine Scale Sets. 每个后端池都有特定的用途,例如用于公用数据、图像和视频。Each of the backend pools serves a specific purpose such as, common data, images, and video. 将流量路由到不同的池可以确保客户在需要信息时可以获得所需的信息。Routing traffic to separate pools ensures that your customers get the information that they need when they need it.

若要启用流量路由,需要创建路由规则并将其分配给在特定端口上侦听的侦听器以确保 Web 流量到达池中的合适服务器。To enable traffic routing, you create routing rules assigned to listeners that listen on specific ports to ensure web traffic arrives at the appropriate servers in the pools.

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

  • 设置网络Set up the network
  • 创建侦听器、URL 路径映射和规则Create listeners, URL path map, and rules
  • 创建可缩放的后端池Create scalable backend pools

URL 路由示例

如果需要,可以使用 Azure CLIAzure 门户完成本过程中的步骤。If you prefer, you can complete this procedure using Azure CLI or the Azure portal.

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

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

如果选择在本地安装和使用 PowerShell,则本文需要 Azure PowerShell 模块 1.0.0 或更高版本。If you choose to install and use the PowerShell locally, this article requires the Azure PowerShell module version 1.0.0 or later. 若要查找版本,请运行 Get-Module -ListAvailable AzTo find the version, run Get-Module -ListAvailable Az . 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。If you need to upgrade, see Install Azure PowerShell module. 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount -Environment AzureChinaCloud 以创建与 Azure 的连接。If you're running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

因为创建资源需要时间,所以可能需要最多 90 分钟才能完成本过程。Because of the time needed to create resources, it can take up to 90 minutes to complete this procedure.

创建资源组Create a resource group

创建包含应用程序的所有资源的资源组。Create a resource group that contains all of the resources for your application.

使用 New-AzResourceGroup 创建 Azure 资源组。Create an Azure resource group using New-AzResourceGroup.

New-AzResourceGroup -Name myResourceGroupAG -Location chinanorth

创建网络资源Create network resources

无论是具有现有虚拟网络还是创建新的虚拟网络,都需要确保它包含一个仅用于应用程序网关的子网。Whether you have an existing virtual network or create a new one, you need to make sure that it contains a subnet that is only used for application gateways. 在本文中,你将为应用程序网关创建一个子网,为规模集创建一个子网。In this article, you create a subnet for the application gateway and a subnet for the scale sets. 将创建一个公用 IP 地址以便访问应用程序网关中的资源。You create a public IP address to enable access to the resources in the application gateway.

使用 New-AzVirtualNetworkSubnetConfig 创建子网配置 myAGSubnetmyBackendSubnetCreate the subnet configurations myAGSubnet and myBackendSubnet using New-AzVirtualNetworkSubnetConfig. 使用 New-AzVirtualNetwork 和子网配置创建名为 myVNet 的虚拟网络。Create the virtual network named myVNet using New-AzVirtualNetwork with the subnet configurations. 最后使用 New-AzPublicIpAddress 创建名为 myAGPublicIPAddress 的公共 IP 地址。And finally, create the public IP address named myAGPublicIPAddress using New-AzPublicIpAddress. 这些资源用于提供与应用程序网关及其关联资源的网络连接。These resources are used to provide network connectivity to the application gateway and its associated resources.

$backendSubnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name myBackendSubnet `
  -AddressPrefix 10.0.1.0/24

$agSubnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name myAGSubnet `
  -AddressPrefix 10.0.2.0/24

$vnet = New-AzVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Location chinanorth `
  -Name myVNet `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $backendSubnetConfig, $agSubnetConfig
$pip = New-AzPublicIpAddress `
  -ResourceGroupName myResourceGroupAG `
  -Location chinanorth `
  -Name myAGPublicIPAddress `
  -AllocationMethod Static `
  -Sku Standard

创建应用程序网关Create an application gateway

本部分将创建支持应用程序网关的资源,然后最终创建应用程序网关。In this section, you create resources that support the application gateway, and then finally create it. 创建的资源包括:The resources you create include:

  • IP 配置和前端端口 - 将前面创建的子网关联到应用程序网关,并分配一个端口用于访问该子网。IP configurations and frontend port - Associates the subnet you previously created to the application gateway and assigns a port to use to access it.
  • 默认池 - 所有应用程序网关必须至少具有一个后端服务器池。Default pool - All application gateways must have at least one backend pool of servers.
  • 默认侦听器和规则 - 默认侦听器侦听已分配的端口上的流量,默认规则将流量发送到默认池。Default listener and rule - The default listener listens for traffic on the port that was assigned and the default rule sends traffic to the default pool.

创建 IP 配置和前端端口Create the IP configurations and frontend port

使用 New-AzApplicationGatewayIPConfiguration 将前面创建的 myAGSubnet 关联到应用程序网关。Associate myAGSubnet you previously created to the application gateway using New-AzApplicationGatewayIPConfiguration. 使用 New-AzApplicationGatewayFrontendIPConfigmyAGPublicIPAddress 分配给应用程序网关。Assign myAGPublicIPAddress to the application gateway using New-AzApplicationGatewayFrontendIPConfig.

$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Name myVNet

$subnet=$vnet.Subnets[0]

$pip = Get-AzPublicIpAddress `
  -ResourceGroupName myResourceGroupAG `
  -Name myAGPublicIPAddress

$gipconfig = New-AzApplicationGatewayIPConfiguration `
  -Name myAGIPConfig `
  -Subnet $subnet

$fipconfig = New-AzApplicationGatewayFrontendIPConfig `
  -Name myAGFrontendIPConfig `
  -PublicIPAddress $pip

$frontendport = New-AzApplicationGatewayFrontendPort `
  -Name myFrontendPort `
  -Port 80

创建默认池和设置Create the default pool and settings

使用 New-AzApplicationGatewayBackendAddressPool 为应用程序网关创建名为 appGatewayBackendPool 的默认后端池。Create the default backend pool named appGatewayBackendPool for the application gateway using New-AzApplicationGatewayBackendAddressPool. 使用 New-AzApplicationGatewayBackendHttpSetting 配置后端池的设置。Configure the settings for the backend pool using New-AzApplicationGatewayBackendHttpSetting.

$defaultPool = New-AzApplicationGatewayBackendAddressPool `
  -Name appGatewayBackendPool

$poolSettings = New-AzApplicationGatewayBackendHttpSetting `
  -Name myPoolSettings `
  -Port 80 `
  -Protocol Http `
  -CookieBasedAffinity Enabled `
  -RequestTimeout 120

创建默认侦听器和规则Create the default listener and rule

应用程序网关需要侦听器才能适当地将流量路由到后端池。A listener is required to enable the application gateway to route traffic appropriately to the backend pool. 在本文中,你将创建两个侦听器。In this article, you create two listeners. 创建的第一个基本侦听器侦听根 URL 上的流量。The first basic listener that you create listens for traffic at the root URL. 创建的第二个侦听器侦听特定 URL 上的流量。The second listener that you create listens for traffic at specific URLs.

使用 New-AzApplicationGatewayHttpListener 以及前面创建的前端配置和前端端口创建名为 myDefaultListener 的默认侦听器。Create the default listener named myDefaultListener using New-AzApplicationGatewayHttpListener with the frontend configuration and frontend port that you previously created.

侦听器需要使用规则来了解哪个后端池使用传入流量。A rule is required for the listener to know which backend pool to use for incoming traffic. 使用 New-AzApplicationGatewayRequestRoutingRule 创建一个名为 rule1 的基本规则。Create a basic rule named rule1 using New-AzApplicationGatewayRequestRoutingRule.

$defaultlistener = New-AzApplicationGatewayHttpListener `
  -Name myDefaultListener `
  -Protocol Http `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $frontendport

$frontendRule = New-AzApplicationGatewayRequestRoutingRule `
  -Name rule1 `
  -RuleType Basic `
  -HttpListener $defaultlistener `
  -BackendAddressPool $defaultPool `
  -BackendHttpSettings $poolSettings

创建应用程序网关Create the application gateway

现在已创建所需的支持资源,请使用 New-AzApplicationGatewaySku 为名为 myAppGateway 的应用程序网关指定参数,然后再使用 New-AzApplicationGateway 创建它。Now that you created the necessary supporting resources, specify parameters for the application gateway named myAppGateway using New-AzApplicationGatewaySku, and then create it using New-AzApplicationGateway.

$sku = New-AzApplicationGatewaySku `
  -Name Standard_v2 `
  -Tier Standard_v2 `
  -Capacity 2

$appgw = New-AzApplicationGateway `
  -Name myAppGateway `
  -ResourceGroupName myResourceGroupAG `
  -Location chinanorth `
  -BackendAddressPools $defaultPool `
  -BackendHttpSettingsCollection $poolSettings `
  -FrontendIpConfigurations $fipconfig `
  -GatewayIpConfigurations $gipconfig `
  -FrontendPorts $frontendport `
  -HttpListeners $defaultlistener `
  -RequestRoutingRules $frontendRule `
  -Sku $sku

创建应用程序网关最长可能需要花费 30 分钟。It may take up to 30 minutes to create the application gateway. 请等待部署成功完成,然后再前进到下一部分。Wait until the deployment finishes successfully before moving on to the next section.

现已创建一个应用程序网关,它会侦听端口 80 上的流量,并将该流量发送到默认的服务器池。At this point, you have an application gateway that listens for traffic on port 80 and sends that traffic to a default server pool.

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

向应用程序网关 Add-AzApplicationGatewayBackendAddressPool 添加名为 imagesBackendPoolvideoBackendPool 的后端池。Add backend pools named imagesBackendPool and videoBackendPool to your application gatewayAdd-AzApplicationGatewayBackendAddressPool. 使用 Add-AzApplicationGatewayFrontendPort 添加池的前端端口。Add the frontend port for the pools using Add-AzApplicationGatewayFrontendPort. 使用 Set-AzApplicationGateway 提交对应用程序网关所做的更改。Submit the changes to the application gateway using Set-AzApplicationGateway.

$appgw = Get-AzApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway

Add-AzApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name imagesBackendPool

Add-AzApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name videoBackendPool

Add-AzApplicationGatewayFrontendPort `
  -ApplicationGateway $appgw `
  -Name bport `
  -Port 8080

Set-AzApplicationGateway -ApplicationGateway $appgw

更新应用程序网关可能也需要最多 20 分钟才能完成。Updating the application gateway can also take up to 20 minutes to finish.

添加后端侦听器Add backend listener

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

$appgw = Get-AzApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway

$backendPort = Get-AzApplicationGatewayFrontendPort `
  -ApplicationGateway $appgw `
  -Name bport

$fipconfig = Get-AzApplicationGatewayFrontendIPConfig `
  -ApplicationGateway $appgw

Add-AzApplicationGatewayHttpListener `
  -ApplicationGateway $appgw `
  -Name backendListener `
  -Protocol Http `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $backendPort

Set-AzApplicationGateway -ApplicationGateway $appgw

添加 URL 路径映射Add URL path map

URL 路径映射可确保将发送到应用程序的 URL 路由到特定的后端池。URL path maps make sure that the URLs sent to your application are routed to specific backend pools. 使用 New-AzApplicationGatewayPathRuleConfigAdd-AzApplicationGatewayUrlPathMapConfig 创建名为 imagePathRulevideoPathRule 的 URL 路径映射。Create URL path maps named imagePathRule and videoPathRule using New-AzApplicationGatewayPathRuleConfig and Add-AzApplicationGatewayUrlPathMapConfig.

$appgw = Get-AzApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway

$poolSettings = Get-AzApplicationGatewayBackendHttpSetting `
  -ApplicationGateway $appgw `
  -Name myPoolSettings

$imagePool = Get-AzApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name imagesBackendPool

$videoPool = Get-AzApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name videoBackendPool

$defaultPool = Get-AzApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name appGatewayBackendPool

$imagePathRule = New-AzApplicationGatewayPathRuleConfig `
  -Name imagePathRule `
  -Paths "/images/*" `
  -BackendAddressPool $imagePool `
  -BackendHttpSettings $poolSettings

$videoPathRule = New-AzApplicationGatewayPathRuleConfig `
  -Name videoPathRule `
    -Paths "/video/*" `
    -BackendAddressPool $videoPool `
    -BackendHttpSettings $poolSettings

Add-AzApplicationGatewayUrlPathMapConfig `
  -ApplicationGateway $appgw `
  -Name urlpathmap `
  -PathRules $imagePathRule, $videoPathRule `
  -DefaultBackendAddressPool $defaultPool `
  -DefaultBackendHttpSettings $poolSettings

Set-AzApplicationGateway -ApplicationGateway $appgw

添加路由规则Add routing rule

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

$appgw = Get-AzApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway

$backendlistener = Get-AzApplicationGatewayHttpListener `
  -ApplicationGateway $appgw `
  -Name backendListener

$urlPathMap = Get-AzApplicationGatewayUrlPathMapConfig `
  -ApplicationGateway $appgw `
  -Name urlpathmap

Add-AzApplicationGatewayRequestRoutingRule `
  -ApplicationGateway $appgw `
  -Name rule2 `
  -RuleType PathBasedRouting `
  -HttpListener $backendlistener `
  -UrlPathMap $urlPathMap

Set-AzApplicationGateway -ApplicationGateway $appgw

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

在此示例中,将创建三个虚拟机规模集以支持所创建的三个后端池。In this example, you create three virtual machine scale sets that support the three backend pools that you created. 创建的规模集分别名为 myvmss1myvmss2myvmss3The scale sets that you create are named myvmss1, myvmss2, and myvmss3. 配置 IP 设置时将规模集分配给后端池。You assign the scale set to the backend pool when you configure the IP settings.

$vnet = Get-AzVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Name myVNet

$appgw = Get-AzApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway

$backendPool = Get-AzApplicationGatewayBackendAddressPool `
  -Name appGatewayBackendPool `
  -ApplicationGateway $appgw

$imagesPool = Get-AzApplicationGatewayBackendAddressPool `
  -Name imagesBackendPool `
  -ApplicationGateway $appgw

$videoPool = Get-AzApplicationGatewayBackendAddressPool `
  -Name videoBackendPool `
  -ApplicationGateway $appgw

for ($i=1; $i -le 3; $i++)
{
  if ($i -eq 1)
  {
     $poolId = $backendPool.Id
  }
  if ($i -eq 2) 
  {
    $poolId = $imagesPool.Id
  }
  if ($i -eq 3)
  {
    $poolId = $videoPool.Id
  }

  $ipConfig = New-AzVmssIpConfig `
    -Name myVmssIPConfig$i `
    -SubnetId $vnet.Subnets[1].Id `
    -ApplicationGatewayBackendAddressPoolsId $poolId

  $vmssConfig = New-AzVmssConfig `
    -Location chinanorth `
    -SkuCapacity 2 `
    -SkuName Standard_DS2 `
    -UpgradePolicyMode Automatic

  Set-AzVmssStorageProfile $vmssConfig `
    -ImageReferencePublisher MicrosoftWindowsServer `
    -ImageReferenceOffer WindowsServer `
    -ImageReferenceSku 2016-Datacenter `
    -ImageReferenceVersion latest `
    -OsDiskCreateOption FromImage

  Set-AzVmssOsProfile $vmssConfig `
    -AdminUsername azureuser `
    -AdminPassword "Azure123456!" `
    -ComputerNamePrefix myvmss$i

  Add-AzVmssNetworkInterfaceConfiguration `
    -VirtualMachineScaleSet $vmssConfig `
    -Name myVmssNetConfig$i `
    -Primary $true `
    -IPConfiguration $ipConfig

  New-AzVmss `
    -ResourceGroupName myResourceGroupAG `
    -Name myvmss$i `
    -VirtualMachineScaleSet $vmssConfig
}

安装 IISInstall IIS

每个规模集包含两个在其上安装了 IIS 的虚拟机实例。Each scale set contains two virtual machine instances on which you install IIS. 将会创建一个示例页面用于测试应用程序网关是否正常工作。A sample page is created to test if the application gateway is working.

$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/appgatewayurl.ps1"); 
  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" }

for ($i=1; $i -le 3; $i++)
{
  $vmss = Get-AzVmss -ResourceGroupName myResourceGroupAG -VMScaleSetName myvmss$i
  Add-AzVmssExtension -VirtualMachineScaleSet $vmss `
    -Name "customScript" `
    -Publisher "Microsoft.Compute" `
    -Type "CustomScriptExtension" `
    -TypeHandlerVersion 1.8 `
    -Setting $publicSettings

  Update-AzVmss `
    -ResourceGroupName myResourceGroupAG `
    -Name myvmss$i `
    -VirtualMachineScaleSet $vmss
}

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

使用 Get-AzPublicIPAddress 获取应用程序网关的公共 IP 地址。Use Get-AzPublicIPAddress to get the public IP address of the application gateway. 复制该公共 IP 地址,并将其粘贴到浏览器的地址栏。Copy the public IP address, and then paste it into the address bar of your browser. 例如,http://52.168.55.24http://52.168.55.24:8080/images/test.htmhttp://52.168.55.24:8080/video/test.htmSuch as, http://52.168.55.24, http://52.168.55.24:8080/images/test.htm, or http://52.168.55.24:8080/video/test.htm.

Get-AzPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress

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

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

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

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

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

清理资源Clean up resources

如果不再需要资源组、应用程序网关和所有相关资源,可以使用 Remove-AzResourceGroup 将其删除。When no longer needed, remove the resource group, application gateway, and all related resources using Remove-AzResourceGroup.

Remove-AzResourceGroup -Name myResourceGroupAG

后续步骤Next steps

基于 URL 重定向 Web 流量Redirect web traffic based on the URL