Creare un gateway applicazione con regole di routing basato su percorsi URL usando Azure PowerShellCreate an application gateway with URL path-based routing rules using Azure PowerShell

È possibile usare Azure PowerShell per configurare regole di routing basato su percorsi URL quando si crea un gateway applicazione.You can use Azure PowerShell to configure URL path-based routing rules when you create an application gateway. In questa esercitazione si creano pool back-end usando un set di scalabilità di macchine virtuali.In this tutorial, you create backend pools using a virtual machine scale set. Si creano quindi le regole di routing per garantire che il traffico Web raggiunga i server appropriati nei pool.You then create routing rules that make sure web traffic arrives at the appropriate servers in the pools.

In questo articolo viene spiegato come:In this article, you learn how to:

  • Configurare la reteSet up the network
  • Creare un gateway applicazione con il mapping di URLCreate an application gateway with URL map
  • Creare set di scalabilità di macchine virtuali con i pool back-endCreate virtual machine scale sets with the backend pools

Esempio di routing basato su URL

Se non si ha una sottoscrizione di Azure, creare un account gratuito prima di iniziare.If you don't have an Azure subscription, create a free account before you begin.

Avviare Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell è una shell interattiva gratuita che può essere usata per eseguire la procedura di questo articolo.The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. Include strumenti comuni di Azure preinstallati e configurati per l'uso con l'account.It has common Azure tools preinstalled and configured to use with your account. È sufficiente fare clic su Copia per copiare il codice, incollarlo in Cloud Shell e quindi premere INVIO per eseguirlo.Just click the Copy to copy the code, paste it into the Cloud Shell, and then press enter to run it. Esistono alcuni modi per avviare Cloud Shell:There are a few ways to launch the Cloud Shell:

Fare clic su Prova nell'angolo superiore destro di un blocco di codice.Click Try It in the upper right corner of a code block. Cloud Shell in questo articolo
Aprire Cloud Shell nel browser.Open Cloud Shell in your browser. https://shell.azure.com/powershell
Fare clic sul pulsante Cloud Shell nel menu in alto a destra nel portale di Azure.Click the Cloud Shell button on the menu in the upper right of the Azure portal. Cloud Shell nel portaleCloud Shell in the portal

Se si sceglie di installare e usare PowerShell in locale, per questa esercitazione è necessario il modulo Azure PowerShell versione 3.6 o successiva.If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 3.6 or later. Per trovare la versione, eseguire Get-Module -ListAvailable AzureRM.To find the version, run Get-Module -ListAvailable AzureRM . Se è necessario eseguire l'aggiornamento, vedere Installare e configurare Azure PowerShell.If you need to upgrade, see Install Azure PowerShell module. Se si esegue PowerShell in locale, è anche necessario eseguire Connect-AzureRmAccount per creare una connessione con Azure.If you are running PowerShell locally, you also need to run Connect-AzureRmAccount to create a connection with Azure.

Creare un gruppo di risorseCreate a resource group

Un gruppo di risorse è un contenitore logico in cui le risorse di Azure vengono distribuite e gestite.A resource group is a logical container into which Azure resources are deployed and managed. Creare un gruppo di risorse di Azure usando New-AzureRmResourceGroup.Create an Azure resource group using New-AzureRmResourceGroup.

New-AzureRmResourceGroup -Name myResourceGroupAG -Location eastus

Creare risorse di reteCreate network resources

Creare le configurazioni di subnet myAGSubnet e myBackendSubnet usando New-AzureRmVirtualNetworkSubnetConfig.Create the subnet configurations myAGSubnet and myBackendSubnet using New-AzureRmVirtualNetworkSubnetConfig. Creare la rete virtuale denominata myVNet usando New-AzureRmVirtualNetwork con le configurazioni di subnet.Create the virtual network named myVNet using New-AzureRmVirtualNetwork with the subnet configurations. Creare infine l'indirizzo IP pubblico denominato myAGPublicIPAddress con New AzureRmPublicIpAddress.And finally, create the public IP address named myAGPublicIPAddress using New-AzureRmPublicIpAddress. Queste risorse vengono usate per fornire la connettività di rete al gateway applicazione e alle risorse associate.These resources are used to provide network connectivity to the application gateway and its associated resources.

$backendSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
  -Name myBackendSubnet `
  -AddressPrefix 10.0.1.0/24
$agSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
  -Name myAGSubnet `
  -AddressPrefix 10.0.2.0/24
$vnet = New-AzureRmVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -Name myVNet `
  -AddressPrefix 10.0.0.0/16 `
  -Subnet $backendSubnetConfig, $agSubnetConfig
$pip = New-AzureRmPublicIpAddress `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -Name myAGPublicIPAddress `
  -AllocationMethod Dynamic

Creare un gateway applicazioneCreate an application gateway

