Alibaba Cloud CDN allows you to create cache keys for requests that are destined for the same resource file. This helps you save the cache space and reduces the number of requests redirected to the origin server. This topic describes how to create custom cache keys.
Feature overview
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 of the request that is sent to retrieve the file. The URL includes parameters.
Scenario 1:


Scenario 2:


Procedure
- Log on to the Alibaba Cloud CDN console.
- On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column.
- In the left-side navigation pane of the domain name, click Cache.
- On the Custom Cache Key tab, click Configure. Note You can modify the URIs, request parameters, HTTP headers, and custom variables in cache keys. A final cache key consists of URIs, request parameters, HTTP headers, and custom variables.
Parameter Description URIs - Source URI: Enter a URI that starts with a forward slash (/). The URI cannot contain http:// or domain names. Perl Compatible Regular Expressions (PCRE) are supported in source URIs.
- Final URI: Enter a URI that starts with a forward slash (/). The URI cannot contain http:// or domain names. Final URIs are part of cache keys.
Request Parameter Parameter actions are included in the request URLs. You can specify the Add, Delete, Modify, and Retain 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
http://aliyundoc.com/a/b/image.jpg
and http://aliyundoc.com/a/b/c/image.jpg
are considered to retrieve the same file. The cache key of the file is http://aliyundoc.com/c/image.jpg
. 
Request Parameter
http://aliyundoc.com/a/b/image.jpg?delete_par=1&modify_par=1
, the add_par=1
parameter is added to the URI, the delete_par
parameter is removed from the URI, and the value of the modify_par
parameter is changed to 2
. In this case, the final URI is http://aliyundoc.com/a/b/image.jpg?modify_par=2&add_par=1
. 
HTTP Headers
User-Agent
and Accept-Language
fields in the HTTP header are included in cache keys. For example, if a request that is destined for http://aliyundoc.com/a/b/image.jpg
carries 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
language
. The source of the variable is Request Header
, the source field of the variable is Accept-Language
, the match rule is ([%w]+),([%w]+)
, and the variable expression is $1aa
. 
http://aliyundoc.com/a/b/image.jpg
carries the HTTP header Accept-Language=en,ch
, the match rule assigns the value en
to $1
in the variable expression. The string aa
is also appended to the end of the variable expression. In this case, variable enaa
with the alias language
is generated. After you append the variable to the URL, the final cache key is http://aliyundoc.com/a/b/image.jpgenaa
. $n
in the variable expression refers to the matched content that is enclosed in the No. n
brackets. In Example 1, Accept-Language=en,ch
and the match rule is ([%w]+),([%w]+)
. Therefore, $1=en
, and $2=ch
. Example 2
expired
. The source of the variable is Request Cookie
, the source field is a
, the match rule is [%w]+:(.*)
, and the variable expression is $1
. 
If a request that is destined for http://aliyundoc.com/a/b/image.jpg
carries Cookie a=expired_time:12635187
, the match rule assigns the value 12635187
to $1
in the variable expression. The alias of the variable is expired
. The final cache key is http://aliyundoc.com/a/b/image.jpg12635187
.
Example 3
Configure URI rules and custom variables at the same time
URI:
/abc/.*/abc
in all request URIs into /abc
. 
Custom variable:
testname
. The source of the variable is Path
, the match 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 to http://aliyundoc.com/abc/image.jpg
based on the URI rule. The URL matches /abc/xyz/(.*)
based on the custom variable rule. In this case, $1
is assigned the value abc
and then appended to the cache key. The final cache key is http://aliyundoc.com/abc/image.jpgabc
. Two rules are applied at the same time to support complex caching logic.
If no custom variable matches the cache key, the variable expression $1
is not added to the cache key.