Edge, Origin, and Cache
With a CDN, your domain's DNS points to the CDN, not to your server. A visitor's request reaches the nearest edge server. If that edge has a fresh copy of the file, it answers at once: a cache hit. If not, it fetches the file from the origin, returns it, and keeps a copy for the next visitor: a cache miss. Cloudflare is one widely used CDN; the ideas below apply to all of them.
The edge is a reverse proxy run by someone else, in many locations.
Cache-Control Headers
Your server tells caches what they may keep, and for how long, with the Cache-Control response header:
public, max-age=86400: any cache may keep the response for a day.s-maxage: a lifetime for shared caches such as a CDN, separate from browsers.private: only the visitor's browser may keep it, never a shared cache.no-store: nobody keeps it.no-cache: may be kept, but must be checked with the origin before each use.
HTTP itself does not forbid caching a response that carries Set-Cookie, but many CDNs, Cloudflare among them, do not cache such a response by default. Send private on anything personal rather than relying on that. In Nginx, long lifetimes for static files look like this:
location ~* \.(css|js|png|jpg|svg|woff2)$ {
add_header Cache-Control "public, max-age=2592000";
}
What Is Cached by Default
Settings differ between CDNs, but the general pattern is similar: static files recognized by their extension, such as images, style sheets, scripts, and fonts, are cached; HTML pages and API responses are passed through to the origin unless you configure otherwise. Cloudflare, for example, decides by file extension and does not cache HTML or JSON by default. Requests that change data, such as POST, are not served from cache. Caching HTML is possible, but only for pages that look the same for everyone; a cached page with one user's account details would be shown to others.
TLS at the Edge
The CDN presents a certificate for your domain to visitors and terminates HTTPS at the edge. The connection from the edge to your origin is a second connection, and it should be encrypted as well, with a valid certificate on your server. A missing or invalid origin certificate shows up as errors such as Cloudflare error 525 and error 526. Background on certificates is in what is SSL/TLS.
On Your Server: Check Whether a Response Came from Cache
curl -I https://example.com/style.css
curl -I https://example.com/style.css
- Cache status header: most CDNs add one. Cloudflare uses
cf-cache-statuswith values such asHIT,MISS,EXPIRED,BYPASS, andDYNAMIC, the last meaning the request was not eligible for caching at all; others usex-cache. For a cacheable file, the second request is usually a hit. age: how many seconds the copy has been in the cache.cache-control: what your origin sent, which is the first place to look when something is not cached.
To bypass the CDN and ask the origin directly, pin the name to your server's address:
curl -I --resolve example.com:443:203.0.113.10 https://example.com/
curl then connects to that address while still sending the right host name. For normal traffic, the origin's access log shows CDN addresses instead of visitors. The CDN passes the visitor's address in a header, which your web server must be set up to read.
Protecting the Origin
- Firewall: allow ports 80 and 443 only from the CDN's published address ranges, so visitors cannot go around it. Keep that list up to date; an outdated list causes errors such as error 522.
- Do not leak the address: old DNS records, a mail server on the same IP, and other names in the zone can reveal the origin. Background on attacks is in what is a DDoS attack.
Purging
After you deploy a change, edge copies may stay until they expire. Purge the changed files in the CDN's dashboard or API. A more reliable habit is versioned file names, such as style.v42.css: a new name is a new file, which every cache fetches fresh.
When a CDN Does Not Help
- Dynamic, per-user pages: every request still goes to the origin.
- Visitors close to the server: the gain from a nearby edge is small.
- A slow application: a cache miss is as slow as before; fix the origin first.
- Uploads and long-running requests: they pass through and may hit the CDN's size or time limits.