Caching Rules In Azure CDN


In my last article, I discussed how to create an Azure CDN using the Azure Storage Service. Now, in this article, we will see how to play with caching rules in the Azure CDN.

Caching Rules in Azure CDN

By default, Azure CDN TTL (Time to Live) is applied for 7 days. If the TTL for the file hasn’t expired, the edge server returns the file from the cache, which will be faster.

We can set two types of rule for the Azure CDN.

  1. Global Rule
  2. Custom Rule

 1. Global Rule

Let’s set a global rule for the CDN which was created in my last article.

Step 1

Go to the Azure portal from CDN profile, select the End Point for the blob.

Step 2

From the setting, select Caching rules.

Figure 1 

Step 3

The Caching rules window will appear. Right now, we don’t have any caching rules applied to the endpoint. You can see that, by default, it holds 7 days of TTL.

Figure 2 

Step 4

Now, we need to set a global rule. Under the global caching rule section, select the Caching behavior as Override; make Cache expiration duration as 10 days, since our end point doesn’t contain any query string, let it ignore the query string for Query string caching behavior. Finally, select Save.

Figure 3 

It can take up to 90 minutes for the settings to reach every CDN POP (point of presence).

Custom Rule 

Custom rule is very handy when we are dealing with multiple endpoints.

In the caching rule setting page, go to custom caching rules and choose the match condition as a path. The matched value should be the path /[container_name]/[blob_name], the caching rule should be overridden as we did in the Global rule. You can set the time based on your requirement. In my case, I set it as 10 days.

Figure 4 

Figure 5
Finally, save.

It can take up to 90 minutes for the settings to reach every CDN POP (point of presence). I'm running the application using above CDN in the browser.
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <title>Untitled</title>  
  7.     <link rel="stylesheet" href="">  
  8.     <link rel="stylesheet" href="">  
  9.     <link rel="stylesheet" href="">  
  10.     <link rel="stylesheet" href="">  
  12.     <script src=""></script>  
  13.     <script src=""></script>  
  14.     <script src=""></script>  
  15.     <script src=""></script>  
  16.     <!--<script src="http://localhost:11207/JS/autocomplete.js"></script>-->  
  17.     <script src=""></script> <!-- Azure CDN -->  
  19. </head>  
  21. <body>    
  22.     <input id="autocomplete" />  
  23. </body>  
  24. </html>  
Max- age of CDN 

From the above figure, you can notice that the cache-control - max-age = 864000, which means 10 days.     
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.