All Products
Search
Document Center

CDN:Configure TTL for HTTP status code

Last Updated:Jul 16, 2025

CDN points of presence (POPs) fetch resources from an origin server, and the origin server returns a response with an HTTP status code. You can configure a Time-to-Live (TTL) for these status codes on Alibaba Cloud CDN. When a client requests the same resource again, CDN returns the status code instead of redirecting the request to the origin server, which reduces the load on the origin server. After the TTL expires, requests are redirected to the origin server.

Scenarios

TTL values are used to control caching behaviors on the POPs if an origin server returns error codes.

If the requested resource is pulled from the origin server, a 2xx HTTP status code is returned and the resource is cached on POPs based on the Alibaba Cloud CDN/DCDN default cache rules and priorities.

Example

If you create a cache rule for HTTP 4xx status codes, the HTTP 4xx status code returned for the first request is cached on the POPs. Before the TTL of the HTTP status code ends, the POPs directly return the HTTP 4xx status code to subsequent requests.

Cache rules for HTTP status codes

  • The following figure shows the cache rules for HTTP status codes 204, 305, 400, 403, 404, 405, 414, 500, 501, 502, 503, and 504.状态码缓存过期时间

    • If requests are processed by the range origin fetch feature, the following cache rules apply:

      • For HTTP status codes other than 200 and 206, such as 204, 305, 400, 403, 404, 405, 414, 500, 501, 502, 503, and 504, resources are not cached.

        For HTTP status codes 200 and 206, resources are cached based on the cache rules that are described in Alibaba Cloud CDN/DCDN default cache rules and priorities.

      • An HTTP 5xx status code allows the POPs to remove cached file chunks. If origin fetch times out, the file chunks are not removed.

        Note

        If you enable the range origin fetch feature, origin servers divide a large file into chunks before the file is returned to POPs. For example, a file is divided into 10 chunks, 5 of which are cached on POPs. When a user requests the 6th chunk, the origin server returns an HTTP 5xx status code, and the POPs remove the 5 chunks that are cached.

    • If requests are not processed by Range origin fetch, the following cache rules apply:

      1. If an origin server returns the set-cookie response header, the POPs do not cache the retrieved resource.

      2. If the origin server does not return the Set-Cookie response header, the retrieved resource is cached based on the cache rules that are configured in the Alibaba Cloud CDN console. For more information, see How cache rules are applied.

      3. If the origin server does not return the Set-Cookie response header and no cache rules are configured in the CDN console, the retrieved resource is processed based on the directive of the Pragma, Cache-Control, or Expires response header.

      4. If the origin server does not return the Set-Cookie, Pragma, Cache-Control, or Expires response header and no cache rules are configured in the CDN console, the retrieved resource is cached for 1 second.

  • For HTTP 303, 304, 401, 407, 600, and 601 status codes, resources are not cached.

Priorities of multiple rules

You can configure multiple cache rules. If a request matches more than one cache rule, only one of the rules is applied. The rules are applied in the following order of priority:

  • Evaluation order:

    Rules are prioritized based on their types. Cache rules that are configured for file name extensions have a higher priority than cache rules that are configured for directories. Rules that are of the same type are prioritized based on their creation time. The rule that has an earlier creation time has a higher priority.

  • Rules of different types: Rules for file name extensions > Rules for directories.

    For example, a request matches 2 rules, and both rules configured the HTTP 404 status code. One rule is set for a file name extension and the other is for a directory. The HTTP 404 status code expires based on the rule that is configured for the file name extension. For more information, see Examples.

  • Rules of the same type: Earlier rule > Later rule (listed from top to bottom)

    For example, a request matches 2 rules and both rules use the HTTP 404 status code. Both rules are configured for a file name extension or a directory. In this case, the HTTP 404 status code expires based on the earlier rule. For more information, see Examples.

