CDN Error Handling: Catch Failures Before Users Do
In this article
- How to Handle a CDN Failure, Step by Step
- Which CDN Problem Do You Actually Have?
- Why Is the CDN Returning 502 and 504 Errors?
- Why Do Users See Stale or Missing Files?
- Why Are HTTPS Requests Failing Through the CDN?
- How Do You Build Automatic Fallback?
- Quick Checks Before and After Any CDN Change
- How Do You Prevent CDN Errors Coming Back?
CDN error handling means your site keeps working when the CDN fails: detect the failure fast, fall back to your origin server, and log which URLs broke so you can fix the cause. Most CDN errors are stale cache, a misconfigured origin, or a bad certificate, and all three have concrete fixes.
Time 10 min · Steps 6 · Level Medium
How to Handle a CDN Failure, Step by Step
- Detect the failure. Watch your CDN's HTTP status codes; a spike in 5xx responses or a jump in time-to-first-byte past about 1 second means something is wrong before users report it.
- Fail over to the origin. Configure your load balancer or DNS so that when the CDN returns errors, requests go straight to your origin server instead of showing users a broken page.
- Serve a fallback page. Return a custom error page with a retry message and a status code, never a blank screen, so users know the outage is temporary.
- Log every failed request. Capture the URL, status code, edge location and timestamp so you can reconstruct what happened after the outage ends.
- Alert the right person. Route 5xx spikes to whoever owns the origin, and cache-miss spikes to whoever owns CDN configuration, so the alert reaches someone who can act.
- Fix the root cause and purge. Once the origin or config is repaired, purge the bad cache entries so users get fresh content instead of the cached errors.
Which CDN Problem Do You Actually Have?
Start with the status code, because each code points to a different layer. A 502 or 504 usually means the CDN cannot reach your origin server. A 403 means the CDN is blocking the request, often a firewall or access rule. A 404 on files that exist at the origin usually means the cache is stale or the cache key does not match the request. A mixed-content or certificate warning means HTTPS is broken somewhere between the CDN and the origin. Test the same URL directly against your origin, bypassing the CDN: if it works there but fails through the CDN, the problem is in the CDN layer; if it fails at the origin too, fix the origin first.
Why Is the CDN Returning 502 and 504 Errors?
These codes mean the CDN asked your origin for the file and either got a bad response (502) or no response in time (504). Confirm it by requesting the URL directly from the origin server and by checking the origin's own logs for the same timestamp. The fix is usually one of three things: the origin's timeout is shorter than the CDN's timeout, so raise the CDN's origin timeout to a realistic value such as 30-60 seconds for slow pages; the origin server is down or overloaded, so restart it or add capacity; or the origin's firewall is blocking the CDN's IP ranges, so allowlist them in the firewall rules. After fixing, purge the URL and request it again through the CDN to confirm the error is gone.
Why Do Users See Stale or Missing Files?
Stale content is a caching problem, not a server problem, and it is the most common CDN complaint. Confirm it by comparing the file's timestamp at the origin with what the CDN serves; if the CDN serves an older version, the cache is stale. The fix is to purge the cache for the affected URLs, then set sensible cache-control headers at the origin so the CDN refreshes on its own: long cache times like one week or one month for hashed asset filenames, and short times like 60 seconds to a few minutes for HTML. If files 404 through the CDN but exist at the origin, check the cache key settings; a key that includes query strings will treat style.css?v=2 and style.css as two different files, which is a common source of missing-file errors.
Why Are HTTPS Requests Failing Through the CDN?
Certificate problems show up as browser warnings, mixed-content errors, or 5xx codes on HTTPS only. Confirm it by opening the failing URL and inspecting the certificate the browser reports; if the name on the certificate does not match your domain, the CDN is serving the wrong certificate. The fix is to enable the CDN's certificate option for your domain (most providers offer a free managed certificate), make sure the CDN-to-origin connection is also HTTPS or at least a trusted connection, and fix any page that loads scripts or images over plain HTTP, because browsers block mixed content on HTTPS pages. Test with the site's own browser tools after each change, since certificate fixes can take a few minutes to propagate.
How Do You Build Automatic Fallback?
The strongest error handling happens before the error: plan the fallback so a CDN outage degrades gracefully instead of taking the site down. Three patterns cover most setups. First, DNS failover: point your domain at the CDN, but keep the origin's IP ready so you can switch DNS records in an outage; expect roughly 5-10 minutes of propagation for a low TTL. Second, origin shielding: keep a second CDN or a direct origin route configured and tested, not built during the outage. Third, client-side retry: for asset loads, a small script that retries a failed file from a second URL gives users a working page even while you fix the CDN. Whichever you use, test the fallback by temporarily blocking the CDN in your own setup and confirming the site still loads; an untested fallback is not a fallback.
Quick Checks Before and After Any CDN Change
- Request one known-good URL through the CDN and directly at the origin; both should return the same content and a 200 status.
- Check the CDN's dashboard error rate before and after any config change; it should return to baseline within a few minutes.
- Verify cache-control headers with your browser's developer tools on a real page, not just on the CDN's test page.
- Purge a single test URL first, then purge broadly; a full purge can briefly overload the origin as every request becomes a cache miss.
- Confirm the custom error page renders, by requesting a URL you know will 404.
- Check that alerts fire to a real channel, by triggering a test alert if your provider offers one.
How Do You Prevent CDN Errors Coming Back?
Prevention is mostly configuration hygiene. Keep cache-control headers correct at the origin so the CDN never has to guess. Monitor the origin's health directly, not just through the CDN, so you can tell the two apart during an incident. Keep the CDN's origin timeout at or above the origin's slowest normal response time. Allowlist the CDN's IP ranges in the origin firewall and re-check after any firewall change. Review CDN access rules after anyone edits them, because a too-strict rule is a classic cause of sudden 403s. And run the fallback test above every few months, since certificates, IP ranges and settings all drift over time. Menu names and settings differ slightly between CDN providers and versions, so match these steps against your own provider's current documentation.
Official source: Cloudflare Help Center