|
| 1 | +--- |
| 2 | +title: Create an application gateway with HTTP to HTTPS redirection - Azure PowerShell | Microsoft Docs |
| 3 | +description: Learn how to create an application gateway with redirected traffic from HTTP to HTTPS using Azure PowerShell. |
| 4 | +services: application-gateway |
| 5 | +author: davidmu1 |
| 6 | +manager: timlt |
| 7 | +editor: tysonn |
| 8 | +tags: azure-resource-manager |
| 9 | + |
| 10 | +ms.service: application-gateway |
| 11 | +ms.topic: article |
| 12 | +ms.workload: infrastructure-services |
| 13 | +ms.date: 01/23/2018 |
| 14 | +ms.author: davidmu |
| 15 | + |
| 16 | +--- |
| 17 | +# Create an application gateway with HTTP to HTTPS redirection using Azure PowerShell |
| 18 | + |
| 19 | +You can use the Azure PowerShell to create an [application gateway](overview.md) with a certificate for SSL termination. A routing rule is used to redirect HTTP traffic to the HTTPS port in your application gateway. In this example, you also create a [virtual machine scale set](../virtual-machine-scale-sets/virtual-machine-scale-sets-overview.md) for the backend pool of the application gateway that contains two virtual machine instances. |
| 20 | + |
| 21 | +In this article, you learn how to: |
| 22 | + |
| 23 | +> [!div class="checklist"] |
| 24 | +> * Create a self-signed certificate |
| 25 | +> * Set up a network |
| 26 | +> * Create an application gateway with the certificate |
| 27 | +> * Add a listener and redirection rule |
| 28 | +> * Create a virtual machine scale set with the default backend pool |
| 29 | +
|
| 30 | +If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 31 | + |
| 32 | +This tutorial requires the Azure PowerShell module version 3.6 or later. Run `Get-Module -ListAvailable AzureRM` to find the version. If you need to upgrade, see [Install Azure PowerShell module](/powershell/azure/install-azurerm-ps). To run the commands in this tutorial, you also need to run `Login-AzureRmAccount` to create a connection with Azure. |
| 33 | + |
| 34 | +## Create a self-signed certificate |
| 35 | + |
| 36 | +For production use, you should import a valid certificate signed by a trusted provider. For this tutorial, you create a self-signed certificate using [New-SelfSignedCertificate](https://docs.microsoft.com/powershell/module/pkiclient/new-selfsignedcertificate). You can use [Export-PfxCertificate](https://docs.microsoft.com/powershell/module/pkiclient/export-pfxcertificate) with the Thumbprint that was returned to export a pfx file from the certificate. |
| 37 | + |
| 38 | +```powershell |
| 39 | +New-SelfSignedCertificate ` |
| 40 | + -certstorelocation cert:\localmachine\my ` |
| 41 | + -dnsname www.contoso.com |
| 42 | +``` |
| 43 | + |
| 44 | +You should see something like this result: |
| 45 | + |
| 46 | +``` |
| 47 | +PSParentPath: Microsoft.PowerShell.Security\Certificate::LocalMachine\my |
| 48 | +
|
| 49 | +Thumbprint Subject |
| 50 | +---------- ------- |
| 51 | +E1E81C23B3AD33F9B4D1717B20AB65DBB91AC630 CN=www.contoso.com |
| 52 | +``` |
| 53 | + |
| 54 | +Use the thumbprint to create the pfx file: |
| 55 | + |
| 56 | +```powershell |
| 57 | +$pwd = ConvertTo-SecureString -String "Azure123456!" -Force -AsPlainText |
| 58 | +Export-PfxCertificate ` |
| 59 | + -cert cert:\localMachine\my\E1E81C23B3AD33F9B4D1717B20AB65DBB91AC630 ` |
| 60 | + -FilePath c:\appgwcert.pfx ` |
| 61 | + -Password $pwd |
| 62 | +``` |
| 63 | + |
| 64 | +## Create a resource group |
| 65 | + |
| 66 | +A resource group is a logical container into which Azure resources are deployed and managed. Create an Azure resource group named *myResourceGroupAG* using [New-AzureRmResourceGroup](/powershell/module/azurerm.resources/new-azurermresourcegroup). |
| 67 | + |
| 68 | +```powershell |
| 69 | +New-AzureRmResourceGroup -Name myResourceGroupAG -Location eastus |
| 70 | +``` |
| 71 | + |
| 72 | +## Create network resources |
| 73 | + |
| 74 | +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. |
| 75 | + |
| 76 | +```powershell |
| 77 | +$backendSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 78 | + -Name myBackendSubnet ` |
| 79 | + -AddressPrefix 10.0.1.0/24 |
| 80 | +$agSubnetConfig = New-AzureRmVirtualNetworkSubnetConfig ` |
| 81 | + -Name myAGSubnet ` |
| 82 | + -AddressPrefix 10.0.2.0/24 |
| 83 | +$vnet = New-AzureRmVirtualNetwork ` |
| 84 | + -ResourceGroupName myResourceGroupAG ` |
| 85 | + -Location eastus ` |
| 86 | + -Name myVNet ` |
| 87 | + -AddressPrefix 10.0.0.0/16 ` |
| 88 | + -Subnet $backendSubnetConfig, $agSubnetConfig |
| 89 | +$pip = New-AzureRmPublicIpAddress ` |
| 90 | + -ResourceGroupName myResourceGroupAG ` |
| 91 | + -Location eastus ` |
| 92 | + -Name myAGPublicIPAddress ` |
| 93 | + -AllocationMethod Dynamic |
| 94 | +``` |
| 95 | + |
| 96 | +## Create an application gateway |
| 97 | + |
| 98 | +### Create the IP configurations and frontend port |
| 99 | + |
| 100 | +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 HTTPS port using [New-AzureRmApplicationGatewayFrontendPort](/powershell/module/azurerm.network/new-azurermapplicationgatewayfrontendport). |
| 101 | + |
| 102 | +```powershell |
| 103 | +$vnet = Get-AzureRmVirtualNetwork ` |
| 104 | + -ResourceGroupName myResourceGroupAG ` |
| 105 | + -Name myVNet |
| 106 | +$subnet=$vnet.Subnets[0] |
| 107 | +$gipconfig = New-AzureRmApplicationGatewayIPConfiguration ` |
| 108 | + -Name myAGIPConfig ` |
| 109 | + -Subnet $subnet |
| 110 | +$fipconfig = New-AzureRmApplicationGatewayFrontendIPConfig ` |
| 111 | + -Name myAGFrontendIPConfig ` |
| 112 | + -PublicIPAddress $pip |
| 113 | +$frontendPort = New-AzureRmApplicationGatewayFrontendPort ` |
| 114 | + -Name myFrontendPort ` |
| 115 | + -Port 443 |
| 116 | +``` |
| 117 | + |
| 118 | +### Create the backend pool and settings |
| 119 | + |
| 120 | +Create the backend pool named *appGatewayBackendPool* 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). |
| 121 | + |
| 122 | +```powershell |
| 123 | +$defaultPool = New-AzureRmApplicationGatewayBackendAddressPool ` |
| 124 | + -Name appGatewayBackendPool |
| 125 | +$poolSettings = New-AzureRmApplicationGatewayBackendHttpSettings ` |
| 126 | + -Name myPoolSettings ` |
| 127 | + -Port 80 ` |
| 128 | + -Protocol Http ` |
| 129 | + -CookieBasedAffinity Enabled ` |
| 130 | + -RequestTimeout 120 |
| 131 | +``` |
| 132 | + |
| 133 | +### Create the default listener and rule |
| 134 | + |
| 135 | +A listener is required to enable the application gateway to route traffic appropriately to the backend pool. In this example, you create a basic listener that listens for HTTPS traffic at the root URL. |
| 136 | + |
| 137 | +Create a certificate object using [New-AzureRmApplicationGatewaySslCertificate](/powershell/module/azurerm.network/new-azurermapplicationgatewaysslcertificate) and then create a listener named *appGatewayHttpListener* using [New-AzureRmApplicationGatewayHttpListener](/powershell/module/azurerm.network/new-azurermapplicationgatewayhttplistener) with the frontend configuration, frontend port, and certificate 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 *rule1* using [New-AzureRmApplicationGatewayRequestRoutingRule](/powershell/module/azurerm.network/new-azurermapplicationgatewayrequestroutingrule). |
| 138 | + |
| 139 | +```powershell |
| 140 | +$pwd = ConvertTo-SecureString ` |
| 141 | + -String "Azure123456!" ` |
| 142 | + -Force ` |
| 143 | + -AsPlainText |
| 144 | +$cert = New-AzureRmApplicationGatewaySslCertificate ` |
| 145 | + -Name "appgwcert" ` |
| 146 | + -CertificateFile "c:\appgwcert.pfx" ` |
| 147 | + -Password $pwd |
| 148 | +$defaultListener = New-AzureRmApplicationGatewayHttpListener ` |
| 149 | + -Name appGatewayHttpListener ` |
| 150 | + -Protocol Https ` |
| 151 | + -FrontendIPConfiguration $fipconfig ` |
| 152 | + -FrontendPort $frontendPort ` |
| 153 | + -SslCertificate $cert |
| 154 | +$frontendRule = New-AzureRmApplicationGatewayRequestRoutingRule ` |
| 155 | + -Name rule1 ` |
| 156 | + -RuleType Basic ` |
| 157 | + -HttpListener $defaultListener ` |
| 158 | + -BackendAddressPool $defaultPool ` |
| 159 | + -BackendHttpSettings $poolSettings |
| 160 | +``` |
| 161 | + |
| 162 | +### Create the application gateway |
| 163 | + |
| 164 | +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) with the certificate. |
| 165 | + |
| 166 | +```powershell |
| 167 | +$sku = New-AzureRmApplicationGatewaySku ` |
| 168 | + -Name Standard_Medium ` |
| 169 | + -Tier Standard ` |
| 170 | + -Capacity 2 |
| 171 | +$appgw = New-AzureRmApplicationGateway ` |
| 172 | + -Name myAppGateway ` |
| 173 | + -ResourceGroupName myResourceGroupAG ` |
| 174 | + -Location eastus ` |
| 175 | + -BackendAddressPools $defaultPool ` |
| 176 | + -BackendHttpSettingsCollection $poolSettings ` |
| 177 | + -FrontendIpConfigurations $fipconfig ` |
| 178 | + -GatewayIpConfigurations $gipconfig ` |
| 179 | + -FrontendPorts $frontendPort ` |
| 180 | + -HttpListeners $defaultListener ` |
| 181 | + -RequestRoutingRules $frontendRule ` |
| 182 | + -Sku $sku ` |
| 183 | + -SslCertificates $cert |
| 184 | +``` |
| 185 | + |
| 186 | +## Add a listener and redirection rule |
| 187 | + |
| 188 | +### Add the HTTP port |
| 189 | + |
| 190 | +Add the HTTP port to the application gateway using [Add-AzureRmApplicationGatewayFrontendPort](/powershell/module/azurerm.network/add-azurermapplicationgatewayfrontendport). |
| 191 | + |
| 192 | +```powershell |
| 193 | +$appgw = Get-AzureRmApplicationGateway ` |
| 194 | + -Name myAppGateway ` |
| 195 | + -ResourceGroupName myResourceGroupAG |
| 196 | +Add-AzureRmApplicationGatewayFrontendPort ` |
| 197 | + -Name httpPort ` |
| 198 | + -Port 80 ` |
| 199 | + -ApplicationGateway $appgw |
| 200 | +``` |
| 201 | + |
| 202 | +### Add the HTTP listener |
| 203 | + |
| 204 | +Add the HTTP listener named *myListener* to the application gateway using [Add-AzureRmApplicationGatewayHttpListener](/powershell/module/azurerm.network/add-azurermapplicationgatewayhttplistener). |
| 205 | + |
| 206 | +```powershell |
| 207 | +$fipconfig = Get-AzureRmApplicationGatewayFrontendIPConfig ` |
| 208 | + -Name myAGFrontendIPConfig ` |
| 209 | + -ApplicationGateway $appgw |
| 210 | +$fp = Get-AzureRmApplicationGatewayFrontendPort ` |
| 211 | + -Name httpPort ` |
| 212 | + -ApplicationGateway $appgw |
| 213 | +Add-AzureRmApplicationGatewayHttpListener ` |
| 214 | + -Name myListener ` |
| 215 | + -Protocol Http ` |
| 216 | + -FrontendPort $fp ` |
| 217 | + -FrontendIPConfiguration $fipconfig ` |
| 218 | + -ApplicationGateway $appgw |
| 219 | +``` |
| 220 | + |
| 221 | +### Add the redirection configuration |
| 222 | + |
| 223 | +Add the HTTP to HTTPS redirection configuration to the application gateway using [Add-AzureRmApplicationGatewayRedirectConfiguration](/powershell/module/azurerm.network/add-azurermapplicationgatewayredirectconfiguration). |
| 224 | + |
| 225 | +```powershell |
| 226 | +$defaultListener = Get-AzureRmApplicationGatewayHttpListener ` |
| 227 | + -Name appGatewayHttpListener ` |
| 228 | + -ApplicationGateway $appgw |
| 229 | +Add-AzureRmApplicationGatewayRedirectConfiguration -Name httpToHttps ` |
| 230 | + -RedirectType Permanent ` |
| 231 | + -TargetListener $defaultListener ` |
| 232 | + -IncludePath $true ` |
| 233 | + -IncludeQueryString $true ` |
| 234 | + -ApplicationGateway $appgw |
| 235 | +``` |
| 236 | + |
| 237 | +### Add the routing rule |
| 238 | + |
| 239 | +Add the routing rule with the redirection configuration to the application gateway using [Add-AzureRmApplicationGatewayRequestRoutingRule](/powershell/module/azurerm.network/add-azurermapplicationgatewayrequestroutingrule). |
| 240 | + |
| 241 | +```powershell |
| 242 | +$myListener = Get-AzureRmApplicationGatewayHttpListener ` |
| 243 | + -Name myListener ` |
| 244 | + -ApplicationGateway $appgw |
| 245 | +$redirectConfig = Get-AzureRmApplicationGatewayRedirectConfiguration ` |
| 246 | + -Name httpToHttps ` |
| 247 | + -ApplicationGateway $appgw |
| 248 | +Add-AzureRmApplicationGatewayRequestRoutingRule ` |
| 249 | + -Name rule2 ` |
| 250 | + -RuleType Basic ` |
| 251 | + -HttpListener $myListener ` |
| 252 | + -RedirectConfiguration $redirectConfig ` |
| 253 | + -ApplicationGateway $appgw |
| 254 | +Set-AzureRmApplicationGateway -ApplicationGateway $appgw |
| 255 | +``` |
| 256 | + |
| 257 | +## Create a virtual machine scale set |
| 258 | + |
| 259 | +In this example, you create a virtual machine scale set to provide servers for the backend pool in the application gateway. You assign the scale set to the backend pool when you configure the IP settings. |
| 260 | + |
| 261 | +```powershell |
| 262 | +$vnet = Get-AzureRmVirtualNetwork ` |
| 263 | + -ResourceGroupName myResourceGroupAG ` |
| 264 | + -Name myVNet |
| 265 | +$appgw = Get-AzureRmApplicationGateway ` |
| 266 | + -ResourceGroupName myResourceGroupAG ` |
| 267 | + -Name myAppGateway |
| 268 | +$backendPool = Get-AzureRmApplicationGatewayBackendAddressPool ` |
| 269 | + -Name appGatewayBackendPool ` |
| 270 | + -ApplicationGateway $appgw |
| 271 | +$ipConfig = New-AzureRmVmssIpConfig ` |
| 272 | + -Name myVmssIPConfig ` |
| 273 | + -SubnetId $vnet.Subnets[1].Id ` |
| 274 | + -ApplicationGatewayBackendAddressPoolsId $backendPool.Id |
| 275 | +$vmssConfig = New-AzureRmVmssConfig ` |
| 276 | + -Location eastus ` |
| 277 | + -SkuCapacity 2 ` |
| 278 | + -SkuName Standard_DS2 ` |
| 279 | + -UpgradePolicyMode Automatic |
| 280 | +Set-AzureRmVmssStorageProfile $vmssConfig ` |
| 281 | + -ImageReferencePublisher MicrosoftWindowsServer ` |
| 282 | + -ImageReferenceOffer WindowsServer ` |
| 283 | + -ImageReferenceSku 2016-Datacenter ` |
| 284 | + -ImageReferenceVersion latest |
| 285 | +Set-AzureRmVmssOsProfile $vmssConfig ` |
| 286 | + -AdminUsername azureuser ` |
| 287 | + -AdminPassword "Azure123456!" ` |
| 288 | + -ComputerNamePrefix myvmss |
| 289 | +Add-AzureRmVmssNetworkInterfaceConfiguration ` |
| 290 | + -VirtualMachineScaleSet $vmssConfig ` |
| 291 | + -Name myVmssNetConfig ` |
| 292 | + -Primary $true ` |
| 293 | + -IPConfiguration $ipConfig |
| 294 | +New-AzureRmVmss ` |
| 295 | + -ResourceGroupName myResourceGroupAG ` |
| 296 | + -Name myvmss ` |
| 297 | + -VirtualMachineScaleSet $vmssConfig |
| 298 | +``` |
| 299 | + |
| 300 | +### Install IIS |
| 301 | + |
| 302 | +```powershell |
| 303 | +$publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/davidmu1/samplescripts/master/appgatewayurl.ps1"); |
| 304 | + "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" } |
| 305 | +$vmss = Get-AzureRmVmss -ResourceGroupName myResourceGroupAG -VMScaleSetName myvmss |
| 306 | +Add-AzureRmVmssExtension -VirtualMachineScaleSet $vmss ` |
| 307 | + -Name "customScript" ` |
| 308 | + -Publisher "Microsoft.Compute" ` |
| 309 | + -Type "CustomScriptExtension" ` |
| 310 | + -TypeHandlerVersion 1.8 ` |
| 311 | + -Setting $publicSettings |
| 312 | +Update-AzureRmVmss ` |
| 313 | + -ResourceGroupName myResourceGroupAG ` |
| 314 | + -Name myvmss ` |
| 315 | + -VirtualMachineScaleSet $vmss |
| 316 | +``` |
| 317 | + |
| 318 | +## Test the application gateway |
| 319 | + |
| 320 | +You can use [Get-AzureRmPublicIPAddress](/powershell/module/azurerm.network/get-azurermpublicipaddress) to get the public IP address of the application gateway. Copy the public IP address, and then paste it into the address bar of your browser. For example, http://52.170.203.149 |
| 321 | + |
| 322 | +```powershell |
| 323 | +Get-AzureRmPublicIPAddress -ResourceGroupName myResourceGroupAG -Name myAGPublicIPAddress |
| 324 | +``` |
| 325 | + |
| 326 | + |
| 327 | + |
| 328 | +To accept the security warning if you used a self-signed certificate, select **Details** and then **Go on to the webpage**. Your secured IIS website is then displayed as in the following example: |
| 329 | + |
| 330 | + |
| 331 | + |
| 332 | +## Next steps |
| 333 | + |
| 334 | +In this tutorial, you learned how to: |
| 335 | + |
| 336 | +> [!div class="checklist"] |
| 337 | +> * Create a self-signed certificate |
| 338 | +> * Set up a network |
| 339 | +> * Create an application gateway with the certificate |
| 340 | +> * Add a listener and redirection rule |
| 341 | +> * Create a virtual machine scale set with the default backend pool |
0 commit comments