When Alibaba Cloud CDN edge nodes retrieve resources from origin servers, the origin servers return HTTP status codes to the nodes. Alibaba Cloud CDN allows you to create cache rules for HTTP status codes. After HTTP status codes are cached on the edge nodes, the nodes can respond to requests that trigger the cached HTTP status codes instead of redirecting the requests to the origin servers. This reduces loads on origin servers. When the time-to-live (TTL) set for an HTTP status code ends, the HTTP status code expires on the edge nodes. Requests destined for the resource are redirected to the origin server.

Scenarios

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

If an edge node retrieves the requested resource from the origin server, an HTTP 2xx status code is returned, as described in Priorities of Alibaba Cloud CDN cache rules. If the origin server cannot respond to requests by returning HTTP status codes, such as non-HTTP 2xx status codes, to the edge nodes, and you do not want the origin server to respond to every request, you can set a TTL for HTTP status codes to cache them on the edge nodes. Then, the edge nodes can return HTTP status codes to requests so that loads on the origin server can be reduced.

Example: File A has been removed from the origin server and is not cached on the edge nodes, but clients attempt to access File A. All requests for File A are redirected to the origin server. The origin server must return an HTTP 4xx status code. This increases loads on the origin server. 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 edge nodes. Before the TTL of the HTTP status code ends, the edge nodes 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.TTL values for HTTP status codes
    • If you enable the Object chunking feature, the following cache rules apply:
      • HTTP status codes other than 200 and 206, such as 204, 305, 400, 403, 404, 405, 414, 500, 501, 502, 503, and 504, are not cached.

        HTTP status codes 200 and 206 are cached based on the cache rules described in Priorities of Alibaba Cloud CDN cache rules.

      • An HTTP 5xx status code enables the edge nodes to remove cached file chunks. If back-to-origin routing times out, the files chucks are not removed.
        Note If object chunking is enabled, origin servers divide a large file into chunks before the file is returned to the edge nodes. For example, a file is divided into 10 chunks, 5 of which have already been cached on the edge nodes. When a user requests the sixth chunk, the origin server returns an HTTP 5xx status code, and the edge nodes remove the 5 chunks that are already cached.
    • If you disable the Object chunking feature, HTTP status codes are cached based on the following cache rules:
      1. If an origin server returns the Set-Cookie response header, the edge nodes 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 set in the Alibaba Cloud CDN console. For more information about how the cache rules are applied, see How cache rules are applied.
      3. If the origin server does not return the Set-Cookie response header, and no cache rules are set 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 set in the Alibaba Cloud CDN console, the retrieved resource is cached for 1 second.
  • HTTP 303, 304, 401, 407, 600, and 601 status codes cannot be cached.

How cache rules are applied

You can set 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 first prioritized based on their types. Cache rules that are set for file extensions have a higher priority than cache rules that are set for directories. Then, the rules that are of the same type are prioritized based on their creation time. The earlier rule has a higher priority.

  • Rules of different types: Rules set for file extensions have a higher priority than rules set for directories .

    For example, a request matches two rules, and both rules use the HTTP 404 status code. One of the rules is set for a file extension and the other is set for a directory. In this case, the HTTP 404 status code expires based on the rule that is set for the file extension. For more information, see Configuration examples.

  • Rules of the same type: The earlier rule has a higher priority. Rules are listed in order of creation time in the console.

    For example, a request matches two rules, and both rules use the HTTP 404 status code. Both rules are set for a file 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 management pane of the domain name, click Cache.
  5. Click the Status Code Expiration tab.
  6. Click Create Rule to create a cache rule for HTTP status codes.
    Create cache rule for HTTP status codes
    Type Usage notes
    Type You can select Directory or File Extension. Select a type based on your business requirements.
    Note Cache rules that are set for file extensions have a higher priority than cache rules that are set 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 in each rule.
      • You can enter a full path. The path must start with a forward slash (/), for example, /directory/aaa.
    • If you select File Extension, take note of the following rules:
      • You can enter one or more file extensions. Separate file extensions with commas (,), for example, JPG,TXT.
        Note If different rules are set for the same file extension that is specified in different letter cases, the most recent rule overwrites the earlier rule. For example, if Rule A is set for JPG,TXT, and later Rule B is set for jpg,txt, Rule B overwrites Rule A. If you want to create a rule for the file extensions in lowercase letters, you can create one rule for txt and another for jpg. File extensions are case-sensitive.
      • You cannot use an asterisk (*) to specify all file types.
    Expire In Specify an HTTP status code and a TTL. The TTL is measured 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 must 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, both exact match and fuzzy match are supported. For example, both 401=10 and 4xx=12 are valid.
  7. Click OK.

    After a cache rule for an HTTP status code is created, 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.Example 1

    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 edge nodes 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 extensions

    The following figure shows how to create a cache rule for file extensions.Example 2

    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 codes expire, requests for jpg or txt files are redirected to the origin server.

  • Example 3: Create cache rules of different types

    Rule A is set for a directory and Rule B is set for file extensions. The rules use different HTTP status codes, as shown in the following figure.Example 3

    When a client sends a request to http://example.com/directory/aaa/test.jpg, the requested resource is not cached on the edge nodes. The request is redirected to the origin server. The origin server returns the HTTP 404 status code. In this case, the request matches both Rule A and Rule B. However, cache rules that are set for file extensions have a higher priority than cache rules that are set for directories. As a result, the HTTP status code is cached for 20 seconds.

  • Example 4: Create cache rules of the same type

    Rule C is set for the directory /directory and Rule D is set for the directory /directory/aaa. The rules use different HTTP status codes, as shown in the following figure.Example 4

    When a client sends a request to http://example.com/directory/aaa/test.jp, the requested resource is not cached on the edge nodes. The request is redirected to the origin server. The origin server returns the HTTP 404 status code. In this case, the request matches both Rule C and Rule D. Rules of the same type are prioritized in order of 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