Controlling cache behavior on Azure Content Delivery Networks (CDN) involves setting rules to determine how long content is stored (cached) at the CDN edge servers before it is refreshed from the origin server. Here’s a simple explanation of how you can manage this,
Basic Concepts
- Cache: Temporary storage of content at CDN edge servers closer to users to improve loading times.
- Origin Server: The server where the original content is hosted.
- Edge Server: Servers distributed across various locations that cache and deliver content to users.
Steps to Control Cache Behavior
Step 1. Default Caching
By default, Azure CDN uses the cache directives (like Cache-Control headers) set by your origin server. If your origin server doesn’t specify any cache settings, the CDN will use its default cache behavior.
Step 2. Custom Caching Rules
You can override the default caching behavior by setting custom caching rules in the Azure portal.
- Navigate to Your CDN Endpoint
- Go to the Azure portal.
- Find your CDN profile and click on the specific endpoint you want to configure.
- Caching Rules
- Within the endpoint settings, look for the "Caching rules" section.
- Click on "Caching rules" to add a new rule or modify existing ones.
- Add a Custom Rule
- Rule Name: Give your rule a meaningful name.
- Path: Define the path or file type the rule applies to (e.g., /images/* to apply the rule to all image files).
- Cache Behavior: Choose the cache behavior:
- Bypass Cache: Do not cache content.
- Override: Use custom settings regardless of origin server headers.
- Set if Missing: Apply the rule only if the origin server doesn't provide cache directives.
- Define Cache Duration
- Expiration Time: Set how long the content should be cached (e.g., 1 hour, 1 day).
Step 3. Advanced Cache Settings
For more granular control, you can configure advanced settings.
- Query String Caching: Decide whether content should be cached based on URL query strings (parameters).
- Ignore Query Strings: Cache content regardless of query strings.
- Include Query Strings: Cache different versions based on query strings.
- Cache Based on Selected Query Strings: Specify which query strings to consider.
- Compression: Enable or disable compression for cached content to reduce bandwidth usage.
Example
Let's say you have a website with images and you want them cached for a week, but your API responses should never be cached. Here’s how you might set this up.
- Image Caching Rule
- Rule Name: Cache Images
- Path: /images/*
- Cache Behavior: Override
- Expiration Time: 7 days
- API Caching Rule
- Rule Name: Bypass API Cache
- Path: /API/*
- Cache Behavior: Bypass Cache
Summary
By configuring caching rules in Azure CDN, you can optimize content delivery to improve user experience. Setting appropriate cache durations for different types of content ensures that static resources are quickly delivered while dynamic or sensitive content is always up-to-date.