Azure CDN – Standard Microsoft: Rules Engine for http to https redirection
Published Aug 09 2019 02:43 AM 10.2K Views
Microsoft

Use Case:

While we have rules engine features for premium Verizon CDN endpoint, recently we have introduced rules engine feature for Microsoft CDN. While I am writing this blog, it’s in preview.

This blog explains how you can utilize this Rules engine feature in Microsoft Standard Azure CDN to redirect your request URL from http to https using PowerShell.

 

Update: This feature is now available to enable on the Azure Portal as well, Please find the following guidelines to do the same from Azure Portal: Enforce https using Azure CDN Standard Rules Engine

 

Pre-requisites:

I assume that you already have an Azure CDN created with Microsoft Standard profile. If you don’t have one created already, follow this.

Ensure you have the Az.Cdn module installed on your machine. To check if the Az.CDN is installed or not, you can do the following,

 

  • Open PowerShell as Administrator

 

  • Run command Import-Module Az.Cdn

 

  • If the above command doesn’t throw any error, you have the Az.Cdn PowerShell module

installed and the module is successfully imported in the current session.

 

The following error while running Import-Module command means that you don’t have the Az.Cdn module installed yet. To install you have to run Install-Module -Name Az.Cdn -force command.

first.jpg

 

If you see the below error while running the install command then you need to run Install-Module -Name Az.Cdn -AllowClobber -Scope CurrentUser command to install it.

2.jpg

 

  • Now to check if the Az.Cdn module is installed successfully, you can try importing the module using the command: Import-Module Az.Cdn

 

You may be prompted to confirm so that it can import this module for the first time.

3.jpg

 
  • You can also run Get-Module command to confirm if it’s imported successfully in your PowerShell session and what version it’s currently using:

 

PS command: Get-Module Az.Cdn

4.jpg

 

Make sure you use the latest version of Az.Cdn i.e. 1.3.0 so that you can use the new cmdlet which are released for setting the rules engine for a CDN endpoint.

 

Steps:

Ensure that the above pre-requisites are followed before proceeding with the following steps.

 

Launch a new PowerShell session and run the following cmdlets:

 

#To connect to Azure account

Connect-AzAccount

 

#set the rule condition when the action will be performed | Updated the below command with latest details

$RuleCondition = New-AzCdnDeliveryRuleCondition -MatchVariable 'RequestScheme' -Operator Equal -MatchValue "HTTP"

 

#Set the action what it should to once the condition is met, here we are doing http to https redirection

$RuleAction = New-AzCdnDeliveryRuleAction -RedirectType Moved -DestinationProtocol Https

 

#Set the Rule with condition and action we just created above

$Rule = New-AzCdnDeliveryRule -Name "rule1" -Order 1 -Condition $RuleCondition -Action $RuleAction

 

#Set the Azuer CDN delivery policy with the rule

$policy = New-AzCdnDeliveryPolicy -Description "RedirectPolicy" -Rule $Rule

 

#get the CDN endpoint reference

#please replace the below parameters as per the CDN endpoint details

$ep = Get-AzCdnEndpoint -ProfileName "<CDN Profile Name>" -EndpointName "<CDN Endpoint Name>" -ResourceGroupName "<Resource Group Name>"

 

#Assign the delivery policy to the CDN endpoint variable

$ep.DeliveryPolicy = $policy

 

#Now call the set CDN endpoint to save the changes on the CDN endpoint

Set-AzCdnEndpoint -CdnEndpoint $ep

 

Hope this helps.

 

Updating the reference articles for more details on CDN standard rules engine feature:

1. https://docs.microsoft.com/en-us/azure/cdn/cdn-standard-rules-engine-reference 

2. https://docs.microsoft.com/en-us/azure/cdn/cdn-standard-rules-engine-match-conditions 

3. https://docs.microsoft.com/en-us/azure/cdn/cdn-standard-rules-engine-actions 

20 Comments
Version history
Last update:
‎Sep 15 2020 01:41 AM
Updated by: