You can create rules to generate cache keys based on different parts of HTTP requests, such as URIs, request parameters, HTTP request headers, and custom variables. You can also use this feature to convert URLs for the same resource into the same cache key. This improves the cache hit ratio, and reduces the number of requests that are redirected to the origin server, response time, and bandwidth usage.
Scenarios
Custom cache keys do not modify origin URLs. Custom cache keys modify only the cache identifiers in the requests. Requests that are redirected to origin servers and that are sent by clients are destined for the same content.
A cache key is the unique ID of a file that is cached on a point of presence (POP). Each file that is cached on POPs has a cache key. By default, the cache key of a file is the URL in the request that is sent to retrieve the file. The URL contains parameters.
Scenario 1
The URLs in requests may contain complex parameters. Therefore, even if the requests are sent to retrieve the same content, due to the variation of URL parameters, POPs consider that the requests are sent to retrieve different resources and then cache resources for all requests. As a result, a large number of requests are redirected to the origin server.
You can set a cache key for a type of requests to reduce the number of requests redirected to the origin server.
Scenario 2
The requests contain the same URL. In this case, DCDN considers that the requests are sent to retrieve the same resource. However, in fact, the client OSs are specified in the client field of the HTTP header in the requests. The requests may be sent to retrieve different files.
You can create two cache keys, include the values of the client field in the cache keys, and then use the cache keys to distinguish the requests.
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 whose acceleration region you want to change and click Configure.
In the left-side navigation tree of the domain name, click Caching.
On the Custom Cache Key tab, configure a cache key.
NoteYou can modify the URIs, parameter actions, HTTP headers, and custom variables in cache keys. A cache key consists of URIs, parameter actions, HTTP headers, and custom variables.
Parameter
Description
URIs
Source URI, which starts with a forward slash (/). Source URIs do not contain http:// or domain names. Perl Compatible Regular Expressions (PCRE) are supported in source URIs.
Final URI, which starts with a forward slash (/). Final URIs do not contain http:// or domain names. Final URIs are part of cache keys.
Parameter Actions
Parameter actions are included in the request URLs. You can specify the Add, Delete, Modify, and Reserve actions. After the specified actions are performed, the result becomes part of a cache key.
HTTP Headers
HTTP headers are included in user requests. You can add multiple HTTP header values to a cache key.
Custom Variables
You can use a regular expression to extract fields from request parameters, HTTP headers, cookies, and URIs, and then add the extracted fields to a cache key.
Click OK.
Sample configurations
URIs
Requests that are destined for URIs http://aliyundoc.com/a/b/image.jpg
and http://aliyundoc.com/a/b/c/image.jpg
are considered to request the same file. The cache key of the file is http://aliyundoc.com/c/image.jpg
.
Parameter Actions
For requests that are destined for http://aliyundoc.com/a/b/image.jpg?delete_par=1&modify_par=1
, add_par=1
is added to the URI, delete_par
is removed, and the value of modify_par
is changed to 2
. In this case, the final URI is http://aliyundoc.com/a/b/image.jpg?modify_par=2&add_par=1
.
The priorities of actions that are applied to the same variable are: Add > Delete > Retain Only > Modify.
HTTP Headers
In this example, the values of the User-Agent
and Accept-Language
fields in the HTTP header are included in cache keys. For example, if a request destined for http://aliyundoc.com/a/b/image.jpg
includes the User-Agent=Mozilla/5.0 (Linux; X11)
and Accept-Language=en
headers, the cache key of the request is http://aliyundoc.com/a/b/image.jpgMozilla/5.0(Linux;X11)en
.
Custom Variables
Example 1
The variable name is language
, the source is Request Header
, the source field is Accept-Language
, the matching rule is ([%w]+),([%w]+)
, and the variable expression is $1aa
.
If a request destined for http://aliyundoc.com/a/b/image.jpg
carries the header Accept-Language=en,ch
, the matching rule assigns the value en
to $1
in the expression. The string aa
is appended to the end of the variable expression. In this case, the enaa
variable with the alias language
is generated. Then, the final cache key is http://aliyundoc.com/a/b/image.jpgenaa
.
$n
in the variable expression specifies the matched content that is enclosed in the n
-th bracket. In Example 1, Accept-Language=en,ch
and the matching rule is ([%w]+),([%w]+)
. Therefore, $1=en
and $2=ch
.
Example 2
The variable name is expired
, the source is Request Cookie
, the source field is a
, the matching rule is [%w]+:(.*)
, and the variable expression is $1
.
If a request destined for http://aliyundoc.com/a/b/image.jpg
carries the header Cookie a=expired_time:12635187
, the matching rule assigns 12635187
to $1
in the variable expression. The alias of the variable is expired
. Then, the final cache key is http://aliyundoc.com/a/b/image.jpg12635187
.
Example 3
Configure URI rules and custom variables at the same time.
URIs
Transform
/abc/.*/abc
in all request URIs into/abc
.Custom Variables
The variable name is
testname
, the source isPath
, the matching rule is/abc/xyz/(.*)
, and the variable expression is$1
.If a request is destined for
http://aliyundoc.com/abc/xyz/abc/image.jpg
, the URL is transformed intohttp://aliyundoc.com/abc/image.jpg
based on the URI rule. The URL matches/abc/xyz/(.*)
according to the custom variable rule. In this case,$1
is assigned the valueabc
and then appended to the cache key. The final cache key ishttp://aliyundoc.com/abc/image.jpgabc
. Two rules are applied at the same time to support complex caching logic.If the custom variable of the cache key is not matched, the variable expression
$1
is not appended to the cache key.