Modify/truncate path in path based routing with Azure application gateway - azure

let's say the Application gateway receives a request http://contoso.com/images and is configured to forward that to backendserver.contoso.com. Using a path based routing rule (/images/* for example).
I want the application gateway not to retain the /images path in the request URL. I need this to be truncated, in which case the request will simply be http://contoso.com/. Any path after the /images will remain intact though.
Is it possible? Any help/clue is greatly appreciated.

Yes you could do this today with PowerShell/CLI. In the backend http setting associated with the pool, please specify a -Path parameter. For example -
Add-AzureRmApplicationGatewayBackendHttpSettings -Path "/" -Name setting1 -Port 80 -Protocol Http -CookieBasedAffinity Disabled
When -Path is not specified at all, incoming request is routed to backend as is - which is the default behavior. When specified as "/" it removes the matching path from URI. If specified as a non-null value, then the specified value is used in place of matched path.

Amsrivas and Stephens answer is correct, but the poweshell CLI seems to have changed, at least # version 5.5.0 and now you have to perfrom this opperation using the following commands:
# Get gateway object
$AppGw = Get-AzureRmApplicationGateway -Name "YOUR GATEWAY NAME" -ResourceGroupName "YOUR GATEWAY RESOURCE GROUP"
# Show current settings
Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $AppGw
# Set path on local object (other values are whatever you want)
Set-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $AppGw -Name "YOUR SETTING NAME" -Port "80" -Protocol "Http" -CookieBasedAffinity "Disabled" -RequestTimeout 30 -Path "/"
# Commit changes back to Azure
$UpdatedAppGw = Set-AzureRmApplicationGateway -ApplicationGateway $AppGw
# Show new settings as applied in Azure
Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $UpdatedAppGw

Related

Azure CLI - Set Azure Application Gateway Backend settings

I have created a Powershell script that calls the Az module..."az network application-gateway probe create" - https://learn.microsoft.com/en-us/cli/azure/network/application-gateway/probe?view=azure-cli-latest#az-network-application-gateway-probe-create
I have read the documentation on the above command but can't work out how to set 'backend settings' - the field marked below by the black line as shown below - any thoughts?
The 'Backend Settings' field is a drop-down box that does list the 'http settings' I want to set the value to - remember I want to do this via; ARM or CLI not manually via the Azure Portal..
I tried to reproduce the same in my environment and got the results successfully like below:
To create the Probe and associated Backend settings, I used the below command while creating probe via CLI:
$probe = New-AzApplicationGatewayProbeConfig -Name probe01 -Protocol Http -HostName 'XXX.com' -Path '/path/path.htm' -Interval 30 -Timeout 120 -UnhealthyThreshold 8
$poolSetting = New-AzApplicationGatewayBackendHttpSettings -Name poolsetting01 -Port 80 -Protocol Http -CookieBasedAffinity Disabled -Probe $probe -RequestTimeout 80
I created the application gateway with all the required parameters like below:
$appgw = New-AzApplicationGateway -Name appgwtest -ResourceGroupName appgw-rg -Location 'West US' -BackendAddressPools $pool -Probes $probe -BackendHttpSettingsCollection $poolSetting -FrontendIpConfigurations $fipconfig -GatewayIpConfigurations $gipconfig -FrontendPorts $fp -HttpListeners $listener -RequestRoutingRules $rule -Sku $sku
In the Portal, Application Gateway created successfully with Backend settings like below:
To add a new probe to an existing application gateway and set Backend Settings, please use the below commands:
$appgw = Get-AzApplicationGateway -Name applicationgatewayname -ResourceGroupName ResourceGroupName
$probe = Add-AzApplicationGatewayProbeConfig -ApplicationGateway $appgw -Name probetest -Protocol Http -HostName 'XXX.com' -Path '/path/custompath.htm' -Interval 30 -Timeout 120 -UnhealthyThreshold 8
$appgw = Set-AzApplicationGatewayBackendHttpSettings -ApplicationGateway $appgw -Name $appgw.BackendHttpSettingsCollection.name -Port 80 -Protocol Http -CookieBasedAffinity Disabled -Probe $probe -RequestTimeout 120
Set-AzApplicationGateway -ApplicationGateway $appgw

