Website Performance
10 Practical CDN Optimization Tips for a Faster Website
By NileVPS Team · Published · 4 min read
اقرأ المقال بالعربيPutting a CDN in front of your website is easy; getting real speed out of it takes a little more work. Many sites switch a CDN on, see the same load times as before, and conclude that it does not help. In most cases the CDN is simply passing almost every request back to the origin server because nothing tells it what it may cache. The ten tips below focus on the settings that make the biggest difference, and most of them come down to sending the right headers from your origin. If you are new to the idea, start with what a CDN is and how it works.
1. Send explicit Cache-Control headers
A CDN decides what to store mainly from the Cache-Control header your origin sends. Without it, many CDNs fall back to conservative defaults or do not cache at all. For static files, set a long lifetime in Nginx:
location ~* \.(css|js|woff2|svg|png|jpg|jpeg|webp|avif)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
access_log off;
}For HTML that changes, use a short shared lifetime instead, for example Cache-Control: public, max-age=0, s-maxage=300. Here s-maxage applies only to shared caches such as the CDN, while browsers revalidate every time.
2. Version your static assets
Long lifetimes are only safe if a changed file gets a new URL. Build tools can add a content hash to file names (app.3f9a1c.js). When the file changes, the name changes, so you never need to purge it and visitors never receive a stale copy.
3. Keep the cache key small
The cache key is what the CDN uses to decide whether two requests are the same object. If it includes every query string, then ?utm_source=x and ?utm_source=y become separate objects and your hit ratio drops. Ignore marketing parameters in the cache key, and avoid varying on cookies for public content. The edge caching guide covers cache keys in more depth.
4. Be careful with the Vary header
Vary: Accept-Encoding is normal and useful. Vary: User-Agent or Vary: Cookie can split the cache into thousands of variants. Only vary on headers that really change the response.
5. Compress at the right layer
Enable Brotli or gzip either at the CDN or at the origin, and make sure the CDN stores compressed variants correctly. Text assets such as HTML, CSS, JavaScript, JSON and SVG shrink a lot; JPEG, PNG, WebP and video files are already compressed and should be left alone. See gzip and Brotli on Nginx.
6. Use an origin shield or tiered caching
Without shielding, every CDN location that misses goes straight to your server. With an origin shield, edge locations first ask a single intermediate cache, so your server sees far fewer requests for the same object. This matters most for sites with global traffic and a small origin VPS.
7. Serve stale content on errors
Directives such as stale-while-revalidate and stale-if-error let the CDN keep serving an old copy while it fetches a new one, or while your origin is down for a moment:
Cache-Control: public, max-age=300, stale-while-revalidate=60, stale-if-error=86400Support varies by provider, so check your CDN's documentation.
8. Enable HTTP/2 and HTTP/3
Most CDNs offer HTTP/2 and HTTP/3 (QUIC) to visitors with a single toggle. HTTP/3 helps mainly on mobile networks with some packet loss. The connection between the CDN and your origin can stay on HTTP/1.1 or HTTP/2.
9. Optimize images before they reach the CDN
A CDN delivers a 3 MB photo faster, but it is still 3 MB. Resize images to the size they are displayed at, use WebP or AVIF where supported, and add width and height attributes to avoid layout shifts. Some CDNs can convert formats on the fly, often as a paid feature.
10. Measure the hit ratio and response headers
Check what is actually happening rather than guessing:
curl -sI https://example.com/assets/app.css | grep -iE 'cache-control|age|cf-cache-status|x-cache'An Age header above zero and a status such as HIT mean the object came from the edge. Your CDN dashboard shows the overall cache hit ratio; watch it after every configuration change.
Quick checklist
| Item | Good sign |
|---|---|
| Static assets | max-age=31536000, immutable and hashed file names |
| HTML | short s-maxage, or not cached if personalized |
| Cache key | marketing query strings ignored |
| Compression | Brotli or gzip on text only |
| Origin load | shield or tiered cache enabled |
| Monitoring | hit ratio checked regularly |
Do not forget the origin
A CDN hides a slow origin only for cached content. Logged-in pages, carts, APIs and every cache miss still depend on your server. A well-sized VPS close to your main audience keeps those requests fast; you can compare plans on the pricing page.
FAQ
Should I cache HTML on a CDN?
You can cache HTML for public pages that look the same for every visitor, usually with a short s-maxage of a few minutes and a purge on publish. Never cache pages that contain user-specific data, such as account pages or carts, unless your CDN is configured to bypass the cache for logged-in sessions.
Why is my CDN hit ratio low?
The usual causes are missing or short Cache-Control headers, query strings or cookies in the cache key, Vary headers that split the cache, and Set-Cookie on static responses, which many CDNs treat as uncacheable. Fix the headers at the origin first.
Do I still need a good server if I use a CDN?
Yes. The CDN serves cached files, but dynamic pages, API calls and cache misses are processed by your origin. A slow origin shows up as a slow first load, slow checkout and slow admin pages, regardless of the CDN.