0
0
Azurecloud~5 mins

CDN caching rules in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
CDN caching rules help store copies of your website files closer to users to make loading faster. They decide how long content stays saved on servers before checking for updates.
When you want your website images to load quickly for visitors worldwide.
When you need to reduce traffic to your main server by serving cached content.
When you want to control how often your website updates appear to users.
When you want to improve user experience by speeding up page load times.
When you want to save bandwidth costs by serving cached files from the CDN.
Config File - cdn-caching-rules.json
cdn-caching-rules.json
{
  "name": "exampleCachingRule",
  "order": 1,
  "conditions": [
    {
      "name": "UrlFileExtension",
      "parameters": {
        "operator": "Equal",
        "matchValues": ["jpg", "png", "css", "js"],
        "negateCondition": false,
        "transforms": []
      }
    }
  ],
  "actions": [
    {
      "name": "CacheExpiration",
      "parameters": {
        "cacheBehavior": "Override",
        "cacheDuration": "1.00:00:00"
      }
    }
  ]
}

This JSON defines a caching rule named exampleCachingRule.

order sets the priority of the rule.

conditions specify which file types the rule applies to (images and stylesheets).

actions tell the CDN to cache these files for 1 day, overriding default settings.

Commands
This command adds a caching rule to the Azure CDN endpoint. It sets the CDN to cache jpg, png, css, and js files for 1 day, overriding default cache settings.
Terminal
az cdn endpoint rule add --resource-group exampleResourceGroup --profile-name exampleProfile --endpoint-name exampleEndpoint --rule-name exampleCachingRule --order 1 --action-name CacheExpiration --cache-behavior Override --cache-duration 1.00:00:00 --match-variable UrlFileExtension --operator Equal --match-values jpg png css js
Expected OutputExpected
{ "name": "exampleCachingRule", "order": 1, "actions": [ { "name": "CacheExpiration", "parameters": { "cacheBehavior": "Override", "cacheDuration": "1.00:00:00" } } ], "conditions": [ { "name": "UrlFileExtension", "parameters": { "operator": "Equal", "matchValues": [ "jpg", "png", "css", "js" ], "negateCondition": false, "transforms": [] } } ] }
--cache-behavior - Sets how the CDN caches content (Override means use this rule's duration)
--cache-duration - Specifies how long content is cached (1 day here)
--match-values - Defines which file extensions this rule applies to
This command lists all caching rules applied to the CDN endpoint so you can verify your new rule is active.
Terminal
az cdn endpoint rule list --resource-group exampleResourceGroup --profile-name exampleProfile --endpoint-name exampleEndpoint
Expected OutputExpected
[ { "name": "exampleCachingRule", "order": 1, "actions": [ { "name": "CacheExpiration", "parameters": { "cacheBehavior": "Override", "cacheDuration": "1.00:00:00" } } ], "conditions": [ { "name": "UrlFileExtension", "parameters": { "operator": "Equal", "matchValues": [ "jpg", "png", "css", "js" ], "negateCondition": false, "transforms": [] } } ] } ]
Key Concept

If you remember nothing else from this pattern, remember: CDN caching rules control how long and for which files your content is saved on servers close to users to speed up loading.

Common Mistakes
Setting cache duration too short or too long without considering content update frequency.
This causes users to see outdated content or forces the CDN to fetch from origin too often, slowing performance.
Choose cache durations that balance freshness and speed based on how often your content changes.
Not specifying file types in conditions, causing all content to be cached the same way.
Different files need different caching; for example, images can be cached longer than HTML pages.
Use conditions to target specific file extensions for appropriate caching rules.
Summary
Use the Azure CLI to add caching rules that specify which file types to cache and for how long.
Verify caching rules by listing them on the CDN endpoint to ensure they are applied correctly.
Caching rules help speed up content delivery by storing files closer to users for a set time.