Creare le configurazioni IP e la porta front-endCreate the IP configurations and frontend port

Associare la subnet myAGSubnet creata in precedenza al gateway applicazione usando New-AzureRmApplicationGatewayIPConfiguration.Associate myAGSubnet that you previously created to the application gateway using New-AzureRmApplicationGatewayIPConfiguration. Assegnare myAGPublicIPAddress al gateway applicazione usando New-AzureRmApplicationGatewayFrontendIPConfig.Assign the myAGPublicIPAddress to the application gateway using New-AzureRmApplicationGatewayFrontendIPConfig.

$vnet = Get-AzureRmVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Name myVNet
$subnet=$vnet.Subnets[0]
$pip = Get-AzureRmPublicIpAddress `
  -ResourceGroupName myResourceGroupAG `
  -Name myAGPublicIPAddress
$gipconfig = New-AzureRmApplicationGatewayIPConfiguration `
  -Name myAGIPConfig `
  -Subnet $subnet
$fipconfig = New-AzureRmApplicationGatewayFrontendIPConfig `
  -Name myAGFrontendIPConfig `
  -PublicIPAddress $pip
$frontendport = New-AzureRmApplicationGatewayFrontendPort `
  -Name myFrontendPort `
  -Port 80

Creare il pool predefinito e le impostazioniCreate the default pool and settings

Creare il pool back-end predefinito denominato appGatewayBackendPool per il gateway applicazione usando New-AzureRmApplicationGatewayBackendAddressPool.Create the default backend pool named appGatewayBackendPool for the application gateway using New-AzureRmApplicationGatewayBackendAddressPool. Configurare le impostazioni per il pool back-end usando New-AzureRmApplicationGatewayBackendHttpSettings.Configure the settings for the backend pool using New-AzureRmApplicationGatewayBackendHttpSettings.

$defaultPool = New-AzureRmApplicationGatewayBackendAddressPool `
  -Name appGatewayBackendPool 
$poolSettings = New-AzureRmApplicationGatewayBackendHttpSettings `
  -Name myPoolSettings `
  -Port 80 `
  -Protocol Http `
  -CookieBasedAffinity Enabled `
  -RequestTimeout 120

Creare il listener predefinito e la regolaCreate the default listener and rule

È necessario un listener per consentire al gateway applicazione di instradare il traffico in modo appropriato al pool back-end.A listener is required to enable the application gateway to route traffic appropriately to the backend pool. In questa esercitazione vengono creati due listener.In this tutorial, you create two listeners. Il primo listener di base creato è in ascolto del traffico nell'URL radice.The first basic listener that you create listens for traffic at the root URL. Il secondo listener creato è in ascolto del traffico in URL specifici.The second listener that you create listens for traffic at specific URLs.

Creare il listener predefinito denominato myDefaultListener usando New-AzureRmApplicationGatewayHttpListener con la configurazione front-end e la porta front-end creata prima.Create the default listener named myDefaultListener using New-AzureRmApplicationGatewayHttpListener with the frontend configuration and frontend port that you previously created. È necessaria una regola per comunicare al listener quale pool back-end usare per il traffico in ingresso.A rule is required for the listener to know which backend pool to use for incoming traffic. Creare una regola di base denominata rule1 usando New-AzureRmApplicationGatewayRequestRoutingRule.Create a basic rule named rule1 using New-AzureRmApplicationGatewayRequestRoutingRule.

$defaultlistener = New-AzureRmApplicationGatewayHttpListener `
  -Name myDefaultListener `
  -Protocol Http `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $frontendport
$frontendRule = New-AzureRmApplicationGatewayRequestRoutingRule `
  -Name rule1 `
  -RuleType Basic `
  -HttpListener $defaultlistener `
  -BackendAddressPool $defaultPool `
  -BackendHttpSettings $poolSettings

Creare il gateway applicazioneCreate the application gateway

Ora che sono state create le risorse di supporto necessarie, specificare i parametri per il gateway applicazione denominato myAppGateway usando New-AzureRmApplicationGatewaySku e quindi crearlo usando New-AzureRmApplicationGateway.Now that you created the necessary supporting resources, specify parameters for the application gateway named myAppGateway using New-AzureRmApplicationGatewaySku, and then create it using New-AzureRmApplicationGateway.

$sku = New-AzureRmApplicationGatewaySku `
  -Name Standard_Medium `
  -Tier Standard `
  -Capacity 2
$appgw = New-AzureRmApplicationGateway `
  -Name myAppGateway `
  -ResourceGroupName myResourceGroupAG `
  -Location eastus `
  -BackendAddressPools $defaultPool `
  -BackendHttpSettingsCollection $poolSettings `
  -FrontendIpConfigurations $fipconfig `
  -GatewayIpConfigurations $gipconfig `
  -FrontendPorts $frontendport `
  -HttpListeners $defaultlistener `
  -RequestRoutingRules $frontendRule `
  -Sku $sku

