Understanding Origin Cache-Control

Learn how Cloudflare interprets Cache-Control headers set at your origin server.


Overview

Cache-Control headers are one way for web administrators to tell Cloudflare how to handle content from the origin. This article explains how Cloudflare makes caching decisions during the request and response phase for a resource, and the options you have for setting Cache-Control directives at your origin server. 

Request phase

In the request phase, the client request URL is matched against a list of cacheable file extensions. If the request matches an extension on this list, Cloudflare serves the resource from cache if present. If the content is stale in the Cloudflare cache, Cloudflare attempts to revalidate the content with the origin before serving the response to the client.

It is possible to modify what Cloudflare deems cacheable via the Cache Level setting in the Cloudflare Page Rule app. 

To learn more about creating Page Rules, refer to our Page Rules tutorial.

As an example, Cache Everything skips the extension check, and all content is treated as cacheable. Refer to Cloudflare's guide on how to enable Cache Everything.

Response phase

If Cloudflare deems a request cacheable, we first examine our caches in multiple network locations for content. If the resource is not present in cache, Cloudflare requests the resource from your origin web server to fill our cache. The response is then sent to the client who initiated the request.

At this point, our cache logic examines the HTTP response received from your origin server.

Based on how Cloudflare interprets request headers, the response is either deemed cacheable and written to disk (for use with the next request for the same resource) or deemed un-cacheable (resulting in the next request missing cache and repeating this flow).


Set cache-control directives

The Cache-Control header can include a number of directives. If multiple directives are passed together, each is separated by a comma.

For more information about Cache-Control directives at origin servers, refer to the Mozilla Cache-Control documentation.

If the directive takes an argument, it follows the directive separated by an equal sign.  For example:

max-age=86400

These directives break down into groupings as follows:

Cacheability - should this resource enter a cache?

public
The public response directive indicates that any cache may store the response, even if the response is normally non-cacheable or cacheable only within a private cache.
private
The private response directive indicates that the response message is intended for a single user (eg. a browser cache) and must not be stored by a shared cache (like Cloudflare, or a corporate proxy).
no-store
The no-store response directive indicates that any cache (ie a client or proxy cache) must not store any part of either the immediate request or response.

Expiration - how long should this resource stay in the cache?

max-age=seconds
The max-age response directive indicates that the response is to be considered stale after its age is greater than the specified number of seconds. Age is defined as the time in seconds since the asset was served from the origin server. The seconds argument is an unquoted integer.
Cloudflare respects whichever value is higher: the Browser Cache TTL in Cloudflare or the max-age header.
s-maxage=seconds
The s-maxage response directive indicates that, in shared caches, the maximum age specified by this directive overrides the maximum age specified by either the max-age directive or the Expires header field.  The s-maxage directive also implies the semantics of the proxy-revalidate response directive.  Browsers ignore s-maxage.
no-cache
The no-cache response directive indicates that the response cannot be used to satisfy a subsequent request without successful validation on the origin server. This allows an origin server to prevent a cache from using it to satisfy a request without contacting it, even by caches that have been configured to send stale responses.
Simultaneously specify a Cloudflare Edge Cache TTL different than a browser’s cache TTL respectively via the s-maxage and max-age Cache-Control headers.  
In addition to setting the parameters below, make sure the HTTP Expires header is set in your origin server to use the Greenwich Mean Time (GMT) format as stipulated in RFC 2616.

Revalidation - how should the cache behave when a resource has expired?

must-revalidate
The must-revalidate response directive indicates that once it has become stale, a cache (client or proxy) must not use the response to satisfy subsequent requests without successful validation on the origin server.
proxy-revalidate
The proxy-revalidate response directive has the same meaning as the must-revalidate response directive, except that it does not apply to private client caches.
stale-while-revalidate=seconds
When present in an HTTP response, the stale-while-revalidate Cache-Control extension indicates that caches may serve the response in which it appears after it becomes stale, up to the indicated number of seconds since the resource expired.
If Always Online is enabled, then the stale-while-revalidate and stale-if-error directive are ignored.
stale-if-error=seconds
The stale-if-error Cache-Control extension indicates that when an error is encountered, a cached stale response may be used to satisfy the request, regardless of other freshness information.
The stale-if-error directive is ignored if Always Online is enabled or if an explicit in-protocol directive (e.g., by a no-store or no-cache cache directive, a must-revalidate cache-response-directive, or an applicable s-maxage or proxy-revalidate cache-response-directive) is passed.

Other

no-transform
The no-transform response directive indicates that an intermediary (regardless of whether it implements a cache) must not transform the payload
immutable
Indicates to clients that the response body does not change over time. The resource, if unexpired, is unchanged on the server and therefore the client should not send a conditional revalidation for it (e.g. If-None-Match or If-Modified-Since) to check for updates, even when the user explicitly refreshes the page. This directive has no effect on public caches like Cloudflare, but does change browser behavior.

Examples

For more information about Cache-Control directives at origin servers, refer to the Mozilla Cache-Control documentation.

Not finding what you need?

95% of questions can be answered using the search tool. This is the quickest way to get a response.

Powered by Zendesk