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
Note
We recommend that you use the Azure Az PowerShell module to interact with Azure. See Install Azure PowerShell to get started. To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.
If you don't have an Azure subscription, create a Trial before you begin.
Prerequisites
- An Azure subscription
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.
Sign in to the Azure portal.
On the Azure portal, select Create a resource.
Select Windows Server 2016 Datacenter in the Popular list.
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: China North 2
- Username: Type a user name
- Password: Type a password
Select Next:Disks.
Select Next:Networking
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.
Select OK.
Ensure that under Subnet, myBackendSubnet is selected for the subnet, and then select Next: Management.
Select Disable to disable boot diagnostics.
Select Review + Create, review the settings on the summary page, and then select Create.
Create two more virtual machines, myVM2 and myVM3 and place them in the MyVNet virtual network and the myBackendSubnet subnet.
Install IIS
Login the Azure portal in powershell with command below:
Connect-AzAccount -Environment AzureChinaCloud
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 chinanorth2 ` -ExtensionName IIS ` -VMName myVM1 ` -Publisher Microsoft.Compute ` -ExtensionType CustomScriptExtension ` -TypeHandlerVersion 1.4 ` -Settings $publicSettings
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
Select Create a resource on the left menu of the Azure portal.
Select Networking and then select Application Gateway in the Featured list.
Basics tab
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 China North 2.
Under Configure virtual network, select myVNet for the name of the virtual network.
Select myAGSubnet for the subnet.
Accept the default values for the other settings and then select Next: Frontends.
Frontends tab
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.
Select Add new for the Public IP address and enter myAGPublicIPAddress for the public IP address name, and then select OK.
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 backends like Azure App Service.
On the Backends tab, select Add a backend pool.
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.
Under Target type, select Virtual machine from the drop-down list.
Under Target select the network interface for myVM1.
Select Add.
Repeat to add an Images backend pool with myVM2 as the target, and a Video backend pool with myVM3 as the target.
Select Add to save the backend pool configuration and return to the Backends tab.
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.
Select Add a routing rule in the Routing rules column.
In the Add a routing rule window that opens, enter myRoutingRule for the Rule name.
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.
On the Backend targets tab, select myBackendPool for the Backend target.
For the HTTP setting, select Add new to create a new HTTP setting. The HTTP setting will determine the behavior of the routing rule.
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.
Under Path-based routing, select Add multiple targets to create a path-based rule.
For Path, type /images/*.
For Target name, type Images.
For HTTP setting, select myHTTPSetting
For Backend target, select Images.
Select Add to save the path rule and return to the Add a routing rule tab.
Repeat to add another rule for Video.
Select Add to add the routing rule and return to the Configuration tab.
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
Select All resources, and then select myAppGateway.
Copy the public IP address, and then paste it into the address bar of your browser. Such as, http://203.0.113.10:8080.
The listener on port 8080 routes this request to the default backend pool.
Change the URL to http://<ip-address>:8080/images/test.htm, replacing <ip-address> with the public IP address of myAppGateway, and you should see something like the following example:
The listener on port 8080 routes this request to the Images backend pool.
Change the URL to http://<ip-address>:8080/video/test.htm, replacing <ip-address> with the public IP address of myAppGateway, and you should see something like the following example:
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
In this tutorial, you created an application gateway with a path-based routing rule.
To learn more about path-based routing in Application Gateways, see URL path-based routing overview
To learn how to create and configure an Application Gateway to redirect web traffic using the Azure CLI, advance to the next tutorial.