Aggiungere i pool back-end di immagini e video e la portaAdd image and video backend pools and port

È possibile aggiungere i pool back-end denominati imagesBackendPool e videoBackendPool al gateway applicazione usando Add-AzureRmApplicationGatewayBackendAddressPool.You can add backend pools named imagesBackendPool and videoBackendPool to your application gateway by using Add-AzureRmApplicationGatewayBackendAddressPool. Per aggiungere la porta front-end per i pool, usare Add-AzureRmApplicationGatewayFrontendPort.You add the frontend port for the pools using Add-AzureRmApplicationGatewayFrontendPort. Inviare quindi le modifiche al gateway applicazione usando Set-AzureRmApplicationGateway.You then submit the changes to the application gateway using Set-AzureRmApplicationGateway.

$appgw = Get-AzureRmApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway
Add-AzureRmApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name imagesBackendPool 
Add-AzureRmApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name videoBackendPool
Add-AzureRmApplicationGatewayFrontendPort `
  -ApplicationGateway $appgw `
  -Name bport `
  -Port 8080
Set-AzureRmApplicationGateway -ApplicationGateway $appgw

Aggiungere il listener back-endAdd backend listener

Aggiungere il listener back-end denominato backendListener necessario per instradare il traffico usando Add-AzureRmApplicationGatewayHttpListener.Add the backend listener named backendListener that's needed to route traffic using Add-AzureRmApplicationGatewayHttpListener.

$appgw = Get-AzureRmApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway
$backendPort = Get-AzureRmApplicationGatewayFrontendPort `
  -ApplicationGateway $appgw `
  -Name bport
$fipconfig = Get-AzureRmApplicationGatewayFrontendIPConfig `
  -ApplicationGateway $appgw
Add-AzureRmApplicationGatewayHttpListener `
  -ApplicationGateway $appgw `
  -Name backendListener `
  -Protocol Http `
  -FrontendIPConfiguration $fipconfig `
  -FrontendPort $backendPort
Set-AzureRmApplicationGateway -ApplicationGateway $appgw

Aggiungere la mappa di percorso URLAdd URL path map

Le mappe di percorso URL assicurano che specifici URL vengano instradati a specifici pool back-end.URL path maps make sure that specific URLs are routed to specific backend pools. È possibile creare le mappe di percorso URL denominate imagePathRule e videoPathRule usando New-AzureRmApplicationGatewayPathRuleConfig e Add-AzureRmApplicationGatewayUrlPathMapConfig.You can create URL path maps named imagePathRule and videoPathRule using New-AzureRmApplicationGatewayPathRuleConfig and Add-AzureRmApplicationGatewayUrlPathMapConfig.

$appgw = Get-AzureRmApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway
$poolSettings = Get-AzureRmApplicationGatewayBackendHttpSettings `
  -ApplicationGateway $appgw `
  -Name myPoolSettings
$imagePool = Get-AzureRmApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name imagesBackendPool
$videoPool = Get-AzureRmApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name videoBackendPool
$defaultPool = Get-AzureRmApplicationGatewayBackendAddressPool `
  -ApplicationGateway $appgw `
  -Name appGatewayBackendPool
$imagePathRule = New-AzureRmApplicationGatewayPathRuleConfig `
  -Name imagePathRule `
  -Paths "/images/*" `
  -BackendAddressPool $imagePool `
  -BackendHttpSettings $poolSettings
$videoPathRule = New-AzureRmApplicationGatewayPathRuleConfig `
  -Name videoPathRule `
    -Paths "/video/*" `
    -BackendAddressPool $videoPool `
    -BackendHttpSettings $poolSettings
Add-AzureRmApplicationGatewayUrlPathMapConfig `
  -ApplicationGateway $appgw `
  -Name urlpathmap `
  -PathRules $imagePathRule, $videoPathRule `
  -DefaultBackendAddressPool $defaultPool `
  -DefaultBackendHttpSettings $poolSettings
Set-AzureRmApplicationGateway -ApplicationGateway $appgw

Aggiungere la regola di routingAdd routing rule

La regola di routing associa la mappa URL al listener creato.The routing rule associates the URL map with the listener that you created. È possibile aggiungere la regola denominata *rule2 usando Add-AzureRmApplicationGatewayRequestRoutingRule.You can add the rule named *rule2 using Add-AzureRmApplicationGatewayRequestRoutingRule.

$appgw = Get-AzureRmApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway
$backendlistener = Get-AzureRmApplicationGatewayHttpListener `
  -ApplicationGateway $appgw `
  -Name backendListener
$urlPathMap = Get-AzureRmApplicationGatewayUrlPathMapConfig `
  -ApplicationGateway $appgw `
  -Name urlpathmap
