All Products
Search
Document Center

CDN:Create a cache rule for HTTP status codes

Last Updated:Feb 29, 2024

When points of presence (POPs) retrieve resources from origin servers, the origin servers return HTTP status codes to the POPs. Alibaba Cloud CDN allows you to create cache rules for HTTP status codes. When clients request the same resource, the POPs return the status code instead of redirecting requests to the origin server. This reduces loads on origin servers. After a cached HTTP status code expires, requests that trigger the code 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.

Normally, if the requested resource is retrieved from the origin server, an HTTP 2xx status code is returned and the resource is cached on POPs based on the cache rule as described in Default cache rule and priorities of cache rules. If the origin server cannot respond to requests by returning HTTP status codes, such as non-HTTP 2xx status codes, to the POPs and you do not want the origin server to respond to every request, you can specify a TTL for HTTP status codes to cache the codes on the POPs. This way, the POPs can directly return HTTP status codes to clients. This reduces loads on the origin server.

Example

File A is removed from the origin server and is not cached on the POPs, but clients attempt to access File A. All requests for File A are redirected to the origin server, and the origin server must return an HTTP 4xx status code for each request. This increases loads on the origin server. If you create a cache rule for HTTP 4xx status codes, the HTTP 4xx status code that is returned for the first request is cached on the POPs. Before the TTL of the HTTP status code ends, the POPs 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 you enable the range origin fetch feature that is described in Range origin fetch, 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 Default cache rule and priorities of cache rules.

      • 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 you disable the range origin fetch feature, 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 Alibaba Cloud 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 Alibaba Cloud 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.

How cache rules are applied

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

  • Rule priorities:

    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 use the HTTP 404 status code. One rule is configured for a file name extension and the other is configured 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 Configuration examples.

  • Rules of the same type: Earlier rule > Later rule

    Rules are listed in order of creation time in the console. 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 Configuration 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 and configure the parameters. The following table describes the parameters.

    配置文件和目录状态码过期时间

    Parameter

    Description

    Type

    Select Directory or File Extension based on your business requirements.

    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 valid.

  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.

Configuration examples

  • Example 1: Create a cache rule for a directory

    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, POPs can 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. After the HTTP 403 or 404 status code expires, requests for .jpg or .txt files are redirected to the origin server.

  • Example 3: Create cache rules of different types

    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.示例三

    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 A and Rule B. However, cache rules that are configured for file name extensions have a higher priority than cache rules that are configured for directories. As a result, the HTTP 404 status code is cached for 20 seconds.

  • Example 4: Create cache rules of the same type

    Rule C is configured for the directory /directory and Rule D is configured for the directory /directory/aaa. The rules use different HTTP status codes, as shown in the following figure.示例四

    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 operations

BatchSetCdnDomainConfig