How to map URL path-based rules in application gateway for your Azure web app service

If you want to redirect users’ requests to different Azure web app service behind Azure application Gateway via URL path-based rules ,you may need some extra settings , this doc will guide you how to map it .

Prerequisites

  1. Azure Subscription .
  2. Existing Azure web apps.
  3. New created Azure application gateway.

Scenario

In this demo ,I have two web apps : http://testweba1.azurewebsites.net/first/ (we call it app1)and http://testweba2.azurewebsites.net/second/ (we call it  app2)and I created a new App GW ip : 13.76.3.134 , a sub domain record: pathtest.devtest.tk assigned to this IP.

So I’ll demonstrate how to map app gw to implement the requesting route  below :

http://pathtest.devtest.tk/first/  => app1 (http://testweba1.azurewebsites.net/first/)

http://pathtest.devtest.tk/second/ => app2(http://testweba2.azurewebsites.net/second/)

Before we start to map this , we should know the route you mapped for your web apps should be the application root path of your web app . For instance , if you want to route

http://pathtest.devtest.tk/first/ => app1 

Your app1 application root path should be :http://testweba1.azurewebsites.net/first/ 

If your the application root path of your Azure web app is  http://testweba1.azurewebsites.net/  you can do quick config at your app service – > Application settings -> Virtual applications and directories:

Steps

The first step , we should create two separate backend pools and add web apps into it :

Next, we should create HttpSettings with custom health Prob for this two pools. For Azure web apps (app services ) there is no way to create proprietary HttpSettings with custom health Prob .So we should run PS script below to create them :

$rg = Get-AzureRmResourceGroup -Name "<resource group name>"

# Retrieve an existing application gateway
$gw = Get-AzureRmApplicationGateway -Name "<app gw name>"  -ResourceGroupName $rg.ResourceGroupName

$match=New-AzureRmApplicationGatewayProbeHealthResponseMatch -StatusCode 200-399

#create two custom health prob for two web apps,pls note, the value of -Path should be the application root path of your web app 
Add-AzureRmApplicationGatewayProbeConfig -name prob4App1 -ApplicationGateway $gw -Protocol Http -Path /first -Interval 30 -Timeout 120 -UnhealthyThreshold 3 -PickHostNameFromBackendHttpSettings -Match $match
Add-AzureRmApplicationGatewayProbeConfig -name prob4App2 -ApplicationGateway $gw -Protocol Http -Path /Second -Interval 30 -Timeout 120 -UnhealthyThreshold 3 -PickHostNameFromBackendHttpSettings -Match $match
$probe4App1 = Get-AzureRmApplicationGatewayProbeConfig -name prob4App1 -ApplicationGateway $gw
$probe4App2 = Get-AzureRmApplicationGatewayProbeConfig -name prob4App2 -ApplicationGateway $gw

#create httpSetting for backend pool1 where app 1 located with custom health prob for app1 
add-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $gw -name pool1HttpSetting -port 80 -protocol http -CookieBasedAffinity Enabled -RequestTimeout 30 
Set-AzureRmApplicationGatewayBackendHttpSettings -Name pool1HttpSetting -ApplicationGateway $gw -PickHostNameFromBackendAddress -Port 80 -Protocol http -CookieBasedAffinity Disabled -RequestTimeout 30 -Probe $probe4App1

#create httpSetting for backend pool2 where app 2 located with custom health prob for app2
add-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $gw -name pool2HttpSetting -port 80 -protocol http -CookieBasedAffinity Enabled -RequestTimeout 30 
Set-AzureRmApplicationGatewayBackendHttpSettings -Name pool2HttpSetting -ApplicationGateway $gw -PickHostNameFromBackendAddress -Port 80 -Protocol http -CookieBasedAffinity Disabled -RequestTimeout 30 -Probe $probe4App2

# Update the application gateway
Set-AzureRmApplicationGateway -ApplicationGateway $gw 

Let’s create a new basic listener with port 80 to listen requests :

Create and map a Path-based rule and bind to the listener we just created :

Save the change and test :

 

Summary

So this blog is about how to map URL path-based rules in application gateway for your Azure web app service.

If you have any questions, please contact us via aka.ms/devchat.

Leave a Reply

Your email address will not be published. Required fields are marked *