All Products
Search
Document Center

Dynamic Content Delivery Network:Create a cache rule for HTTP status codes

Last Updated:Mar 06, 2024

When points of presence (POPs) retrieve resources from origin servers, the origin servers return HTTP status codes to the POPs. Dynamic Content Delivery Network (DCDN) 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

Time-to-live (TTL) values for status codes are used to control caching behaviors on POPs when an origin server returns status codes.

Normally, if a POP retrieves the requested resource from the origin server, status code 2xx is returned and the POP processes the retrieved resource as described in Default cache rule and priorities of cache rules. If the origin server cannot quickly respond to requests by returning HTTP status codes other than status code 2xx, and you do not want the origin server to respond to all requests, you can specify a TTL for HTTP status codes. Then, the DCDN POPs can directly return HTTP status codes to requests. This way, loads on the origin server can be alleviated.

Typical scenario

File A is removed from the origin server. Clients attempt to access the file, which is not cached on POPs. Then all requests for file A are redirected to the origin server. The origin server must return HTTP status code 4xx to each request. This burdens the origin server. If you create a cache rule for HTTP status code 4xx, HTTP status code 4xx returned for the first request is cached on DCDN POPs. Before the TTL of HTTP status code ends, DCDN POPs directly return HTTP status code 4xx 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 Configure range origin fetch, 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 that are described in Create a cache rule for resources.

      • An HTTP 5xx status code allows the POPs to remove cached file chunks. If back-to-origin routing 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, in which 5 chunks have been cached on the POPs. When a user requests the 6th chunk, the origin server returns an HTTP 5xx status code, and the POPs remove the cached chunks.

    • If you disable the range origin fetch feature that is described in Configure 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 HTTP status codes.

      2. If the origin server does not return the Set-Cookie response header, HTTP status codes are cached based on the cache rules that are configured in the DCDN console. For more information about how the cache rules are applied, see Cache rule prioritization.

      3. If the origin server does not return the Set-Cookie response header and no cache rules are configured in the DCDN console, HTTP status codes are cached based on the configuration 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 DCDN console, HTTP status codes are cached for 1 second.

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

Cache rule prioritization

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 configured for file name extensions have a higher priority than cache rules that are configured for directories. Then, the 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 set for file extensions have a higher priority than rules set for directories.

    For example, a request matches 2 rules, and both rules use HTTP status code 404. One rule is set for a file extension and the other is set for a directory. In this case, HTTP status code 404 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 2 rules, and both rules use HTTP status code 404. Both rules are set for a file extension or a directory. In this case, HTTP status code 404 expires based on the earlier rule. For more information, see Configuration examples.

Procedure

  1. Log on to the DCDN 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 Configure in the Actions column.

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

  5. Click the Status Code Expiration tab.

  6. Click Create Rule, and configure the parameters.

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

    Type

    Usage notes

    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 rule prioritization.

    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 (/). Example: /directory/aaa.

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

      • 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 then Rule B is configured for jpg,txt, Rule B overwrites Rule A. If you want to create a rule for the file name extensions 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 status codes 2xx and 3xx, 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 status codes 4xx and 5xx, exact match and fuzzy match are supported. For example, 401=10 and 4xx=12 are both 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.示例一

    For 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, DCDN 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 extensions

    The following figure shows how to create a cache rule for file 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, DCDN POPs can directly return the codes to requests. After the HTTP 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 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 DCDN POPs. 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 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 DCDN POPs. 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

BatchAddDcdnDomain