CDN Setup Guide
Content Delivery Network configuration and best practices.
What is CDN?
Benefits
• Faster content delivery
• Reduced server load
• Lower bandwidth costs
• DDoS protection
• Global reach
How it works
1. Content cached on edge servers
2. User requests served from nearest location
3. Reduces latency and improves speed
Popular CDN providers
• Cloudflare (free + paid)
• Amazon CloudFront
• Fastly
• Akamai
• BunnyCDN
Cloudflare Setup
1. Create account
Sign up at cloudflare.com
2. Add site
Enter your domain name
3. Update nameservers
Point to Cloudflare nameservers
ns1.cloudflare.com
ns2.cloudflare.com
4. Configure cache rules
Cache Level: Standard
Browser Cache TTL: Respect Existing Headers
5. Enable features
• Auto Minify (CSS, JS, HTML)
• Brotli compression
• HTTP/3 (QUIC)
• Always Use HTTPS
AWS CloudFront
Create distribution via AWS CLI
aws cloudfront create-distribution \
--origin-domain-name mybucket.s3.amazonaws.com \
--default-root-object index.html
Invalidate cache
aws cloudfront create-invalidation \
--distribution-id E1234567890ABC \
--paths "/*"
Custom cache policy
• Min TTL: 0
• Max TTL: 31536000 (1 year)
• Default TTL: 86400 (1 day)
Compression
Enable automatic object compression: Yes
CDN Best Practices
Use versioned URLs
script.js?v=1.2.3
script-abc123.js
Set proper Cache-Control headers
Cache-Control: public, max-age=31536000, immutable
Use CDN for static assets only
• Images, CSS, JavaScript
• Fonts, videos
• NOT for dynamic content
Enable compression
Gzip or Brotli for text assets
Use HTTP/2 or HTTP/3
Multiplexing, header compression
Purge cache strategically
• After deployments
• For specific files only
• Avoid full cache purges