CloudFront X-Cache Error: What It Means & Fixes
A CloudFront X-Cache error means the X-Cache response header from Amazon CloudFront reports a problem rather than a normal hit or miss, most often an "Error from cloudfront" value. This happens when CloudFront cannot fetch a valid response from the origin or cannot serve the requested object, and the header is your first clue for diagnosing whether the issue is at the edge, in the cache, or at the origin.
What the X-Cache Header Actually Reports
CloudFront adds several diagnostic headers to responses it serves. The X-Cache header summarizes the cache outcome. Typical values include Hit from cloudfront, Miss from cloudfront, RefreshHit from cloudfront, and Error from cloudfront. A normal hit means the object was served from an edge location. A miss means CloudFront forwarded the request to the origin and cached the result. An error value means the request did not produce a cacheable, successful response.
The header is informational, not the error itself. The actual HTTP status code (such as 502, 503, or 504) and the X-Cache value together tell the story. For example, a 502 Bad Gateway with X-Cache: Error from cloudfront usually points to the origin returning an invalid response or being unreachable. A 504 Gateway Timeout with the same header suggests the origin took too long to respond. Reading only the header without the status code can send you down the wrong path.
Common Causes of X-Cache Errors
Most X-Cache errors trace back to one of a few origin-side or configuration problems. The list below covers the usual suspects.
- Origin unavailable or unhealthy: The origin server is down, overloaded, or refusing connections, so CloudFront cannot retrieve the object.
- Origin timeout: The origin takes longer than the configured origin response timeout to reply, producing a 504 and an error cache value.
- Invalid origin response: The origin returns malformed headers, an unexpected status code, or a response that violates CloudFront requirements.
- SSL/TLS mismatch: CloudFront is configured to connect to the origin over HTTPS, but the origin certificate is invalid, expired, or does not match the origin domain name.
- DNS or networking issues: The origin domain does not resolve correctly from CloudFront's network, or security groups and firewalls block CloudFront's IP ranges.
- Cache behavior misconfiguration: Forwarded headers, query strings, or cookies cause cache key problems, or the origin path is wrong.
- Custom error pages not configured: Without custom error responses, CloudFront passes through origin errors and may cache them depending on the error caching minimum TTL.
How CloudFront Handles Errors and Caching
CloudFront does not cache every error indefinitely. By default, it caches certain HTTP error responses for a short period controlled by the Error Caching Minimum TTL setting in the cache behavior. This means a transient origin failure can be served from the edge for a few seconds or minutes even after the origin recovers. That behavior explains why some X-Cache errors seem to persist briefly after a fix.
When CloudFront receives an error from the origin, it may return a generic error page to the viewer unless you configure custom error responses. A custom error response can map an origin status code to a different viewer-facing status code and optionally a static error page. This is useful for branding and for avoiding leaking origin details, but it does not fix the underlying cause. The X-Cache header will still reflect the error outcome.
Origin failover groups add another layer. If you configure an origin group with a primary and secondary origin, CloudFront can retry the request against the secondary origin when the primary fails. The X-Cache header and related headers such as X-Origin-Verify help confirm which origin served the response. Failover reduces viewer-facing errors but can mask a persistent primary origin problem if you do not monitor it.
Diagnosing an X-Cache Error Step by Step
Work through the following steps in order. Each step narrows the problem to the edge, the configuration, or the origin.
- Capture the full response. Use browser developer tools or a command-line tool to record the HTTP status code, all response headers including X-Cache, X-Amz-Cf-Id, and X-Amz-Cf-Pop, and the response body. The X-Amz-Cf-Id is a unique request identifier that AWS support can use to trace the request.
- Check the status code. A 502 points to an invalid or unreachable origin response. A 503 often indicates capacity or throttling at the origin. A 504 indicates a timeout. A 403 or 404 may indicate a permissions or path problem rather than a classic cache error.
- Test the origin directly. Request the same object from the origin domain, bypassing CloudFront, from a network with similar access. If the origin fails directly, the problem is at the origin. If it succeeds, the problem is in the CloudFront-to-origin path or configuration.
- Review CloudFront logs. Standard logs or real-time logs record the cache result, status code, and origin details for each request. Look for patterns such as a single edge location, a specific object path, or a spike in 5xx responses.
- Inspect the cache behavior. Confirm the origin domain, origin path, protocol policy, and timeout settings. Verify that forwarded headers and query strings are intentional and that the cache key matches what the origin expects.
- Check certificates and DNS. For HTTPS origins, confirm the certificate is valid and matches the origin domain. Confirm the origin DNS resolves and that firewall rules allow CloudFront's published IP ranges.
- Invalidate if needed. If a bad response was cached, create an invalidation for the affected paths. Invalidation clears the edge cache but does not repair the origin.
Fixing the Most Frequent Problems
For origin timeouts, increase the origin response timeout only after confirming the origin cannot be made faster; a longer timeout delays the error rather than preventing it. For invalid responses, check that the origin returns well-formed headers and a supported status code. For SSL errors, update the origin certificate or change the origin protocol policy to match what the origin supports. For DNS and firewall issues, verify the origin domain and allow CloudFront's IP ranges at the origin.
A common misconception is that the X-Cache header itself is the error to fix. It is a symptom label. Changing cache settings or invalidating objects will not resolve an origin that is down, misconfigured, or returning invalid responses. Treat the header as a pointer and investigate the status code and origin behavior.
Monitoring and Preventing Recurrence
Set up CloudFront alarms on 5xx error rates and origin latency. Use real-time logs to detect error spikes quickly. Configure custom error responses so viewers see a controlled page during origin failures. If high availability matters, use origin failover groups and health checks so CloudFront can route around a failing primary origin. Periodically review cache behaviors and origin settings after any deployment that changes domains, certificates, or paths.
Frequently Asked Questions
What does "X-Cache: Error from cloudfront" mean?
It means CloudFront could not serve a valid cached or origin response for that request. The accompanying HTTP status code identifies whether the cause is a timeout, an invalid origin response, or another failure.
Is an X-Cache error a CloudFront outage?
Not usually. Most X-Cache errors originate from the origin or from configuration, not from a CloudFront service outage. Check the status code and test the origin directly before assuming an edge problem.
Why does the error persist after I fix the origin?
CloudFront may cache error responses for the Error Caching Minimum TTL. Waiting for that period to expire or creating an invalidation for the affected paths clears the stale error from the edge.
Can I stop CloudFront from caching errors?
You can reduce the error caching minimum TTL in the cache behavior, but you cannot fully disable error caching for all cases. Custom error responses control what viewers see but do not change the underlying cache behavior.
Does the X-Cache header appear on all responses?
It appears on responses served by CloudFront, but the exact set of diagnostic headers can vary by configuration and whether the response came from the cache or the origin.