Configure CDN Rules
Why CDN configuration matters
Section titled “Why CDN configuration matters”Default CDN settings work for most sites, but they are a compromise. Your marketing landing pages might benefit from aggressive caching, while your checkout flow needs no caching at all. Custom CDN rules let you fine-tune performance for each part of your site without sacrificing content freshness.
Set cache headers in your application
Section titled “Set cache headers in your application”The CDN respects HTTP cache headers sent by your application. The most direct way to control caching is to set these headers in your code.
ASP.NET Core response caching
Section titled “ASP.NET Core response caching”Add the response caching middleware and set cache profiles:
builder.Services.AddResponseCaching();
// In your middleware pipelineapp.UseResponseCaching();Apply cache profiles to controllers or pages:
[ResponseCache(Duration = 600, VaryByHeader = "Accept-Language")]public IActionResult Index(){ return View();}Per-route cache rules
Section titled “Per-route cache rules”For different cache behavior on different URL patterns, use middleware:
app.Use(async (context, next) =>{ var path = context.Request.Path.Value;
if (path.StartsWith("/api/")) { context.Response.Headers["Cache-Control"] = "no-store"; } else if (path.StartsWith("/static/")) { context.Response.Headers["Cache-Control"] = "public, max-age=31536000, immutable"; }
await next();});Configure CMS output cache
Section titled “Configure CMS output cache”Optimizely CMS has a built-in output cache that works with the CDN layer. Configure it in your startup:
services.Configure<OutputCacheOptions>(options =>{ options.DefaultExpiration = TimeSpan.FromMinutes(10);});Cache by content type
Section titled “Cache by content type”Set different cache durations for different content types:
services.AddOutputCache(options =>{ options.AddPolicy("LongCache", builder => builder.Expire(TimeSpan.FromHours(1))); options.AddPolicy("ShortCache", builder => builder.Expire(TimeSpan.FromMinutes(2)));});Set custom response headers
Section titled “Set custom response headers”Add custom headers for security, CORS, or debugging through middleware:
app.Use(async (context, next) =>{ context.Response.Headers["X-Content-Type-Options"] = "nosniff"; context.Response.Headers["X-Frame-Options"] = "SAMEORIGIN"; context.Response.Headers["Strict-Transport-Security"] = "max-age=31536000; includeSubDomains";
await next();});Purge the CDN cache
Section titled “Purge the CDN cache”Automatic purge on publish
Section titled “Automatic purge on publish”When editors publish content, the CMS automatically purges affected URLs from the CDN. This is handled by the built-in cache invalidation system. No additional configuration is needed for this default behavior.
Manual purge via portal
Section titled “Manual purge via portal”- Log into the management portal
- Navigate to your project
- Go to CDN > Purge Cache
- Choose purge scope:
- Single URL — Enter the exact URL to purge
- Path prefix — Enter a path like
/en/blog/to purge all matching URLs - Full purge — Clear the entire CDN cache
- Click Purge
Purge via CLI
Section titled “Purge via CLI”opti cdn purge --url "https://www.example.com/en/blog/" --environment Productionopti cdn purge --all --environment IntegrationCache debugging
Section titled “Cache debugging”Verify cache status
Section titled “Verify cache status”Check the response headers to confirm caching behavior:
curl -I https://www.example.com/en/Look for these headers:
| Header | Meaning |
|---|---|
X-Cache: HIT | Response served from CDN cache |
X-Cache: MISS | Response fetched from origin |
Age: 120 | Content has been cached for 120 seconds |
Cache-Control: public, max-age=600 | Cacheable for 10 minutes |
Common issues
Section titled “Common issues”| Symptom | Cause | Fix |
|---|---|---|
| Pages never cache | Cache-Control: private or no-store | Check middleware and CMS output cache settings |
| Stale content after publish | Purge not triggering | Verify CMS event handling; manual purge as fallback |
| Different content per user | Vary: Cookie creating too many variants | Reduce cookie variation; use client-side personalization |
| Static assets not caching | Missing cache headers on static files | Enable static file caching middleware |
Recommended cache strategy
Section titled “Recommended cache strategy”| URL pattern | Cache-Control | Rationale |
|---|---|---|
/static/*, /assets/* | public, max-age=31536000, immutable | Versioned files; cache forever |
/en/* (CMS pages) | public, max-age=600 | Content pages; 10-minute cache |
/api/* | no-store | Dynamic API responses |
/episerver/* | no-store | CMS editor interface |
/util/* | no-store | Utility and health endpoints |