| .. | ||
| cdn-configmap.yaml | ||
| cloudflare-config.yaml | ||
| cloudfront-config.yaml | ||
| nginx-cdn-config.yaml | ||
| README.md | ||
CDN Configuration
This directory contains Kubernetes configurations for Content Delivery Network (CDN) setup to optimize delivery of static assets and audio files.
Overview
CDN configuration provides:
- Faster asset delivery through edge caching
- Reduced origin server load
- Better global performance with geographically distributed caching
- Optimized caching for different asset types
Components
nginx-cdn-config
- Optimized nginx configuration for CDN integration
- Long cache headers for static assets
- CORS headers for cross-origin requests
- Range request support for audio/video streaming
cdn-configmap
- General CDN configuration
- Provider selection
- Cache TTL settings
- Feature toggles
Provider-Specific Configs
- cloudflare-config.yaml: Cloudflare CDN configuration
- cloudfront-config.yaml: AWS CloudFront CDN configuration
Supported CDN Providers
Cloudflare
- Pros: Easy setup, free tier, DDoS protection, global network
- Cons: Limited customization on free tier
- Best for: Small to medium deployments
AWS CloudFront
- Pros: Highly customizable, integrates with AWS services, pay-per-use
- Cons: More complex setup, AWS account required
- Best for: AWS-based infrastructure
Generic CDN
- Pros: Works with any CDN provider
- Cons: Manual configuration required
- Best for: Custom CDN solutions
Deployment
1. Apply nginx CDN Configuration
kubectl apply -f k8s/cdn/nginx-cdn-config.yaml
Update frontend deployment to use this config:
volumeMounts:
- name: nginx-cdn-config
mountPath: /etc/nginx/conf.d/cdn.conf
subPath: nginx-cdn.conf
volumes:
- name: nginx-cdn-config
configMap:
name: nginx-cdn-config
2. Apply CDN ConfigMap
kubectl apply -f k8s/cdn/cdn-configmap.yaml
3. Configure CDN Provider
Cloudflare
- Update
cloudflare-config.yamlwith your zone ID - Create secret with API token:
kubectl create secret generic cloudflare-secrets \ --from-literal=api-token=your-api-token \ -n veza-production - Apply configuration:
kubectl apply -f k8s/cdn/cloudflare-config.yaml
AWS CloudFront
- Update
cloudfront-config.yamlwith your distribution ID - Create secret with AWS credentials:
kubectl create secret generic aws-secrets \ --from-literal=access-key-id=your-key \ --from-literal=secret-access-key=your-secret \ -n veza-production - Apply configuration:
kubectl apply -f k8s/cdn/cloudfront-config.yaml
Configuration
Cache TTL Settings
Edit cdn-configmap.yaml to adjust cache TTLs:
# Static assets (JS, CSS, images, fonts)
cdn-cache-ttl: "31536000" # 1 year
# Audio files
cdn-audio-cache-ttl: "2592000" # 30 days
Enable/Disable CDN Features
# Enable CDN for static assets
cdn-assets-enabled: "true"
# Enable CDN for audio files
cdn-audio-enabled: "true"
# Enable CDN for images
cdn-images-enabled: "true"
Integration with Services
Frontend
The frontend should use CDN URLs for static assets. Update environment variables:
VITE_CDN_URL=https://cdn.veza.com
VITE_CDN_ENABLED=true
Backend API
The backend CDN service (internal/services/cdn_service.go) can generate CDN URLs:
cdnService := services.NewCDNService(services.CDNConfig{
Provider: services.CDNProviderCloudflare,
BaseURL: "https://cdn.veza.com",
Enabled: true,
})
assetURL := cdnService.GetAssetURL("images", "logo.png")
audioURL := cdnService.GetAudioURL("track-123", "song.mp3")
Cache Invalidation
Manual Invalidation
# Invalidate specific paths
kubectl exec -it deployment/veza-backend-api -n veza-production -- \
/app/veza-api cdn invalidate /static/js/app.js /audio/track-123/song.mp3
Automatic Invalidation
The backend CDN service supports automatic cache invalidation on content updates. Configure in cdn-configmap.yaml:
cdn-invalidation-on-update: "true"
Testing
Verify CDN Headers
# Check static asset headers
curl -I https://cdn.veza.com/static/js/app.js
# Should see:
# Cache-Control: public, immutable, max-age=31536000
# X-CDN-Cache-Status: HIT
Test CORS
# Test CORS for audio files
curl -H "Origin: https://app.veza.com" \
-H "Access-Control-Request-Method: GET" \
-H "Access-Control-Request-Headers: Range" \
-X OPTIONS \
https://cdn.veza.com/audio/track-123/song.mp3
Check Cache Status
# View CDN cache headers
curl -I https://cdn.veza.com/static/css/app.css | grep -i cache
Monitoring
CDN Metrics
Monitor CDN performance:
- Cache hit ratio
- Origin requests
- Bandwidth usage
- Response times
Set Up Alerts
Alert on:
- Low cache hit ratio (< 80%)
- High origin requests
- CDN errors
Best Practices
- Use long cache TTLs for immutable assets (JS, CSS with hashes)
- Use shorter TTLs for dynamic content
- Enable compression (gzip, brotli) at CDN level
- Use CDN for audio/video to reduce origin load
- Monitor cache hit rates and adjust TTLs accordingly
- Invalidate cache when deploying new versions
- Use versioned URLs for assets (e.g.,
/static/js/app-v1.2.3.js)
Troubleshooting
Assets Not Loading from CDN
-
Check CDN configuration:
kubectl get configmap cdn-config -n veza-production -o yaml -
Verify CDN base URL is correct
-
Check DNS resolution for CDN domain
-
Verify CORS headers are set correctly
Cache Not Working
-
Check cache headers in response:
curl -I https://cdn.veza.com/static/js/app.js -
Verify CDN provider settings
-
Check cache TTL configuration
-
Verify CDN is enabled in configmap
CORS Issues
- Check CORS headers in nginx config
- Verify
Access-Control-Allow-Originis set - Check preflight OPTIONS requests are handled
- Verify allowed methods and headers