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 behavior 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 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.
HTTP status code 5xx allows the POPs to remove cached file chunks. If origin fetch times out, the file chunks are not removed.
NoteIf you enable range origin fetch, 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 do not configure range origin fetch, the following rules apply:
If an origin server returns the
Set-Cookie
response header, the POPs do not cache the HTTP status codes.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.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 thePragma
,Cache-Control
, orExpires
response header.If the origin server does not return the
Set-Cookie
,Pragma
,Cache-Control
, orExpires
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 prioritized based on their types. Cache rules that are configured for file 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 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 rule is configured for a file extension and the other is configured for a directory. In this case, the HTTP 404 status code expires based on the rule that is configured 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
Log on to the DCDN console.
In the left-side navigation pane, click Domain Names.
On the Domain Names page, find the domain name that you want to manage and click Configure in the Actions column.
In the left-side navigation tree of the domain name, click Caching.
Click the Status Code Expiration tab.
Click Create Rule, and configure the parameters.
Type
Item
Type
Select Directory or File Extension based on your business requirements.
NoteCache rules that are configured for file 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 extensions. Separate file extensions with commas (,). Example:
jpg,txt
.NoteIf different rules are configured 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 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 extensions in lowercase letters, you can create one rule for txt and another rule for jpg. File extensions are case-sensitive.
You cannot use the asterisk 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.
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.
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 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.