How do i create Azure application gateway using Powershell -Multiple BackendPool

https://learn.microsoft.com/en-us/azure/application-gateway/tutorial-ssl-powershell
Hi All,
Using above link I'm able to create an application gateway with one Backend pool, with one Http settings, with one listener and with one rule.
But when it comes to Multiple Backend Pool, multiple Http settings, multiple listeners, multiple rules?
How do I define multiple Backend Pools, http settings, listeners and rules while creating Azure application gateway using PowerShell?
I have tested in my environment.
You can define multiple Backend Pools, http settings, listeners and rules while creating Azure application gateway using PowerShell
For defining multiple front end ports, use below command :
$frontendport1 = New-AzApplicationGatewayFrontendPort -Name FrontendPort1 -Port portnumber
$frontendport2 = New-AzApplicationGatewayFrontendPort -Name FrontendPort2 -Port portnumber
For defining multiple backend pools, use below command :
$backendPool1 = New-AzApplicationGatewayBackendAddressPool -Name AGBackendPool1
$backendPool2 = New-AzApplicationGatewayBackendAddressPool -Name AGBackendPool2
For defining multiple poolsettings, use below command :
$poolSettings1 = New-AzApplicationGatewayBackendHttpSetting -Name myPoolSettings1 -Port portnumber -Protocol Http -CookieBasedAffinity Enabled -RequestTimeout 30
$poolSettings2 = New-AzApplicationGatewayBackendHttpSetting -Name myPoolSettings2 -Port portnumber -Protocol Http -CookieBasedAffinity Enabled -RequestTimeout 30
For defining multiple listeners, use below command :
$defaultlistener1 = New-AzApplicationGatewayHttpListener -Name AGListener1 -Protocol Http -FrontendIPConfiguration $fipconfig -FrontendPort $frontendport1
$defaultlistener2 = New-AzApplicationGatewayHttpListener -Name AGListener2 -Protocol Http -FrontendIPConfiguration $fipconfig -FrontendPort $frontendport2
For defining multiple rules, use below command :
$frontendRule1 = New-AzApplicationGatewayRequestRoutingRule -Name rule1 -RuleType Basic -HttpListener $defaultlistener1 -BackendAddressPool $backendPool1 -BackendHttpSettings $poolSettings1
$frontendRule2 = New-AzApplicationGatewayRequestRoutingRule -Name rule2 -RuleType Basic -HttpListener $defaultlistener2 -BackendAddressPool $backendPool2 -BackendHttpSettings $poolSettings2
Now you can define this multiple Backend Pools, http settings, listeners and rules while creating Azure application gateway.
You can use below command to create Azure Application Gateway :
New-AzApplicationGateway -Name AppGatewayName -ResourceGroupName RGName -Location westus2 -BackendAddressPools $backendPool1, $backendPool2 -BackendHttpSettingsCollection $poolSettings1, $poolSettings2 -FrontendIpConfigurations $fipconfig -GatewayIpConfigurations $gipconfig -FrontendPorts $frontendport1, $frontendport2 -HttpListeners $defaultlistener1, $defaultlistener2 -RequestRoutingRules $frontendRule1, $frontendRule2 -Sku $sku

Azure - Add http listener to existing ApplicationGateway through ps command

