Tutorial: URL path-based redirection using CLI - Azure Application Gateway (2024)

  • 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:

Tutorial: URL path-based redirection using CLI - Azure Application Gateway (1)

If you prefer, you can complete this tutorial using Azure PowerShell.

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

Prerequisites

  • Use the Bash environment in Azure Cloud Shell. For more information, see Quickstart for Bash in Azure Cloud Shell.

    Tutorial: URL path-based redirection using CLI - Azure Application Gateway (2)

  • 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.

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.

az network vnet create \ --name myVNet \ --resource-group myResourceGroupAG \ --location eastus \ --address-prefix 10.0.0.0/16 \ --subnet-name myAGSubnet \ --subnet-prefix 10.0.1.0/24az network vnet subnet create \ --name myBackendSubnet \ --resource-group myResourceGroupAG \ --vnet-name myVNet \ --address-prefix 10.0.2.0/24az network public-ip create \ --resource-group myResourceGroupAG \ --name myAGPublicIPAddress \ --allocation-method Static \ --sku Standard

Create an application gateway

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.

az network application-gateway create \ --name myAppGateway \ --location eastus \ --resource-group myResourceGroupAG \ --vnet-name myVNet \ --subnet myAGsubnet \ --capacity 2 \ --sku Standard_v2 \ --http-settings-cookie-based-affinity Disabled \ --frontend-port 80 \ --http-settings-port 80 \ --http-settings-protocol Http \ --public-ip-address myAGPublicIPAddress \ --priority 100

It may take several minutes for the application gateway to be created. After the application gateway is created, you can see these new features:

  • appGatewayBackendPool - An application gateway must have at least one backend address pool.
  • appGatewayBackendHttpSettings - Specifies that port 80 and an HTTP protocol is used for communication.
  • appGatewayHttpListener - The default listener associated with appGatewayBackendPool.
  • appGatewayFrontendIP - Assigns myAGPublicIPAddress to appGatewayHttpListener.
  • rule1 - The default routing rule that is associated with appGatewayHttpListener.

Add backend pools and ports

You can add backend address pools named imagesBackendPool and videoBackendPool to your application gateway by using az network application-gateway address-pool create. You add the frontend ports for the pools using az network application-gateway frontend-port create.

az network application-gateway address-pool create \ --gateway-name myAppGateway \ --resource-group myResourceGroupAG \ --name imagesBackendPoolaz network application-gateway address-pool create \ --gateway-name myAppGateway \ --resource-group myResourceGroupAG \ --name videoBackendPoolaz network application-gateway frontend-port create \ --port 8080 \ --gateway-name myAppGateway \ --resource-group myResourceGroupAG \ --name bportaz network application-gateway frontend-port create \ --port 8081 \ --gateway-name myAppGateway \ --resource-group myResourceGroupAG \ --name rport

Add listeners and rules

Add listeners

Add the backend listeners named backendListener and redirectedListener that are needed to route traffic using az network application-gateway http-listener create.

az network application-gateway http-listener create \ --name backendListener \ --frontend-ip appGatewayFrontendIP \ --frontend-port bport \ --resource-group myResourceGroupAG \ --gateway-name myAppGatewayaz network application-gateway http-listener create \ --name redirectedListener \ --frontend-ip appGatewayFrontendIP \ --frontend-port rport \ --resource-group myResourceGroupAG \ --gateway-name myAppGateway

Add the default URL path map

URL path maps make sure specific URLs are routed to specific backend pools. You can create URL path maps named imagePathRule and videoPathRule using az network application-gateway url-path-map create and az network application-gateway url-path-map rule create

