Prompt
How do I set up a CDN for both static and dynamic content?
Latest observation
A CDN can help with both static and dynamic content, but they’re handled a bit differently:
- Static content (images, CSS, JS, fonts, downloads) is ideal for CDN caching.
- Dynamic content (personalized pages, API responses, logged-in data) usually isn’t cached heavily, but can still benefit from CDN features like edge routing, TLS termination, DDoS protection, and selective/short-lived caching.
1) Start by classifying your content
Separate your app into:
Static assets
Examples:
/assets/*/images/*/css/*/js/*/fonts/*
These should be served from the CDN with long cache lifetimes.
Dynamic content
Examples:
/api/*/account/*/checkout/*- personalized HTML
These usually need:
- no cache, or
- very short cache with careful variation rules, or
- edge compute / origin shielding / response caching for safe endpoints
2) Put the CDN in front of your origin
Typical setup:
Users → CDN → Origin server / app server / storage bucket
Your origin could be:
- web server (Nginx/Apache)
- app server (Node/Python/Ruby/etc.)
- object storage (S3, GCS, Azure Blob) for static content
- load balancer in front of multiple app instances
You’ll configure the CDN to fetch from your origin when it misses cache.
3) Configure static content caching
For static files:
Use long cache headers
At origin, send headers like:
Cache-Control: public, max-age=31536000, immutable
This tells browsers/CDNs to cache aggressively.
Use fingerprinted filenames
Example:
app.8f3a1c.jsstyles.91bdc2.css
Then you can safely cache for a year because changes create new filenames.
CDN rules
Set CDN behavior to:
- cache everything under static paths
- respect origin cache headers, or override with strong caching
- compress assets (Brotli/Gzip)
- enable HTTP/2 or HTTP/3
4) Handle dynamic content carefully
For dynamic pages and APIs, you usually have three options:
A. Don’t cache
Use:
Cache-Control: no-store
Best for:
- logged-in pages
- personal data
- checkout/cart
- sensitive APIs
B. Short TTL caching
For content that changes often but can be stale briefly:
Cache-Control: public, max-age=30, s-maxage=60
max-age= browser caches-maxage= shared cache/CDN cache
Good for:
- product listings
- homepages
- news feeds
- rate-limited APIs
C. Cache at the edge with variation
Cache based on:
- country/region
- device type
- language
- query string
- selected cookies
Be careful: varying by cookie or authorization header can destroy cache hit rates and can leak content if misconfigured.
5) Use cache rules by path
Most CDNs let you create rules like:
Static
- Path matches
/static/*,/assets/*,/images/* - Cache TTL: 1 year
- Ignore query strings or normalize them if appropriate
Dynamic API
- Path matches
/api/* - Cache TTL: 0 or very short
- Bypass cache when
Authorizationheader is present
HTML pages
- Cache only public pages
- Bypass authenticated sessions
- Possibly use edge caching for anonymous users
6) Make sure origin sends the right headers
At your origin, configure headers per content type.
Static file example
Cache-Control: public, max-age=31536000, immutable
Content-Encoding: br
Public dynamic content example
Cache-Control: public, max-age=30, s-maxage=300
Private content example
Cache-Control: private, no-store
7) Purge/invalidate when content changes
For static assets:
- best practice is fingerprinted filenames, so you rarely purge
- if needed, purge by URL or path
For dynamic cached content:
- use short TTLs
- purge on publish/update events if your CDN supports it
8) Protect dynamic endpoints
Even if not cached, the CDN can still help dynamic content by:
- absorbing DDoS traffic
- terminating TLS
- rate limiting
- bot filtering
- origin shielding
- load balancing across regions
9) Common gotchas
- Caching personalized pages accidentally
- Don’t cache responses with user-specific data unless carefully separated.
- Ignoring cookies/authorization
- Cache rules must bypass or vary correctly.
- Cache busting with query strings
- Decide whether query strings should be part of the cache key.
- Serving stale content too long
- Use sensible TTLs and purge workflows.
- Origin cache headers overridden unintentionally
- Know whether your CDN respects origin headers or has its own rules.
10) Example practical setup
A common pattern:
Static
- Store assets in object storage or on your origin
- Serve through CDN
- Fingerprint filenames
- Cache for 1 year
Dynamic
- Serve HTML/API from app servers behind CDN
- Do not cache authenticated responses
- Cache public pages for 30–300 seconds where safe
- Use edge protections and origin shielding
11) A simple decision table
| Content type | CDN cache? | Typical TTL |
|---|---|---|
| JS/CSS/images/fonts | Yes | 1 year |
| Versioned downloads | Yes | 1 year |
| Public HTML | Sometimes | 30s–5m |
| Public API | Sometimes | 10s–5m |
| Logged-in pages | Usually no | 0 |
| Checkout/account data | No | 0 |
If you tell me which CDN you’re using (Cloudflare, Fastly, Akamai, AWS CloudFront, Azure CDN, etc.) and what stack your app runs on, I can give you a concrete configuration example.