Procedure

  1. Log on to the Alibaba Cloud CDN console.

  2. In the left-side navigation pane, click Domain Names.

  3. On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column.

  4. In the left-side navigation tree of the domain name, click Cache.

  5. Click the Status Code Expiration tab.

  6. Click Create Rule to configure the status code expiration time.

    image

    Type

    Notes

    Type

    Select Directory or File Extension as needed.

    Note

    Cache rules that are configured for file name extensions have a higher priority than cache rules that are configured for directories. For more information, see Cache rules for HTTP status codes.

    Object

    • If you select Directory, take note of the following limits:

      • You can add only one directory to each rule.

      • You can enter a full path. The path must start with a forward slash (/). Example: /directory/aaa.

    • If you select File Extension, take note of the following rules:

      • You can enter one or more file name extensions. Separate multiple file name extensions with commas (,). Example: jpg,txt.

        Note

        If different rules are configured for the same file name extension that is specified in different letter cases, the most recent rule overwrites the earlier rule. For example, if Rule A is configured for JPG,TXT and Rule B is configured for jpg,txt, Rule B overwrites Rule A. If you want to create a rule for file name extensions that are specified in lowercase letters, you can create one rule for txt and another rule for jpg. File name extensions are case-sensitive.

      • You cannot use an asterisk (*) as a wildcard character to specify all file types.

    Expire In

    Specify an HTTP status code and a TTL. The TTL is in seconds. The maximum TTL is three years. Take note of the following rules:

    • Separate multiple HTTP status codes with commas (,).

    • For HTTP 2xx and 3xx status codes, you need to create a rule for each specific code. Fuzzy match is not supported. For example, 201=10 is valid but 2xx=12 is invalid.

    • For HTTP 4xx and 5xx status codes, exact match and fuzzy match are supported. For example, 401=10 and 4xx=12 are both valid.

    Honor Origin Cache Policy

    If you enable this option and the origin server returns cache policy headers (including Cache-Control and Pragma), the cache policy of the origin server takes precedence.

    Ignore Origin No-Cache Header

    If you enable this option, CDN POPs ignore the following cache policy headers returned by the origin server (these headers indicate no caching).

    • Cache-Control: no-store

    • Cache-Control: no-cache

    • Cache-Control: max-age=0

    • Pragma: no-cache

    Client Follows CDN Cache Policy

    If you enable this option, CDN POPs return the final effective cache policy to the client.

    Force Revalidation

    This parameter takes effect only when the cache expiration time is set to 0. The effects are as follows:

    • Disabled (default): When the cache expiration time of CDN is set to 0, files are not cached on CDN POPs, and each request needs to retrieve content from the origin server.

    • Enabled: When the cache expiration time of CDN is set to 0, files can be cached on CDN POPs, but each request needs to verify the cached content with the origin server.

  7. Click OK.

    After you create a cache rule for an HTTP status code, the cache rule is displayed on the Expire In tab. You can Modify or Delete the rule.

Examples

  • Example 1: Folder-type rule

    The following figure shows how to create a cache rule for a directory:示例一

    In the /directory/aaa directory, all HTTP 4xx status codes are cached for 10 seconds. The HTTP 201 status code is cached for 15 seconds. Before the cached HTTP status codes expire, the POPs can directly return the codes to requests. After the HTTP status codes expire, requests are redirected to the origin server.

  • Example 2: Create a cache rule for file name extensions

    The following figure shows how to create a cache rule for file name extensions:示例二

    For files whose extension is .jpg or .txt, the HTTP 403 status code is cached for 10 seconds, and the HTTP 404 status code is cached for 15 seconds. Before the cached HTTP status codes expire, the POPs can directly return the codes to requests. After the HTTP status codes expire, requests are redirected to the origin server.

  • Example 3: Priority of different types of rules

    Rule A is configured for a directory and Rule B is configured for file name extensions. The rules use different HTTP status codes, as shown in the following figure:示例三

    A user requests http://example.com/directory/aaa/test.jpg. The resource is not cached on the POPs, and the origin server returns an HTTP 404 status code. This request matches both a directory rule and a file name extension rule. The priority is File name extension > Directory, the file name extension rule is applied, and the HTTP 404 status code is cached for 20 seconds.

  • Example 4: Priority of multiple rules of the same type

    Rule C is configured for the /directory and Rule D is configured for /directory/aaa. The rules use different HTTP status codes:示例四

    When a client sends a request to http://example.com/directory/aaa/test.jpg, the requested resource is not cached on POPs. The request is redirected to the origin server. The origin server returns the HTTP 404 status code. In this case, the request matches Rule C and Rule D. Rules of the same type are prioritized based on creation time. The earlier rule has a higher priority. As a result, the HTTP 404 status code is cached for 15 seconds.

Related API operation

BatchSetCdnDomainConfig