This page explains how to customize Cloud CDN cache keys.
Changing cache key configuration might result in a sudden drop in the cache hit ratio if new requests start using cache keys that are different from old cache key entries. Similarly, changing the cache key creator does not necessarily invalidate cached entries if new requests use the same key as old cache keys. To invalidate existing cached entries, see Invalidating cached content.
Before you begin
This page assumes an understanding of Cloud CDN, Cloud CDN cache keys, and load balanced backend services. We recommend reviewing these pages before proceeding.
Enable Cloud CDN and customize cache keys
These instructions activate Cloud CDN for a load balanced backend service and customize the cache key by excluding one or more components. If you don't already have a load balancer to use as an origin, see the external Application Load Balancer documentation for instructions to create one.
Backend buckets don't include the protocol or host in the cache key because these don't influence how objects are referenced within a Cloud Storage bucket. However, they include a set of query string parameters that are specific to Cloud Storage, which might affect the response and might be extended by a customizable list of parameter names.
Console
- In the Google Cloud console, go to the Cloud CDN page.
- Click Add origin.
- In the Origin pull-down menu, click Select origin.
- Select the origin for which you want to enable CDN.
In the row of the origin, click Configure.
If Cloud CDN is already enabled on this origin, first click the
menu, select Edit, and then click Configure.Clear the checkboxes for any fields that you want to omit from the cache key for this backend service.
Click Save.
Click Add.
gcloud
Specify one or more of the optional flags to exclude the component from the cache keys for this backend service.
gcloud compute backend-services update BACKEND_SERVICE \ --enable-cdn
Optional flags:
--no-cache-key-include-protocol
--no-cache-key-include-host
--no-cache-key-include-query-string
Update cache keys to re-add protocol, host, and query string
By default, backend services configured to use Cloud CDN include all components of the request URI in cache keys. If you previously indicated that one or more components should be excluded, you can use the following steps to include them again.
These instructions re-add the protocol, host, and query string to the cache key for an existing backend service that already has Cloud CDN enabled.
Console
- In the Google Cloud console, go to the Cloud CDN page.
- In your load balancer's row, click Menu and then click Edit.
- In the row of the backend service or backend bucket that you want to modify, click Configure.
- Under Cache key, select Custom.
- Select the Protocol, Host, and Query string checkboxes.
- Leave the Query string parameters field blank.
- Click Save.
- Click Update.
gcloud
To re-add one component only, specify that flag alone. Any unspecified flags are left unchanged.
gcloud compute backend-services update BACKEND_SERVICE \ --cache-key-include-protocol \ --cache-key-include-host \ --cache-key-include-query-string
Update cache keys to use an include or exclude list of query string parameters
These instructions set Cloud CDN cache keys to use an include list or exclude list with query string parameters.
Console
- In the Google Cloud console, go to the Cloud CDN page.
- In your load balancer's row, click Menu and then click Edit.
- In the row of the backend service that you want to modify, click Configure.
- Under Cache key, select Custom.
- Confirm that the Query string checkbox is selected.
If you want to specify query string parameters that should be part of the cache key, select Include only selected.
If you want to specify that all query string parameters except the ones that you list are included in the cache key, select Include all but selected.
Enter your comma-separated list of strings in the Query string parameters field.
Click Save.
Click Update.
gcloud
Use this command to set the query string parameter user
to the include
list of a backend service.
gcloud compute backend-services update BACKEND_SERVICE \ --cache-key-include-query-string \ --cache-key-query-string-whitelist user
Use this command to set the query string parameter user
to the exclude
list of a backend service.
gcloud compute backend-services update BACKEND_SERVICE \ --cache-key-include-query-string \ --cache-key-query-string-blacklist user
Use this command to set the query string parameter user
to the include
list of a backend bucket.
gcloud compute backend-buckets update BACKEND_BUCKET \ --cache-key-query-string-whitelist user
Update cache keys to use HTTP headers
These instructions set Cloud CDN cache keys to use HTTP headers.
Console
The Google Cloud console is not supported for this feature.
gcloud
gcloud compute backend-services update BACKEND_SERVICE \ --cache-key-include-http-header=[HEADER_FIELD_NAME,...]
gcloud compute backend-buckets update BACKEND_BUCKET \ --cache-key-include-http-header=[HEADER_FIELD_NAME,...]
Update cache keys to use named cookies
These instructions set Cloud CDN cache keys to use HTTP cookies.
Console
The Google Cloud console is not supported for this feature.
gcloud
gcloud compute backend-services update BACKEND_SERVICE \ --cache-key-include-named-cookie=[NAMED_COOKIE,...]
What's next
- To check whether Cloud CDN is serving responses from cache, see Logs and metrics for caching.