When a CDN point of presence (POP) fetches a resource from your origin server, the origin returns an HTTP status code. By default, CDN uses the origin's cache headers to decide whether and how long to cache that status code. Status code TTL lets you override this behavior: set a custom cache duration per status code so POPs serve it directly to clients without performing a new origin fetch until the TTL expires.
How it works
A POP receives a request for a resource that is not in its cache. It fetches the resource from the origin, which returns a status code. If you have configured a TTL for that status code, the POP caches it for the configured duration. Within that duration, subsequent requests for the same resource receive the cached status code immediately — no origin fetch occurs. When the TTL expires, the next request triggers a new origin fetch.
Use case: A file is deleted from the origin server, but clients keep requesting it. Without a configured TTL, every request reaches the origin and receives a 404 response, increasing origin load. After you configure a 10-second TTL for 404, the POP caches the first 404 response and serves it directly for the next 10 seconds.
Default caching behavior
The following table summarizes how CDN handles abnormal status codes when no TTL is configured in the console and the origin returns no Set-Cookie header.
| Status code group | Default behavior (no console config, no Set-Cookie) |
|---|---|
| 204, 305, 404, 405, 414, 424, 429, 500, 501, 502, 503, 504 | Cached per Pragma, Cache-Control, or Expires header from origin. If none of these headers are present, cached for 1 second. |
| 302, 307, 403 | Cached per Pragma, Cache-Control, or Expires header from origin. If none of these headers are present, not cached. |
| 304 | Never cached. You cannot configure a TTL for this status code. |
| Other abnormal codes (e.g., 400) | Not cached unless you configure a TTL in the console. |
If the origin returns a Set-Cookie header, CDN does not cache the response regardless of the status code or any console configuration.Cache rules for abnormal status codes
Status codes 204, 305, 404, 405, 414, 424, 429, 500, 501, 502, 503, 504
If the origin returns a
set-cookieresponse header, CDN does not cache the response.If the origin does not return a Set-Cookie header, the response is cached for the duration you configured in the CDN console. For details on how multiple rules interact, see Priority of multiple rules.
If no Set-Cookie header is present and no TTL is configured in the console, the response is cached based on the
Pragma,Cache-Control, orExpiresresponse header from the origin.If none of the above apply — no Set-Cookie, no console config, and no
Pragma,Cache-Control, orExpiresheader — the response is cached for 1 second.
Status codes 302, 307, 403
If the origin returns a
set-cookieresponse header, CDN does not cache the response.If the origin does not return a Set-Cookie header, the response is cached for the duration you configured in the CDN console.
If no Set-Cookie header is present and no TTL is configured in the console, the response is cached based on the
Pragma,Cache-Control, orExpiresresponse header from the origin.If none of the above apply, the response is not cached (unlike the group above, there is no 1-second fallback).
Status code 304
CDN does not cache 304 responses. You cannot configure a TTL for this status code.
Other abnormal status codes (for example, 400)
If the origin returns a
set-cookieresponse header, CDN does not cache the response.If the origin does not return a Set-Cookie header, the response is cached for the duration you configured in the CDN console.
In all other scenarios, the response is not cached.
Range origin fetch behavior
In a range origin fetch, the origin splits a large file into smaller shards and returns them to the POP sequentially. If a POP receives a non-206 status code from the origin during a range origin fetch, all previously cached shards for that file are deleted.
Example: A file is split into 10 shards. The POP has cached five shards. When the POP requests the sixth shard, the origin returns a 5xx status code. CDN deletes all five previously cached shards.
An origin fetch timeout does not cause cached shards to be deleted.
Priority of multiple rules
When a request matches more than one cache rule for the same status code, only one rule takes effect. CDN determines the effective rule as follows:
Type takes precedence: File Extension rules take priority over Directory rules.
Creation time as tiebreaker: If multiple rules share the same type, the rule created first (shown higher in the rule list) takes effect.
The evaluation order is: File Extension > Directory, and for the same type, older > newer.
Create a status code cache rule
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud CDN domain name that is configured and active
Access to the CDN console
Steps
Log on to the CDN console.
In the left navigation pane, click Domain Names.
On the Domain Names page, find the target domain name and click Manage in the Actions column.
In the domain's navigation pane, click Cache.
Click the Status Code TTL tab.
Click Create Rule. Configure the following parameters:
Parameter Description Type Select Directory or File Extension. File Extension rules take priority over Directory rules. Object Directory: Enter the full path of a single directory. The path must start with a forward slash ( /). For example,/directory/aaa. File Extension: Enter one or more file extensions separated by commas. For example,jpg,txt. Rules are case-sensitive:jpgandJPGare treated as different extensions, and a later rule for the same extension (different case) overwrites the earlier one. Asterisks (*) are not supported.Expire In Specify status codes and their cache durations. The maximum duration is three years. The unit is seconds. Separate multiple status codes with commas. For 2xx and 3xx codes, specify individual codes only (for example, 201=10). Wildcard patterns such as2xx=12are not supported for 2xx and 3xx codes. For 4xx and 5xx codes, both individual codes (for example,401=10) and wildcard patterns (for example,4xx=12) are supported.Honor Origin TTL If enabled and the origin returns Cache-ControlorPragmaheaders, the origin's cache policy takes precedence over the configured TTL.Ignore Origin No-Cache Header If enabled, CDN POPs ignore the following headers from the origin: Cache-Control: no-store,Cache-Control: no-cache,Cache-Control: max-age=0, andPragma: no-cache.Follow POP Cache Policy If enabled, the CDN POP returns the final effective cache policy to the client. Force Revalidation Applies only when the cache duration is set to 0. Shutdown (default): CDN nodes do not cache the content. Each request performs an origin fetch. Enabled: CDN nodes cache the content, but each request performs an origin fetch to revalidate the cached content before serving it.
Click OK.
The rule appears in the list on the Expire In tab. To update or remove a rule, click Modify or Delete in the rule's row.
Configuration examples
Example 1: Directory rule

This rule targets the /directory/aaa directory. For requests in this directory, all 4xx status codes are cached for 10 seconds and the 201 status code is cached for 15 seconds. During these periods, POPs respond directly without performing an origin fetch. After the cache duration expires, the next request triggers an origin fetch.
Example 2: File extension rule

This rule targets .jpg and .txt files. For these files, the 403 status code is cached for 10 seconds and the 404 status code is cached for 15 seconds.
Example 3: Priority when rule types differ

A directory rule and a file extension rule are configured with different TTLs for the 404 status code. A client requests http://example.com/directory/aaa/test.jpg. The resource is not cached. The POP fetches it from the origin, which returns 404. The request matches both rules. Because File Extension > Directory, the file extension rule takes effect, and the 404 status code is cached for 20 seconds.
Example 4: Priority when rule types are the same

Directory Rule 1 targets /directory. Directory Rule 2 targets /directory/aaa. Both assign different TTLs to the 404 status code. A client requests http://example.com/directory/aaa/test.jpg. The request matches both directory rules. Because older > newer for rules of the same type, Directory Rule 1 (created first) takes effect, and the 404 status code is cached for 15 seconds.
API reference
To configure status code TTL using the API, use BatchSetCdnDomainConfig.