A cacheable response is an HTTP response that Cloud CDN can store and quickly
retrieve, thus allowing for faster load times. Not all HTTP responses are
cacheable. Cloud CDN uses HTTP response headers that your origin
server sends to determine how to cache content at the edge. You can use standard
Cache-Control headers to manage caching universally, or use targeted
CDN-Cache-Control headers to apply specific caching rules to Cloud CDN
without affecting how end-user browsers cache your content.
Cache modes
With cache modes, you can control the factors that determine whether Cloud CDN caches your content.
Cloud CDN offers three cache modes, which define how responses are cached, whether Cloud CDN respects cache directives sent by the origin, and how cache TTLs are applied.
The available cache modes are shown in the following table:
| Cache mode | Behavior |
|---|---|
CACHE_ALL_STATIC |
Automatically caches successful responses with
static content that isn't otherwise
non-cacheable.
Origin responses that set valid caching directives are also cached. This behavior is the default for Cloud CDN-enabled backends created by using the Google Cloud CLI or the REST API. |
USE_ORIGIN_HEADERS |
Requires successful origin responses to set valid cache directives and valid
caching headers. Successful responses without these directives
are forwarded from the origin. |
FORCE_CACHE_ALL |
Unconditionally caches successful responses, overriding any cache directives set by the origin. This mode might not be appropriate if the backend serves private, per-user (user identifiable) content, such as dynamic HTML or API responses. Note: When private
bucket access is enabled on a bucket, you must set the
|
Error responses may be cached even in the absence of valid cache directives.
Before you set the cache mode to FORCE_CACHE_ALL, consider the following
behaviors:
For signed URLs or signed cookies,
FORCE_CACHE_ALLoverrides the maximum age specified through the Cache entry maximum age setting in the Google Cloud console or thegcloud --signed-url-cache-max-ageoption.FORCE_CACHE_ALLchanges the time to live (TTL) of any previously cached content. This change can cause some entries that were previously considered fresh (due to having longer TTLs from origin headers) to be considered stale, and it can cause some entries that were previously considered stale to be considered fresh.FORCE_CACHE_ALLoverrides cache directives (Cache-ControlandExpires) but does not override other origin response headers. In particular, aVaryheader might suppress caching even if the cache mode isFORCE_CACHE_ALL. For more information, see Vary headers.
For setup instructions, see Setting the cache mode.
Static content
Static content is content that is always the same, even when accessed by different users. The CSS that you use to style your site, JavaScript to provide interactivity, video, and image content typically don't change for each user for a given URL (cache key), and thus benefit from being cached across Cloud CDN's global edge network.
When you set the cache mode to CACHE_ALL_STATIC, and a response
does not have explicit caching directives in Cache-Control or Expires
headers, Cloud CDN automatically caches that response for the
following:
- Web Assets, including CSS (
text/css), JavaScript (application/javascript) and all web fonts, including WOFF2 (font/woff2) - Images, including JPEG (
image/jpg) and PNG (image/png) - Videos, including H.264, H.265, and MP4 (
video/mp4) - Audio files, including MP3 (
audio/mpeg) and MP4 (audio/mp4) - Formatted documents, including PDF (
application/pdf)
The following table provides a summary.
| Category | MIME types |
|---|---|
| Web assets | text/css text/ecmascript text/javascript application/javascript |
| Fonts | Any Content-Type matching font/* |
| Images | Any Content-Type matching image/* |
| Videos | Any Content-Type matching video/* |
| Audio | Any Content-Type matching audio/* |
| Formatted document types | application/pdf and application/postscript |
Cloud CDN inspects the Content-Type HTTP response header, which
reflects the MIME
type
of the content being served.
Note the following:
Your origin's web server software must set the
Content-Typefor each response. Many web servers automatically set theContent-Typeheader, including NGINX, Varnish, and Apache.Cloud Storage sets the
Content-Typeheader automatically when you use the Google Cloud console or the Google Cloud CLI to upload content.Cloud Storage always provides a
Cache-Controlheader to Cloud CDN. If no value is explicitly chosen, it sends a default value. As a result, all successful Cloud Storage responses are cached according to Cloud Storage default values, unless you explicitly adjust the cache control metadata for objects in Cloud Storage or useFORCE_CACHE_ALLmode to override the values sent by Cloud Storage.If you want to cache
text/htmlandapplication/jsoncontent types, you must set explicitCache-Controlheaders in the response, being careful not to accidentally cache one user's data and serve it to all users.
If a response is cacheable based on its MIME type but has a Cache-Control
response header of private or no-store, or a Set-Cookie
header, it isn't cached. To learn more, see cacheability rules.
Other content types, such as HTML (text/html) and JSON
(application/json), are not cached by default for successful responses. These
types of responses are typically dynamic (per user). Examples include shopping
carts, product pages with user personalization, and authenticated API
responses. Negative caching, if enabled, can
still cause them to be cached for certain status codes, however.
Cloud CDN doesn't use file extensions in the URL path to determine whether a response is cacheable because many valid cacheable responses aren't reflected in URLs.
Cache policy configuration methods
Depending on how much control you need over caching behavior, you can configure caching behavior for Cloud CDN at the backend service, backend bucket, or at a more granular level in URL maps.
Backend service or backend bucket cache policy
Configure a cache policy on a backend service or backend bucket to apply a single caching policy to all requests that are routed to that backend.
Cache policies in URL maps
You can configure Cloud CDN cache policies at various levels of the URL map. This provides granular control over caching policies based on criteria like hostname, URL path, HTTP headers, and query parameters for specific routes.
Setting cache policies at different levels of the URL map, such as root, path matchers, path rules, and route rules, gives you granular control over caching different types of content served by the same backend.
For example, you can configure path rules to:
- Cache static images in the
/images/*path for 24 hours. - Cache HTML pages in the
/pages/*path for 5 minutes.
You can configure cache policies in URL maps when:
- a single backend serves different types of content
- different paths require different caching behavior
- you want to enable caching for specific routes only
For details on how to configure cache policies in URL map, see Configure a Cloud CDN cache policy.
Default values for caching
For caching parameters, Cloud CDN uses the following default values:
| Parameter | Default value | Description |
|---|---|---|
| Cache mode | CACHE_ALL_STATIC |
Automatically caches successful responses for common static content types. |
| Client TTL | 3600s |
Sets a 1-hour max-age for the client's browser cache. |
| Default TTL | 3600s |
Sets a 1-hour cache duration if the origin provides no headers. |
| Include Host | true |
The request host is included in the cache key. |
| Include Protocol | true |
HTTP and HTTPS requests are cached as separate objects. |
| Include Query String | true |
The entire query string is part of the cache key. |
| Max TTL | 86400s |
The absolute maximum time (24 hours) an object remains in the cache. |
| Negative Caching |