Tutorial: Create an application gateway with URL path-based redirection using the Azure CLI
Article
You can use the Azure CLI to configure URL path-based routing rules when you create an application gateway. In this tutorial, you create backend pools using virtual machine scale sets. You then create URL routing rules that make sure web traffic is redirected to the appropriate backend pool.
In this tutorial, you learn how to:
Set up the network
Create an application gateway
Add listeners and routing rules
Create virtual machine scale sets for backend pools
The following example shows site traffic coming from both ports 8080 and 8081 and being directed to the same backend pools:
If you prefer, you can complete this tutorial using Azure PowerShell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Sign in with the Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
This tutorial requires version 2.0.4 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
Create a resource group
A resource group is a logical container into which Azure resources are deployed and managed. Create a resource group using az group create.
The following example creates a resource group named myResourceGroupAG in the eastus location.
Azure CLI
az group create --name myResourceGroupAG --location eastus
Create network resources
Create the virtual network named myVNet and the subnet named myAGSubnet using az network vnet create. You can then add the subnet named myBackendSubnet that's needed by the backend servers using az network vnet subnet create. Create the public IP address named myAGPublicIPAddress using az network public-ip create.
Use az network application-gateway create to create the application gateway named myAppGateway. When you create an application gateway using the Azure CLI, you specify configuration information, such as capacity, sku, and HTTP settings. The application gateway is assigned to myAGSubnet and myPublicIPAddress that you previously created.
The routing rules associate the URL path maps with the listeners that you created. You can add the rules named defaultRule and redirectedRule using az network application-gateway rule create.
In this example, you create three virtual machine scale sets that support the three backend pools that you created. The scale sets that you create are named myvmss1, myvmss2, and myvmss3. Each scale set contains two virtual machine instances on which you install NGINX.
Replace <azure-user> and <password> with a user name and password of your choice.
Azure CLI
for i in `seq 13`; do
if [ $i-eq1]
then
poolName="appGatewayBackendPool"
fi
if [ $i-eq2]
then
poolName="imagesBackendPool"
fi
if [ $i-eq3]
then
poolName="videoBackendPool"
fi
az vmss create \
--name myvmss$i \
--resource-group myResourceGroupAG \
--image Ubuntu2204 \
--admin-username<azure-user> \
--admin-password<password> \
--instance-count2 \
--vnet-name myVNet \
--subnet myBackendSubnet \
--vm-sku Standard_DS2 \
--upgrade-policy-mode Automatic \
--app-gateway myAppGateway \
--backend-pool-name$poolName
done
Install NGINX
Azure CLI
for i in `seq 13`; do
az vmss extension set \
--publisher Microsoft.Azure.Extensions \
--version2.0 \
--name CustomScript \
--resource-group myResourceGroupAG \
--vmss-name myvmss$i \
--settings'{ "fileUris": ["https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/install_nginx.sh"], "commandToExecute": "./install_nginx.sh" }'
done
Test the application gateway
To get the public IP address of the application gateway, use az network public-ip show. Copy the public IP address, and then paste it into the address bar of your browser. Such as, http://40.121.222.19, http://40.121.222.19:8080/images/test.htm, http://40.121.222.19:8080/video/test.htm, or http://40.121.222.19:8081/images/test.htm.
Azure CLI
az network public-ip show \
--resource-group myResourceGroupAG \
--name myAGPublicIPAddress \
--query[ipAddress] \
--output tsv
Change the URL to http://<ip-address>:8080/images/test.html, replacing your IP address for <ip-address>, and you should see something like the following example:
Change the URL to http://<ip-address>:8080/video/test.html, replacing your IP address for <ip-address>, and you should see something like the following example:
Now, change the URL to http://<ip-address>:8081/images/test.htm, replacing your IP address for <ip-address>, and you should see traffic redirected back to the images backend pool at http://<ip-address>:8080/images.
Clean up resources
When no longer needed, remove the resource group, application gateway, and all related resources.
In this module, you'll learn to improve application resilience by distributing load across multiple servers and use path-based routing to direct web traffic.