I am trying to add a new listener to existing Azure application gateway using powershell script. Here's the command I have used.
Add-AzApplicationGatewayHttpListener -ApplicationGateway $Appgw -Name $listenerName -FrontendIPConfiguration $fipconfig -FrontendPort $port -SslCertificate $cert -HostName $hostName -Protocol "Https" -Debug
All the variables are correctly initialized. Command executes well and return ApplicationGateway object which seems normal. However the newly added listener is not visible in Azure portal. After watching carefully the new listener in ApplicationGateway object (in powershell) it just show below resource path,
/subscriptions/<subscription-id>/resourceGroups/ResourceGroupNotSet/providers/Microsoft.Network/applicationGateways/ApplicationGatewayNameNotSet/httpListeners/<new-listener-name>
two things doesn't seem normal here,
ResourceGroupNotSet
ApplicationGatewayNameNotSet
Can anyone please suggest what could be the reason behind this? and why the listener is not shown in portal at all?
According to my test, if we want to add HTTP listener to existing Azure Application gateway with Powershell, we need to run the command Set-AzApplicationGateway -ApplicationGateway $appgw after you run the command Add-AzApplicationGatewayHttpListener. Because the command Add-AzApplicationGatewayHttpListener just will create a new HTTP listener but it will not update the application gateway.
Connect-AzAccount
$AppGWname ="stantest"
$groupName="stan"
$ipName="appGwPublicFrontendIp"
$portName="port_80"
$listenerName="test1"
$appgw= Get-AzApplicationGateway -Name $AppGWname -ResourceGroupName $groupName
$FEC= Get-AzApplicationGatewayFrontendIPConfig -Name $ipName -ApplicationGateway $appgw
Add-AzApplicationGatewayFrontendPort -ApplicationGateway $appgw -Name $portName -Port 80
$port =Get-AzApplicationGatewayFrontendPort -ApplicationGateway $appgw -Name $portName
Add-AzApplicationGatewayHttpListener -ApplicationGateway $appgw -Name $listenerName -FrontendIPConfiguration $FEC -FrontendPort $port -Protocol Http
Set-AzApplicationGateway -ApplicationGateway $appgw

How to configure OpenPorts option for New-AzureRmVm to create VM without default network security group RDP port(3389,5985).

I'd like to create Windows Server VM which has source filtered RDP port using Powershell.
New-AzureRmVM and Add-AzureRmNetworkSecurityRuleConfig partially work for me.
New-AzureRmVM create VM with nsg rule for default RDP port which allow any source. I have to delete them after the script run.
I tried to set -OpenPorts option to $null or None.
Is this possible? Or, any other method to achieve this?
Unfortunately, it seems you cannot delete the RDP NGS rule through setting the -OpenPorts to $null or None when you create the VM using PowerShell command New-AzureRmVM.
-OpenPorts
A list of ports to open on the network security group (NSG) for the created VM. The default value depends on the type of image
chosen (i.e., Windows: 3389, 5985 and Linux: 22).
When you create the windows VM, the default port is opened according to the image type. But you can change the NSG rule to filter the traffic when you creating.
# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzureRmNetworkSecurityRuleConfig -Name myNetworkSecurityGroupRuleRDP -Protocol Tcp `
-Direction Inbound -Priority 1000 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix * `
-DestinationPortRange 3389 -Access Allow
# Create a network security group
$nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $resourceGroup -Location $location `
-Name myNetworkSecurityGroup -SecurityRules $nsgRuleRDP
Set the -SourceAddressPrefix, -SourcePortRange, -DestinationAddressPrefix and the -Access as you want to filter the traffic. For more details, see Create a fully configured virtual machine with PowerShell.
You can change Azure VM default RDP port. For more details, please refer to the blog.
Write-host "What Port would you like to set for RDP: " -ForegroundColor Yellow -NoNewline;$RDPPort = Read-Host
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-TCP\" -Name PortNumber -Value $RDPPort
New-NetFirewallRule -DisplayName "RDP HighPort" -Direction Inbound –LocalPort $RDPPort -Protocol TCP -Action Allow

How do I use Set- commands with AzureRM and Application Gateway?