az network application-gateway url-path-map create \ --gateway-name myAppGateway \ --name urlpathmap \ --paths /images/* \ --resource-group myResourceGroupAG \ --address-pool imagesBackendPool \ --default-address-pool appGatewayBackendPool \ --default-http-settings appGatewayBackendHttpSettings \ --http-settings appGatewayBackendHttpSettings \ --rule-name imagePathRuleaz network application-gateway url-path-map rule create \ --gateway-name myAppGateway \ --name videoPathRule \ --resource-group myResourceGroupAG \ --path-map-name urlpathmap \ --paths /video/* \ --address-pool videoBackendPool

Add redirection configuration

You can configure redirection for the listener using az network application-gateway redirect-config create.

az network application-gateway redirect-config create \ --gateway-name myAppGateway \ --name redirectConfig \ --resource-group myResourceGroupAG \ --type Found \ --include-path true \ --include-query-string true \ --target-listener backendListener

Add the redirection URL path map

az network application-gateway url-path-map create \ --gateway-name myAppGateway \ --name redirectpathmap \ --paths /images/* \ --resource-group myResourceGroupAG \ --redirect-config redirectConfig \ --rule-name redirectPathRule

Add routing rules

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.

az network application-gateway rule create \ --gateway-name myAppGateway \ --name defaultRule \ --resource-group myResourceGroupAG \ --http-listener backendListener \ --rule-type PathBasedRouting \ --url-path-map urlpathmap \ --address-pool appGatewayBackendPool \ --priority 100az network application-gateway rule create \ --gateway-name myAppGateway \ --name redirectedRule \ --resource-group myResourceGroupAG \ --http-listener redirectedListener \ --rule-type PathBasedRouting \ --url-path-map redirectpathmap \ --address-pool appGatewayBackendPool \ --priority 100

Create virtual machine scale sets

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.

for i in `seq 1 3`; do if [ $i -eq 1 ] then poolName="appGatewayBackendPool" fi if [ $i -eq 2 ] then poolName="imagesBackendPool" fi if [ $i -eq 3 ] then poolName="videoBackendPool" fi az vmss create \ --name myvmss$i \ --resource-group myResourceGroupAG \ --image Ubuntu2204 \ --admin-username <azure-user> \ --admin-password <password> \ --instance-count 2 \ --vnet-name myVNet \ --subnet myBackendSubnet \ --vm-sku Standard_DS2 \ --upgrade-policy-mode Automatic \ --app-gateway myAppGateway \ --backend-pool-name $poolNamedone

Install NGINX

for i in `seq 1 3`; do az vmss extension set \ --publisher Microsoft.Azure.Extensions \ --version 2.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.

az network public-ip show \ --resource-group myResourceGroupAG \ --name myAGPublicIPAddress \ --query [ipAddress] \ --output tsv

Tutorial: URL path-based redirection using CLI - Azure Application Gateway (3)

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:

Tutorial: URL path-based redirection using CLI - Azure Application Gateway (4)

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:

Tutorial: URL path-based redirection using CLI - Azure Application Gateway (5)

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.

az group delete --name myResourceGroupAG

Next steps

Learn more about what you can do with application gateway

I'm an expert in Azure infrastructure and deployment, particularly skilled in using Azure CLI and PowerShell for resource provisioning and management. My depth of knowledge comes from hands-on experience and staying abreast of the latest Azure developments and best practices.

The article you provided focuses on leveraging Azure CLI commands to configure an Application Gateway in Azure, employing URL path-based routing rules. It covers several key concepts:

  1. Azure CLI and Azure PowerShell:

    • The article primarily uses Azure CLI commands for configuring the Application Gateway, but it also mentions Azure PowerShell as an alternative.
  2. Application Gateway and URL Path-Based Routing:

    • It demonstrates setting up an Application Gateway to manage web traffic and route it to appropriate backend pools based on URL paths.
  3. Network Setup:

    • It covers creating necessary network resources like Virtual Networks, Subnets, and Public IP addresses using Azure CLI commands (az network commands).
  4. Application Gateway Creation:

    • Details on creating an Application Gateway (az network application-gateway create) specifying various configurations like capacity, SKU, HTTP settings, frontend port, etc.
  5. Backend Pools and Listeners:

    • Adding backend address pools, frontend ports, and creating listeners for routing traffic using commands like az network application-gateway address-pool create, az network application-gateway frontend-port create, and az network application-gateway http-listener create.
  6. URL Path Maps and Redirection:

    • Creating URL path maps (az network application-gateway url-path-map create) to direct specific URLs to designated backend pools and configuring redirection for the listener.
  7. Routing Rules and Virtual Machine Scale Sets (VMSS):

    • Configuring routing rules (az network application-gateway rule create) linking URL path maps to listeners and creating VMSS (az vmss create) with NGINX installed for backend pools.
  8. Testing and Cleanup:

    • Testing the setup by accessing the application through specific URLs and providing instructions for cleaning up the created resources (az group delete) once they're no longer needed.

This tutorial provides a comprehensive walkthrough using Azure CLI commands, allowing users to understand and implement URL path-based routing rules efficiently within an Application Gateway, along with related networking and resource management tasks.

Let me know if you need a detailed explanation or help with any specific part of this process!

Tutorial: URL path-based redirection using CLI - Azure Application Gateway (2024)
Top Articles
Latest Posts
Article information

Author: Rueben Jacobs

Last Updated:

Views: 5939

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Rueben Jacobs

Birthday: 1999-03-14

Address: 951 Caterina Walk, Schambergerside, CA 67667-0896

Phone: +6881806848632

Job: Internal Education Planner

Hobby: Candle making, Cabaret, Poi, Gambling, Rock climbing, Wood carving, Computer programming

Introduction: My name is Rueben Jacobs, I am a cooperative, beautiful, kind, comfortable, glamorous, open, magnificent person who loves writing and wants to share my knowledge and understanding with you.