Add-AzureRmApplicationGatewayRequestRoutingRule `
  -ApplicationGateway $appgw `
  -Name rule2 `
  -RuleType PathBasedRouting `
  -HttpListener $backendlistener `
  -UrlPathMap $urlPathMap
Set-AzureRmApplicationGateway -ApplicationGateway $appgw

Creare set di scalabilità di macchine virtualiCreate virtual machine scale sets

In questo esempio si creano tre set di scalabilità di macchine virtuali che supportano i tre pool back-end creati.In this example, you create three virtual machine scale sets that support the three backend pools that you created. I set di scalabilità creati sono denominati myvmss1, myvmss2 e myvmss3.The scale sets that you create are named myvmss1, myvmss2, and myvmss3. Ogni set di scalabilità contiene due istanze di macchina virtuale in cui si installa IIS.Each scale set contains two virtual machine instances on which you install IIS. Il set di scalabilità viene assegnato al pool back-end quando si configurano le impostazioni IP.You assign the scale set to the backend pool when you configure the IP settings.

$vnet = Get-AzureRmVirtualNetwork `
  -ResourceGroupName myResourceGroupAG `
  -Name myVNet
$appgw = Get-AzureRmApplicationGateway `
  -ResourceGroupName myResourceGroupAG `
  -Name myAppGateway
$backendPool = Get-AzureRmApplicationGatewayBackendAddressPool `
  -Name appGatewayBackendPool `
  -ApplicationGateway $appgw
$imagesPool = Get-AzureRmApplicationGatewayBackendAddressPool `
  -Name imagesBackendPool `
  -ApplicationGateway $appgw
$videoPool = Get-AzureRmApplicationGatewayBackendAddressPool `
  -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-AzureRmVmssIpConfig `
    -Name myVmssIPConfig$i `
    -SubnetId $vnet.Subnets[1].Id `
    -ApplicationGatewayBackendAddressPoolsId $poolId
  $vmssConfig = New-AzureRmVmssConfig `
    -Location eastus `
    -SkuCapacity 2 `
    -SkuName Standard_DS2 `
    -UpgradePolicyMode Automatic
  Set-AzureRmVmssStorageProfile $vmssConfig `
    -ImageReferencePublisher MicrosoftWindowsServer `
    -ImageReferenceOffer WindowsServer `
    -ImageReferenceSku 2016-Datacenter `
    -ImageReferenceVersion latest
  Set-AzureRmVmssOsProfile $vmssConfig `
    -AdminUsername azureuser `
    -AdminPassword "Azure123456!" `
    -ComputerNamePrefix myvmss$i
  Add-AzureRmVmssNetworkInterfaceConfiguration `
    -VirtualMachineScaleSet $vmssConfig `
    -Name myVmssNetConfig$i `
    -Primary $true `
    -IPConfiguration $ipConfig
  New-AzureRmVmss `
    -ResourceGroupName myResourceGroupAG `
    -Name myvmss$i `
    -VirtualMachineScaleSet $vmssConfig
}

Installare IISInstall IIS

$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/davidmu1/samplescripts/master/appgatewayurl.ps1"); 
  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" }

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

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

Testare il gateway applicazioneTest the application gateway

È possibile usare Get-AzureRmPublicIPAddress per ottenere l'indirizzo IP pubblico del gateway applicazione.You can use Get-AzureRmPublicIPAddress to get the public IP address of the application gateway. Copiare l'indirizzo IP pubblico e quindi incollarlo nella barra degli indirizzi del browser.Copy the public IP address, and then paste it into the address bar of your browser. Ad esempio, http://52.168.55.24, http://52.168.55.24:8080/images/test.htm oppure http://52.168.55.24:8080/video/test.htm.Such 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-AzureRmPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress

Testare l'URL di base nel gateway applicazione

Modificare l'URL in http://:8080/video/test.htm sostituendo con l'indirizzo IP usato come nell'esempio seguente:Change the URL to http://:8080/video/test.htm, substituting your IP address for , and you should see something like the following example:

Testare l'URL delle immagini nel gateway applicazione

Modificare l'URL in http://:8080/video/test.htm come nell'esempio seguente:Change the URL to http://:8080/video/test.htm and you should see something like the following example:

Testare l'URL video nel gateway applicazione

Passaggi successiviNext steps

In questo articolo si è appreso come:In this article, you learned how to:

  • Configurare la reteSet up the network
  • Creare un gateway applicazione con il mapping di URLCreate an application gateway with URL map
  • Creare set di scalabilità di macchine virtuali con i pool back-endCreate virtual machine scale sets with the backend pools

Per altre informazioni sui gateway applicazione e sulle risorse associate, continuare con le procedure dettagliate.To learn more about application gateways and their associated resources, continue to the how-to articles.