POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/BACKEND_BUCKET
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/BACKEND_SERVICE
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/BACKEND_BUCKET
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices
PUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/BACKEND_SERVICE
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-05 (世界標準時間)。"],[[["\u003cp\u003eNegative caching allows setting distinct Time-To-Live (TTL) values for various HTTP status codes, offering fine-grained control over caching errors and redirects.\u003c/p\u003e\n"],["\u003cp\u003eConfiguring negative caching can reduce origin load and enhance user experience by minimizing response latency for common errors.\u003c/p\u003e\n"],["\u003cp\u003eDefault TTL values are provided for specific status codes like 300, 301, 308, 404, 405, 410, 451, and 501, but can be overridden using negative caching policies.\u003c/p\u003e\n"],["\u003cp\u003eNegative caching can be enabled or disabled through the Google Cloud console, \u003ccode\u003egcloud\u003c/code\u003e CLI, or API, and it requires specifying TTL values for all response codes to be cached.\u003c/p\u003e\n"],["\u003cp\u003eWhen negative caching is active, Cloud CDN manages the \u003ccode\u003eCache-Control\u003c/code\u003e header sent to clients, including setting \u003ccode\u003emax-age\u003c/code\u003e based on configured TTL values.\u003c/p\u003e\n"]]],[],null,["# Use negative caching\n\nThis page provides instructions for using negative caching with\nCloud CDN. Negative caching lets you set a different TTL for each status\ncode.\n\nThe reason to do this is to apply fine-grained control over caching for common\nerrors or redirects. This can reduce the load on your origins and improve the\nend-user experience by reducing response latency.\n\nBefore you begin\n----------------\n\n- Read about [cache modes and static content](/cdn/docs/caching#cache-modes).\n\n- Ensure that Cloud CDN is enabled; for instructions, see\n [Using Cloud CDN](/cdn/docs/using-cdn).\n\n- If necessary, update to the latest version of the Google Cloud CLI:\n\n ```\n gcloud components update\n ```\n | **Note:** Make sure that you're using gcloud CLI version `316.0.0` or later.\n\nStatus codes and default TTLs\n-----------------------------\n\nNegative caching applies to specific status codes, which are listed in the\nfollowing table.\n| **Note:** The TTL values for HTTP success codes (HTTP 2xx) are controlled by the values of [default TTL and max TTL](/cdn/docs/using-ttl-overrides).\n\nCloud CDN applies the following default TTLs to these status codes:\n\nYou can override these default values by using negative caching to set a cache\nTTL for the specified HTTP status code.\n\nSetting up negative caching\n---------------------------\n\nNegative caching lets you configure your service to cache failures as well as\nsuccesses. This allows Cloud CDN to shield origins from requests that\ngenerate errors (like 404 Not Found) in the same way that it shields origins\nfrom requests that generate successful responses. \n\n### Console\n\n1. In the Google Cloud console, go to the **Load Balancing** page.\n\n [Go to the Load balancing page](https://console.cloud.google.com/networking/loadbalancing/list)\n2. Click the name of your external Application Load Balancer.\n3. Click **Edit** edit.\n4. In **Backend configuration** , select a backend and click **Edit** edit.\n5. Make sure that **Enable Cloud CDN** is selected.\n6. At the bottom of the window, click **Advanced configurations**.\n7. Click **Enable negative caching**.\n8. Click **Add negative caching policy** .\n 1. Enter an **HTTP status code**.\n 2. Select a **Cache time to live (TTL)**.\n9. Click **Update**.\n10. Click **Update** again.\n\n### gcloud\n\nFor backend buckets, use the [gcloud compute backend-buckets\ncreate](/sdk/gcloud/reference/compute/backend-buckets/create) or\n[gcloud compute backend-buckets\nupdate](/sdk/gcloud/reference/compute/backend-buckets/update) command\nwith the `--negative-caching` flag.\n\nFor backend services, use the [gcloud compute backend-services\ncreate](/sdk/gcloud/reference/compute/backend-services/create) or\n[gcloud compute backend-services\nupdate](/sdk/gcloud/reference/compute/backend-services/update) command\nwith the `--negative-caching` flag. \n\n```\ngcloud compute backend-buckets (create | update) BACKEND_BUCKET_NAME\n --negative-caching\n``` \n\n```\ngcloud compute backend-services (create | update) BACKEND_SERVICE_NAME\n --negative-caching\n```\n\nTo enable negative caching of only two specific error responses,\nfor example, set responses with status code `404` to be cached for 60\nseconds, and responses with status code `405` to be cached for 120 seconds. \n\n```\ngcloud compute backend-services update BACKEND_SERVICE_NAME \\\n --negative-caching \\\n --cache-mode=CACHE_ALL_STATIC \\\n --default-ttl=86400 \\\n --negative-caching-policy='404=60,405=120'\n```\n\n### api\n\nFor backend buckets, use the\n[Method: backendBuckets.insert](/compute/docs/reference/rest/v1/backendBuckets/insert) or\n[Method: backendBuckets.update](/compute/docs/reference/rest/v1/backendBuckets/update)\nAPI call.\n\nFor backend services, use the\n[Method: backendServices.insert](/compute/docs/reference/rest/v1/backendServices/insert) or\n[Method: backendServices.update](/compute/docs/reference/rest/v1/backendServices/update)\nAPI call.\n\nUse one of the following API calls: \n\n```\nPOST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets\nPUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/BACKEND_BUCKET\nPOST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices\nPUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/BACKEND_SERVICE\n```\n\nAdd the following snippet to the JSON request body: \n\n```\n\"cdnPolicy\": {\n \"negativeCaching\": ON,\n \"negativeCachingPolicy\": [\n {\n \"code\": STATUS_CODE,\n \"ttl\": TTL_SECONDS\n }\n ]\n}\n```\n\nNegative caching must be enabled to configure the `negativeCachingPolicy`\nsettings. If you omit the policy and have `negativeCaching` enabled,\nCloud CDN uses the default values listed in [Status codes and default\nTTLs](#status-code-default-ttl).\n\nWhen specifying a negative caching policy, make sure to specify a cache TTL for\n*all* response codes that you want to cache. Cloud CDN doesn't apply\nany default negative caching when a policy exists.\n\nFor \u003cvar translate=\"no\"\u003eSTATUS_CODE\u003c/var\u003e, you can specify the following HTTP status codes:\n\n- `300`, `301`, `302`, `307`, `308`\n- `404`, `405`, `410`, `421`, `451`\n- `501`\n\nFor each status code, you can specify a number of seconds to cache responses.\nTo disable negative caching for the status code, exclude the code from your\nnegative caching policy.\n| **Note:** Explicitly including a code and setting the TTL to `0` disables caching, but doing so is not recommended because the use of a TTL value of `0` in this context is different from other uses in Cloud CDN. For example, a `max-age=0` directive in a response `cache-control` header means that the cache must always revalidate the response, rather than that the response is not cacehable.\n\nThe maximum allowed value is 1800 seconds (30 minutes); however, infrequently\naccessed objects might be evicted from the cache before the defined TTL.\n\nWhen the cache mode is set to `CACHE_ALL_STATIC` or `USE_ORIGIN_HEADERS`,\nnegative caching is applied to responses with the specified response code that\nlack any `Cache-Control` or `Expires` headers.\n\nWhen the cache mode is set to `FORCE_CACHE_ALL`, negative caching overrides any\ncaching headers set by the origin, and Cloud CDN will cache the\nresponse for the length of time specified by the TTL, or will not cache it at\nall if no TTL is set.\n\nIn addition, when the cache mode is set to `FORCE_CACHE_ALL`, Cloud CDN\nwill also modify the `max-age` sent to the client in the `Cache-Control`\nheader. In particular, if a given error has a configured TTL setting,\nCloud CDN will take the lesser of that TTL setting and the `client_ttl`\nconfiguration setting, and send a `Cache-Control: public,max-age=N` header with\nthat value. If a given error does not have a configured TTL setting,\nCloud CDN will remove any `Cache-Control` header sent by the\norigin. Cloud CDN will also always remove any `Expires` header sent by\nthe origin.\n\nIf an origin has negative caching initially enabled but then disabled (either\nmanually or by disabling caching for a particular response code), cached error\nresponses are considered valid based only on their `Cache-Control` or `Expires`\nheaders. Therefore, once negative caching is disabled, a response with no\ncaching age directives in its headers is not served from cache.\n\nCloud CDN caches in response to `GET` requests. For more information, see\n[Cacheable content](/cdn/docs/caching#cacheability).\n\nEach cache entry is identified by a [cache key](/cdn/docs/caching#cache-keys).\n\n### Disable negative caching\n\n### Console\n\n1. In the Google Cloud console, go to the **Load Balancing** page.\n\n [Go to the Load balancing page](https://console.cloud.google.com/networking/loadbalancing/list)\n2. Click the name of your external Application Load Balancer.\n3. Click **Edit** edit.\n4. In **Backend configuration** , select a backend and click **Edit** edit.\n5. Make sure that **Enable Cloud CDN** is selected.\n6. At the bottom of the window, click **Advanced configurations**.\n7. Clear the checkbox next to **Enable negative caching**.\n8. Click **Update**.\n9. Click **Update** again.\n\n### gcloud\n\nFor backend buckets, use the [gcloud compute backend-buckets\ncreate](/sdk/gcloud/reference/compute/backend-buckets/create) or\n[gcloud compute backend-buckets\nupdate](/sdk/gcloud/reference/compute/backend-buckets/update) command\nwith the `--no-negative-caching` flag.\n\nFor backend services, use the [gcloud compute backend-services\ncreate](/sdk/gcloud/reference/compute/backend-services/create) or\n[gcloud compute backend-services\nupdate](/sdk/gcloud/reference/compute/backend-services/update) command\nwith the `--no-negative-caching` flag. \n\n```\ngcloud compute backend-services (create | update) (BACKEND_SERVICE_NAME | BACKEND_BUCKET_NAME)\n --no-negative-caching\n```\n\n### api\n\nFor backend buckets, use the\n[Method: backendBuckets.insert](/compute/docs/reference/rest/v1/backendBuckets/insert) or\n[Method: backendBuckets.update](/compute/docs/reference/rest/v1/backendBuckets/update)\nAPI call.\n\nFor backend services, use the\n[Method: backendServices.insert](/compute/docs/reference/rest/v1/backendServices/insert) or\n[Method: backendServices.update](/compute/docs/reference/rest/v1/backendServices/update)\nAPI call.\n\nUse one of the following API calls: \n\n```\nPOST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets\nPUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendBuckets/BACKEND_BUCKET\nPOST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices\nPUT https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/backendServices/BACKEND_SERVICE\n```\n\nAdd the following snippet to the JSON request body: \n\n```\n\"cdnPolicy\": {\n \"negativeCaching\": OFF\n}\n```"]]