|
| 1 | +--- |
| 2 | +title: Create an application gateway with internal redirection - Azure PowerShell | Microsoft Docs |
| 3 | +description: Learn how to create an application gateway that redirects internal web traffic to the appropriate backend pool of servers using Azure Powershell. |
| 4 | +services: application-gateway |
| 5 | +author: davidmu1 |
| 6 | +manager: timlt |
| 7 | +editor: tysonn |
| 8 | + |
| 9 | +ms.service: application-gateway |
| 10 | +ms.devlang: na |
| 11 | +ms.topic: article |
| 12 | +ms.tgt_pltfrm: na |
| 13 | +ms.workload: infrastructure-services |
| 14 | +ms.date: 01/23/2018 |
| 15 | +ms.author: davidmu |
| 16 | + |
| 17 | +--- |
| 18 | +# Create an application gateway with internal redirection using Azure PowerShell |
| 19 | + |
| 20 | +You can use Azure Powershell to configure [web traffic redirection](application-gateway-multi-site-overview.md) when you create an [application gateway](application-gateway-introduction.md). In this tutorial, you define a backend pool using a virtual machines scale set. You then configure listeners and rules based on domains that you own to make sure web traffic arrives at the appropriate pool. This tutorial assumes that you own multiple domains and uses examples of *www.contoso.com* and *www.contoso.org*. |
| 21 | + |
| 22 | +In this article, you learn how to: |
| 23 | + |
| 24 | +> [!div class="checklist"] |
| 25 | +> * Set up the network |
| 26 | +> * Create an application gateway |
| 27 | +> * Add listeners and redirection rule |
| 28 | +> * Create a virtual machine scale set with the backend pool |
| 29 | +> * Create a CNAME record in your domain |
| 30 | +
|
| 31 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 32 | + |
| 33 | +[!INCLUDE [cloud-shell-powershell.md](../../includes/cloud-shell-powershell.md)] |
| 34 | + |
| 35 | +If you choose to install and use the PowerShell locally, this tutorial requires the Azure PowerShell module version 3.6 or later. To find the version, run ` Get-Module -ListAvailable AzureRM` . If you need to upgrade, see [Install Azure PowerShell module](/powershell/azure/install-azurerm-ps). If you are running PowerShell locally, you also need to run `Login-AzureRmAccount` to create a connection with Azure. |
| 36 | + |
| 37 | +## Create a resource group |
| 38 | + |
| 39 | +A resource group is a logical container into which Azure resources are deployed and managed. Create an Azure resource group using [New-AzureRmResourceGroup](/powershell/module/azurerm.resources/new-azurermresourcegroup). |
| 40 | + |
| 41 | +```azurepowershell-interactive |
| 42 | +New-AzureRmResourceGroup -Name myResourceGroupAG -Location eastus |
| 43 | +``` |
| 44 | + |
| 45 | +## Create network resources |
| 46 | + |
| 47 | +Create the subnet configurations for *myBackendSubnet* and *myAGSubnet* using [New-AzureRmVirtualNetworkSubnetConfig](/powershell/module/azurerm.network/new-azurermvirtualnetworksubnetconfig). Create the virtual network named *myVNet* using [New-AzureRmVirtualNetwork](/powershell/module/azurerm.network/new-azurermvirtualnetwork) with the subnet configurations. And finally, create the public IP address named *myAGPublicIPAddress* using [New-AzureRmPublicIpAddress](/powershell/module/azurerm.network/new-azurermpublicipaddress). These resources are used to provide network connectivity to the application gateway and its associated resources. |
| 48 | + |
| 49 | +```azurepowershell-interactive |
| 50 | +$backendSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 51 | + -Name myBackendSubnet ` |
| 52 | + -AddressPrefix 10.0.1.0/24 |
| 53 | +$agSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 54 | + -Name myAGSubnet ` |
| 55 | + -AddressPrefix 10.0.2.0/24 |
| 56 | +$vnet = New-AzureRmVirtualNetwork ` |
| 57 | + -ResourceGroupName myResourceGroupAG ` |
| 58 | + -Location eastus ` |
| 59 | + -Name myVNet ` |
| 60 | + -AddressPrefix 10.0.0.0/16 ` |
| 61 | + -Subnet $backendSubnetConfig, $agSubnetConfig |
| 62 | +$pip = New-AzureRmPublicIpAddress ` |
| 63 | + -ResourceGroupName myResourceGroupAG ` |
| 64 | + -Location eastus ` |
| 65 | + -Name myAGPublicIPAddress ` |
| 66 | + -AllocationMethod Dynamic |
| 67 | +``` |
| 68 | + |
| 69 | +## Create an application gateway |
| 70 | + |
| 71 | +### Create the IP configurations and frontend port |
| 72 | + |
| 73 | +Associate *myAGSubnet* that you previously created to the application gateway using [New-AzureRmApplicationGatewayIPConfiguration](/powershell/module/azurerm.network/new-azurermapplicationgatewayipconfiguration). Assign *myAGPublicIPAddress* to the application gateway using [New-AzureRmApplicationGatewayFrontendIPConfig](/powershell/module/azurerm.network/new-azurermapplicationgatewayfrontendipconfig). And then you can create the HTTP port using [New-AzureRmApplicationGatewayFrontendPort](/powershell/module/azurerm.network/new-azurermapplicationgatewayfrontendport). |
| 74 | + |
| 75 | +```azurepowershell-interactive |
| 76 | +$vnet = Get-AzureRmVirtualNetwork ` |
| 77 | + -ResourceGroupName myResourceGroupAG ` |
| 78 | + -Name myVNet |
| 79 | +$subnet=$vnet.Subnets[0] |
| 80 | +$pip = Get-AzureRmPublicIpAddress ` |
| 81 | + -ResourceGroupName myResourceGroupAG ` |
| 82 | + -Name myAGPublicIPAddress |
| 83 | +$gipconfig = New-AzureRmApplicationGatewayIPConfiguration ` |
| 84 | + -Name myAGIPConfig ` |
| 85 | + -Subnet $subnet |
| 86 | +$fipconfig = New-AzureRmApplicationGatewayFrontendIPConfig ` |
| 87 | + -Name myAGFrontendIPConfig ` |
| 88 | + -PublicIPAddress $pip |
| 89 | +$frontendPort = New-AzureRmApplicationGatewayFrontendPort ` |
| 90 | + -Name myFrontendPort ` |
| 91 | + -Port 80 |
| 92 | +``` |
| 93 | + |
| 94 | +### Create the backend pool and settings |
| 95 | + |
| 96 | +Create a backend pool named *contosoPool* for the application gateway using [New-AzureRmApplicationGatewayBackendAddressPool](/powershell/module/azurerm.network/new-azurermapplicationgatewaybackendaddresspool). Configure the settings for the backend pool using [New-AzureRmApplicationGatewayBackendHttpSettings](/powershell/module/azurerm.network/new-azurermapplicationgatewaybackendhttpsettings). |
| 97 | + |
| 98 | +```azurepowershell-interactive |
| 99 | +$contosoPool = New-AzureRmApplicationGatewayBackendAddressPool ` |
| 100 | + -Name contosoPool |
| 101 | +$poolSettings = New-AzureRmApplicationGatewayBackendHttpSettings ` |
| 102 | + -Name myPoolSettings ` |
| 103 | + -Port 80 ` |
| 104 | + -Protocol Http ` |
| 105 | + -CookieBasedAffinity Enabled ` |
| 106 | + -RequestTimeout 120 |
| 107 | +``` |
| 108 | + |
| 109 | +### Create the first listener and rule |
| 110 | + |
| 111 | +A listener is required to enable the application gateway to route traffic appropriately to the backend pool. In this tutorial, you create two listeners for your two domains. In this example, listeners are created for the domains of *www.contoso.com* and *www.contoso.org*. |
| 112 | + |
| 113 | +Create the first listener named *contosoComListener* using [New-AzureRmApplicationGatewayHttpListener](/powershell/module/azurerm.network/new-azurermapplicationgatewayhttplistener) 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. Create a basic rule named *contosoComRule* using [New-AzureRmApplicationGatewayRequestRoutingRule](/powershell/module/azurerm.network/new-azurermapplicationgatewayrequestroutingrule). |
| 114 | + |
| 115 | +```azurepowershell-interactive |
| 116 | +$contosoComlistener = New-AzureRmApplicationGatewayHttpListener ` |
| 117 | + -Name contosoComListener ` |
| 118 | + -Protocol Http ` |
| 119 | + -FrontendIPConfiguration $fipconfig ` |
| 120 | + -FrontendPort $frontendPort ` |
| 121 | + -HostName "www.contoso.com" |
| 122 | +$frontendRule = New-AzureRmApplicationGatewayRequestRoutingRule ` |
| 123 | + -Name contosoComRule ` |
| 124 | + -RuleType Basic ` |
| 125 | + -HttpListener $contosoComListener ` |
| 126 | + -BackendAddressPool $contosoPool ` |
| 127 | + -BackendHttpSettings $poolSettings |
| 128 | +``` |
| 129 | + |
| 130 | +### Create the application gateway |
| 131 | + |
| 132 | +Now that you created the necessary supporting resources, specify parameters for the application gateway named *myAppGateway* using [New-AzureRmApplicationGatewaySku](/powershell/module/azurerm.network/new-azurermapplicationgatewaysku), and then create it using [New-AzureRmApplicationGateway](/powershell/module/azurerm.network/new-azurermapplicationgateway). |
| 133 | + |
| 134 | +```azurepowershell-interactive |
| 135 | +$sku = New-AzureRmApplicationGatewaySku ` |
| 136 | + -Name Standard_Medium ` |
| 137 | + -Tier Standard ` |
| 138 | + -Capacity 2 |
| 139 | +$appgw = New-AzureRmApplicationGateway ` |
| 140 | + -Name myAppGateway ` |
| 141 | + -ResourceGroupName myResourceGroupAG ` |
| 142 | + -Location eastus ` |
| 143 | + -BackendAddressPools $contosoPool ` |
| 144 | + -BackendHttpSettingsCollection $poolSettings ` |
| 145 | + -FrontendIpConfigurations $fipconfig ` |
| 146 | + -GatewayIpConfigurations $gipconfig ` |
| 147 | + -FrontendPorts $frontendPort ` |
| 148 | + -HttpListeners $contosoComListener ` |
| 149 | + -RequestRoutingRules $frontendRule ` |
| 150 | + -Sku $sku |
| 151 | +``` |
| 152 | + |
| 153 | +### Add the second listener |
| 154 | + |
| 155 | +Add the listener named *contosoOrgListener* that's needed to redirect traffic using [Add-AzureRmApplicationGatewayHttpListener](/powershell/module/azurerm.network/add-azurermapplicationgatewayhttplistener). |
| 156 | + |
| 157 | +```azurepowershell-interactive |
| 158 | +$appgw = Get-AzureRmApplicationGateway ` |
| 159 | + -ResourceGroupName myResourceGroupAG ` |
| 160 | + -Name myAppGateway |
| 161 | +$frontendPort = Get-AzureRmApplicationGatewayFrontendPort ` |
| 162 | + -Name myFrontendPort ` |
| 163 | + -ApplicationGateway $appgw |
| 164 | +$ipconfig = Get-AzureRmApplicationGatewayFrontendIPConfig ` |
| 165 | + -Name myAGFrontendIPConfig ` |
| 166 | + -ApplicationGateway $appgw |
| 167 | +Add-AzureRmApplicationGatewayHttpListener ` |
| 168 | + -ApplicationGateway $appgw ` |
| 169 | + -Name contosoOrgListener ` |
| 170 | + -Protocol Http ` |
| 171 | + -FrontendIPConfiguration $ipconfig ` |
| 172 | + -FrontendPort $frontendPort ` |
| 173 | + -HostName "www.contoso.org" |
| 174 | +Set-AzureRmApplicationGateway -ApplicationGateway $appgw |
| 175 | +``` |
| 176 | + |
| 177 | +### Add the redirection configuration |
| 178 | + |
| 179 | +You can configure redirection for the listener using [Add-AzureRmApplicationGatewayRedirectConfiguration](/powershell/module/azurerm.network/add-azurermapplicationgatewayredirectconfiguration). |
| 180 | + |
| 181 | +```azurepowershell-interactive |
| 182 | +$appgw = Get-AzureRmApplicationGateway ` |
| 183 | + -ResourceGroupName myResourceGroupAG ` |
| 184 | + -Name myAppGateway |
| 185 | +$contosoComlistener = Get-AzureRmApplicationGatewayHttpListener ` |
| 186 | + -Name contosoComListener ` |
| 187 | + -ApplicationGateway $appgw |
| 188 | +$contosoOrglistener = Get-AzureRmApplicationGatewayHttpListener ` |
| 189 | + -Name contosoOrgListener ` |
| 190 | + -ApplicationGateway $appgw |
| 191 | +Add-AzureRmApplicationGatewayRedirectConfiguration ` |
| 192 | + -ApplicationGateway $appgw ` |
| 193 | + -Name redirectOrgtoCom ` |
| 194 | + -RedirectType Found ` |
| 195 | + -TargetListener $contosoComListener ` |
| 196 | + -IncludePath $true ` |
| 197 | + -IncludeQueryString $true |
| 198 | +Set-AzureRmApplicationGateway -ApplicationGateway $appgw |
| 199 | +``` |
| 200 | + |
| 201 | +### Add the second routing rule |
| 202 | + |
| 203 | +You can then associate the redirection configuration to a new rule named *contosoOrgRule* using [Add-AzureRmApplicationGatewayRequestRoutingRule](/powershell/module/azurerm.network/add-azurermapplicationgatewayrequestroutingrule). |
| 204 | + |
| 205 | +```azurepowershell-interactive |
| 206 | +$appgw = Get-AzureRmApplicationGateway ` |
| 207 | + -ResourceGroupName myResourceGroupAG ` |
| 208 | + -Name myAppGateway |
| 209 | +$contosoOrglistener = Get-AzureRmApplicationGatewayHttpListener ` |
| 210 | + -Name contosoOrgListener ` |
| 211 | + -ApplicationGateway $appgw |
| 212 | +$redirectConfig = Get-AzureRmApplicationGatewayRedirectConfiguration ` |
| 213 | + -Name redirectOrgtoCom ` |
| 214 | + -ApplicationGateway $appgw |
| 215 | +Add-AzureRmApplicationGatewayRequestRoutingRule ` |
| 216 | + -ApplicationGateway $appgw ` |
| 217 | + -Name contosoOrgRule ` |
| 218 | + -RuleType Basic ` |
| 219 | + -HttpListener $contosoOrgListener ` |
| 220 | + -RedirectConfiguration $redirectConfig |
| 221 | +Set-AzureRmApplicationGateway -ApplicationGateway $appgw |
| 222 | +``` |
| 223 | + |
| 224 | +## Create virtual machine scale set |
| 225 | + |
| 226 | +In this example, you create a virtual machine scale set that supports the backend pool that you created. The scale set that you create is named *myvmss* and contains two virtual machine instances on which you install IIS. You assign the scale set to the backend pool when you configure the IP settings. |
| 227 | + |
| 228 | +```azurepowershell-interactive |
| 229 | +$vnet = Get-AzureRmVirtualNetwork ` |
| 230 | + -ResourceGroupName myResourceGroupAG ` |
| 231 | + -Name myVNet |
| 232 | +$appgw = Get-AzureRmApplicationGateway ` |
| 233 | + -ResourceGroupName myResourceGroupAG ` |
| 234 | + -Name myAppGateway |
| 235 | +$backendPool = Get-AzureRmApplicationGatewayBackendAddressPool ` |
| 236 | + -Name contosoPool ` |
| 237 | + -ApplicationGateway $appgw |
| 238 | +$ipConfig = New-AzureRmVmssIpConfig ` |
| 239 | + -Name myVmssIPConfig ` |
| 240 | + -SubnetId $vnet.Subnets[1].Id ` |
| 241 | + -ApplicationGatewayBackendAddressPoolsId $backendPool.Id |
| 242 | +$vmssConfig = New-AzureRmVmssConfig ` |
| 243 | + -Location eastus ` |
| 244 | + -SkuCapacity 2 ` |
| 245 | + -SkuName Standard_DS2 ` |
| 246 | + -UpgradePolicyMode Automatic |
| 247 | +Set-AzureRmVmssStorageProfile $vmssConfig ` |
| 248 | + -ImageReferencePublisher MicrosoftWindowsServer ` |
| 249 | + -ImageReferenceOffer WindowsServer ` |
| 250 | + -ImageReferenceSku 2016-Datacenter ` |
| 251 | + -ImageReferenceVersion latest |
| 252 | +Set-AzureRmVmssOsProfile $vmssConfig ` |
| 253 | + -AdminUsername azureuser ` |
| 254 | + -AdminPassword "Azure123456!" ` |
| 255 | + -ComputerNamePrefix myvmss |
| 256 | +Add-AzureRmVmssNetworkInterfaceConfiguration ` |
| 257 | + -VirtualMachineScaleSet $vmssConfig ` |
| 258 | + -Name myVmssNetConfig ` |
| 259 | + -Primary $true ` |
| 260 | + -IPConfiguration $ipConfig |
| 261 | +New-AzureRmVmss ` |
| 262 | + -ResourceGroupName myResourceGroupAG ` |
| 263 | + -Name myvmss ` |
| 264 | + -VirtualMachineScaleSet $vmssConfig |
| 265 | +``` |
| 266 | + |
| 267 | +### Install IIS |
| 268 | + |
| 269 | +```azurepowershell-interactive |
| 270 | +$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/davidmu1/samplescripts/master/appgatewayurl.ps1"); |
| 271 | + "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" } |
| 272 | +$vmss = Get-AzureRmVmss -ResourceGroupName myResourceGroupAG -VMScaleSetName myvmss |
| 273 | +Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss ` |
| 274 | + -Name "customScript" ` |
| 275 | + -Publisher "Microsoft.Compute" ` |
| 276 | + -Type "CustomScriptExtension" ` |
| 277 | + -TypeHandlerVersion 1.8 ` |
| 278 | + -Setting $publicSettings |
| 279 | +Update-AzureRmVmss ` |
| 280 | + -ResourceGroupName myResourceGroupAG ` |
| 281 | + -Name myvmss ` |
| 282 | + -VirtualMachineScaleSet $vmss |
| 283 | +``` |
| 284 | + |
| 285 | +## Create CNAME record in your domain |
| 286 | + |
| 287 | +After the application gateway is created with its public IP address, you can get the DNS address and use it to create a CNAME record in your domain. You can use [Get-AzureRmPublicIPAddress](/powershell/module/azurerm.network/get-azurermpublicipaddress) to get the DNS address of the application gateway. Copy the *fqdn* value of the DNSSettings and use it as the value of the CNAME record that you create. The use of A-records is not recommended because the VIP may change when the application gateway is restarted. |
| 288 | + |
| 289 | +```azurepowershell-interactive |
| 290 | +Get-AzureRmPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress |
| 291 | +``` |
| 292 | + |
| 293 | +## Test the application gateway |
| 294 | + |
| 295 | +Enter your domain name into the address bar of your browser. Such as, http://www.contoso.com. |
| 296 | + |
| 297 | + |
| 298 | + |
| 299 | +Change the address to your other domain, for example http://www.contoso.org and you should see that the traffic has been redirected back to the listener for www.contoso.com. |
| 300 | + |
| 301 | +## Next steps |
| 302 | + |
| 303 | +In this article, you learned how to: |
| 304 | + |
| 305 | +> [!div class="checklist"] |
| 306 | +> * Set up the network |
| 307 | +> * Create an application gateway |
| 308 | +> * Add listeners and redirection rule |
| 309 | +> * Create a virtual machine scale set with the backend pools |
| 310 | +> * Create a CNAME record in your domain |
| 311 | +
|
| 312 | +> [!div class="nextstepaction"] |
| 313 | +> [Learn more about what you can do with application gateway](./application-gateway-introduction.md) |
0 commit comments