Tutorial: Create an application gateway with path-based routing rules using the Azure portal

You can use the Azure portal to configure URL path-based routing rules when you create an application gateway. In this tutorial, you create backend pools using virtual machines. You then create routing rules that make sure web traffic arrives at the appropriate servers in the pools.

In this article, you learn how to:

  • Create an application gateway
  • Create virtual machines for backend servers
  • Create backend pools with the backend servers
  • Create a backend listener
  • Create a path-based routing rule

URL routing example

Note

This article uses the Azure Az PowerShell module, which is the recommended PowerShell module for interacting with Azure. To get started with the Az PowerShell module, see Install Azure PowerShell. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

Create virtual machines

In this example, you create three virtual machines to be used as backend servers for the application gateway. You also install IIS on the virtual machines to verify that the application gateway works as expected.

  1. Sign in to the Azure portal at https://portal.azure.com.

  2. On the Azure portal, select Create a resource.

  3. Select Windows Server 2016 Datacenter in the Popular list.

  4. Enter these values for the virtual machine:

    • Subscription - Select your subscription.
    • Resource group, select Create new, and then type myResourceGroupAG.
    • Virtual machine name: myVM1
    • Region: (US) East US
    • Username: Type a user name
    • Password: Type a password
  5. Select Next:Disks.

  6. Select Next:Networking

  7. For Virtual network, select Create new and then type these values for the virtual network:

    • myVNet - for the name of the virtual network.
    • 10.0.0.0/16 - for the virtual network address space.
    • myBackendSubnet for the first subnet name
    • 10.0.1.0/24 - for the subnet address space.
    • myAGSubnet - for the second subnet name.
    • 10.0.0.0/24 - for the subnet address space.
  8. Select OK.

  9. Ensure that under Subnet, myBackendSubnet is selected for the subnet, and then select Next: Management.

  10. Select Disable to disable boot diagnostics.

  11. Select Review + Create, review the settings on the summary page, and then select Create.

  12. Create two more virtual machines, myVM2 and myVM3 and place them in the MyVNet virtual network and the myBackendSubnet subnet.

Install IIS

  1. Open the interactive shell and make sure that it's set to PowerShell.

    Install custom extension

  2. Run the following command to install IIS on the virtual machine:

         $publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/appgatewayurl.ps1");  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" }
    
        Set-AzVMExtension `
         -ResourceGroupName myResourceGroupAG `
         -Location eastus `
         -ExtensionName IIS `
         -VMName myVM1 `
         -Publisher Microsoft.Compute `
         -ExtensionType CustomScriptExtension `
         -TypeHandlerVersion 1.4 `
         -Settings $publicSettings
    
  3. Install IIS on the other virtual machines using the steps that you just finished. Use myVM2 and myVM3 for VMName values in Set-AzVMExtension.

Create an application gateway

  1. Select Create a resource on the left menu of the Azure portal. The New window appears.

  2. Select Networking and then select Application Gateway in the Featured list.

Basics tab

  1. On the Basics tab, enter these values for the following application gateway settings:

    • Subscription: Select your subscription.

    • Resource group: Select myResourceGroupAG for the resource group.

    • Application gateway name: Type myAppGateway for the name of the application gateway.

    • Region - Select (US) East US.

      Create new application gateway: Basics

  2. Under Configure virtual network, select myVNet for the name of the virtual network.

  3. Select myAGSubnet for the subnet.

  4. Accept the default values for the other settings and then select Next: Frontends.

Frontends tab

  1. On the Frontends tab, verify Frontend IP address type is set to Public.

    Note

    For the Application Gateway v2 SKU, you can only choose Public frontend IP configuration. Private frontend IP configuration is currently not enabled for this v2 SKU.

  2. Select Add new for the Public IP address and enter myAGPublicIPAddress for the public IP address name, and then select OK.

  3. Select Next: Backends.

Backends tab

The backend pool is used to route requests to the backend servers that serve the request. Backend pools can be composed of NICs, virtual machine scale sets, public IPs, internal IPs, fully qualified domain names (FQDN), and multi-tenant back-ends like Azure App Service.

  1. On the Backends tab, select Add a backend pool.

  2. In the Add a backend pool window that opens, enter the following values to create an empty backend pool:

    • Name: Enter myBackendPool for the name of the backend pool.
  3. Under Target type, select Virtual machine from the drop-down list.

  4. Under Target select the network interface for myVM1.

  5. Select Add.

  6. Repeat to add an Images backend pool with myVM2 as the target, and a Video backend pool with myVM3 as the target.

  7. Select Add to save the backend pool configuration and return to the Backends tab.

  8. On the Backends tab, select Next: Configuration.

Configuration tab

On the Configuration tab, you'll connect the frontend and backend pool you created using a routing rule.

  1. Select Add a routing rule in the Routing rules column.

  2. In the Add a routing rule window that opens, enter myRoutingRule for the Rule name.

  3. A routing rule requires a listener. On the Listener tab within the Add a routing rule window, type the following values for the listener:

    • Listener name: Enter myListener for the name of the listener.

    • Frontend IP: Select Public to choose the public IP you created for the frontend.

    • Port: Type 8080

      Accept the default values for the other settings on the Listener tab, then select the Backend targets tab to configure the rest of the routing rule.

  4. On the Backend targets tab, select myBackendPool for the Backend target.

  5. For the HTTP setting, select Add new to create a new HTTP setting. The HTTP setting will determine the behavior of the routing rule.

  6. In the Add an HTTP setting window that opens, enter myHTTPSetting for the HTTP setting name. Accept the default values for the other settings in the Add an HTTP setting window, then select Add to return to the Add a routing rule window.

  7. Under Path-based routing, select Add multiple targets to create a path-based rule.

  8. For Path, type /images/*.

  9. For Target name, type Images.

  10. For HTTP setting, select myHTTPSetting

  11. For Backend target, select Images.

  12. Select Add to save the path rule and return to the Add a routing rule tab.

  13. Repeat to add another rule for Video.

  14. Select Add to add the routing rule and return to the Configuration tab.

  15. Select Next: Tags and then Next: Review + create.

Note

You do not need to add a custom /* path rule to handle default cases. This is automatically handled by the default backend pool.

Note

Wildcard delimiter * is only honored at the end of the rule. For more information and supported path based rules examples, see URL Path Based Routing overview.

Review + create tab

Review the settings on the Review + create tab, and then select Create to create the virtual network, the public IP address, and the application gateway. It may take several minutes for Azure to create the application gateway. Wait until the deployment finishes successfully before moving on to the next section.

Test the application gateway

  1. Select All resources, and then select myAppGateway.

    Record application gateway public IP address

  2. Copy the public IP address, and then paste it into the address bar of your browser. Such as, http://52.188.72.175:8080.

    Test base URL in application gateway

    The listener on port 8080 routes this request to the default backend pool.

  3. Change the URL to http://<ip-address>:8080/images/test.htm, replacing <ip-address> with your IP address, and you should see something like the following example:

    Test images URL in application gateway

    The listener on port 8080 routes this request to the Images backend pool.

  4. Change the URL to http://<ip-address>:8080/video/test.htm, replacing <ip-address> with your IP address, and you should see something like the following example:

    Test video URL in application gateway

    The listener on port 8080 routes this request to the Video backend pool.

Clean up resources

When no longer needed, delete the resource group and all related resources. To do so, select the resource group and select Delete resource group.

Next steps