I am trying to run the following commands to change the settings on an existing Rule in my Azure Application Gateway:
$updatedAppGW = Set-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $AppGW `
-Name $ChosenSubscription.httpsRule `
-RuleType Basic `
-BackendAddressPool $backendPool `
-BackendHttpSettings $httpSettings
# Save Gateway configuration
Write-Host "[$(__LINE__)] Attempting to save changes to the Application Gateway..." -ForegroundColor Cyan
Set-AzureRmApplicationGateway -ApplicationGateway $updatedAppGW | Out-Null
The Set-AzureRmApplicationGatewayRequestRoutingRule command appears to run correctly (silently, at least).
However, when I then try to "save" the Application Gateway configuration with the command Set-AzureRmApplicationGateway I receive the error Set-AzureRmApplicationGateway : Object reference not set to an instance of an object.
I think this is because I'm not using these "Set" commands correctly.
I read online that when I run Set-AzureRmApplicationGatewayRequestRoutingRule, I'm only actually making changes to the Rule in local memory. I then have to save the changes of the Application Gateway.
Is this true? If so... how do I actually save the Application Gateway configuration in this context? Earlier in my script, when using Add-AzureRm commands (such as Add-AzureRmApplicationGatewayBackendAddressPool), I've immediately (following line) run Set-AzureRmApplicationGateway and it has worked as intended.
I have also tried altering the Set-AzureRmApplicationGateway command in the code block at the top of this post to use my original $AppGW variable instead of this $updatedAppGW variable which I think my Set-AzureRmApplicationGatewayRequestRoutingRule command is producing. Neither works - identical error.
EDIT: Additional diagnosis
Adding the following Write-Host output...
Write-Host "[$(__LINE__)] Retrieved AG Rule '$($rule.Name)'." -ForegroundColor Magenta
Write-Host "[$(__LINE__)] Attempting to change this rule to point at Backend Address Pool '$($backendPool.Name)' and HTTP Settings '$($httpSettings.Name)'..." -ForegroundColor Cyan
# Re-retrieve the Application Gateway after saving it earlier
$AppGW = Get-AzureRmApplicationGateway -Name $ChosenSubscription.appGateway -ResourceGroupName $ChosenSubscription.resourceGroup
# Re-retrieve the Backend Address Pool and HTTP Settings that we've created, for the sake of updating the rule
$backendPool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $AppGW -Name $MaintenanceToggleBackendPool
$httpSettings = Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $AppGW -Name $MaintenanceToggleHTTPSetting
Write-Host "[$(__LINE__)] `$AppGW.Name $($AppGW.Name)" -ForegroundColor Green
Write-Host "[$(__LINE__)] `$AppGW.ProvisioningState $($AppGW.ProvisioningState)" -ForegroundColor Green
Write-Host "[$(__LINE__)] `$AppGW.OperationalState $($AppGW.OperationalState)" -ForegroundColor Green
$updatedAppGW = Set-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $AppGW `
-Name $ChosenSubscription.httpsRule `
-RuleType Basic `
-BackendAddressPool $backendPool `
-BackendHttpSettings $httpSettings
Write-Host "[$(__LINE__)] `$updatedAppGW.Name $($updatedAppGW.Name)" -ForegroundColor Green
Write-Host "[$(__LINE__)] `$updatedAppGW.ProvisioningState $($updatedAppGW.ProvisioningState)" -ForegroundColor Green
Write-Host "[$(__LINE__)] `$updatedAppGW.OperationalState $($updatedAppGW.OperationalState)" -ForegroundColor Green
# Save Gateway configuration
Write-Host "[$(__LINE__)] Attempting to save changes to the Application Gateway..." -ForegroundColor Cyan
Set-AzureRmApplicationGateway -ApplicationGateway $updatedAppGW | Out-Null
... gives the following console output:
OK, managed to fix the problem myself... sigh
On the Set-AzureRmApplicationGatewayRequestRoutingRule command, you must specify the -HttpListener parameter, or it will fail silently.
# Re-retrieve the Backend Address Pool and HTTP Settings that we've created, for the sake of updating the rule
$backendPool = Get-AzureRmApplicationGatewayBackendAddressPool -ApplicationGateway $AppGW -Name $MaintenanceToggleBackendPool
$httpSettings = Get-AzureRmApplicationGatewayBackendHttpSettings -ApplicationGateway $AppGW -Name $MaintenanceToggleHTTPSetting
$httpListener = Get-AzureRmApplicationGatewayHttpListener -ApplicationGateway $AppGW -Name "HttpListenerTest"
$updatedAppGW = Set-AzureRmApplicationGatewayRequestRoutingRule -ApplicationGateway $AppGW `
-Name $ChosenSubscription.httpsRule `
-RuleType Basic `
-BackendAddressPool $backendPool `
-BackendHttpSettings $httpSettings `
-HttpListener $httpListener
This is why the Set-AzureRmApplicationGateway command wasn't working properly - it had a malformed RequestRoutingRule in its memory.

Resources