Alibaba Cloud Content Delivery Network (CDN) allows you to set a cache key for a type of request that is destined for the same resource file. This can help you save the cache space and reduce the number of requests redirected to the origin server. This topic describes how to customize cache keys.

Features

A cache key is the unique ID of a file cached on a CDN node. Each file cached on CDN nodes has a cache key. By default, the cache key of a file is the URL (including parameters) of the request sent to retrieve the file.

Scenario 1:

The URLs in requests may contain complex parameters, even if they are submitted to retrieve the same resource file. Due to the variation of URL parameters, CDN nodes treat these requests as requests sent for retrieving different resource files and then cache resource files for all of them. As a result, a large number of requests are redirected to the origin server. Figure 1
You can set a cache key for a type of request to reduce the number of requests redirected to the origin server. Figure 2

Scenario 2:

The requests carry the same URL. In this case, Alibaba Cloud CDN treat these requests as requests sent to retrieve the same resource file. However, different client operating systems are specified in the client field of the HTTP header in these requests. You may need to return different resource files based on the specified operating system. Case 1
To resolve this problem, you can set two cache keys and include the value of the client field in the cache keys, and then use the cache keys to distinguish the requests. Case 2

Procedure

  1. Log on to the Alibaba Cloud CDN console.
  2. On the Domain Names page, find and click the domain name that you want to manage or click Manage in the Actions column.
  3. In the left-side navigation tree, click Cache.
  4. On the Custom Cache Key tab, set a cache key.
    Note Each cache key consists of URIs, parameter actions, HTTP headers, and custom variables. You can modify the URIs, parameter actions, HTTP headers, and custom variables in cache keys.
    Custom cache keys
  5. Click OK.

Examples

URIs

In this example, requests destined for URIs http://yourdomain.com/a/b/test.jpg and http://yourdomain.com/a/b/c/test.jpg are considered requests sent to retrieve the same resource file. The cache key of the resource file is http://yourdomain.com/c/test.jpg. URI

Parameter Actions

For requests destined for the URI http://yourdomain.com/a/b/test.jpg?delete_par=1&modify_par=1 , the parameter add_par=1 is added to the URI, the parameter delete_par is deleted from the URI, and the value of the parameter modify_par is changed to 2. Therefore, the URI is changed to http://yourdomain.com/a/b/test.jpg?modify_par=2&add_par=1.
Notice For parameter actions, the priorities of rules applied to the same variable are as follows: Add > Delete > Retain Only > Modify.
Parameter actions

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://yourdomain.com/a/b/test.jpg carries User-Agent=Mozilla/5.0 (Linux; X11) and Accept-Language=en in its header, the cache key of the request is http://yourdomain.com/a/b/test.jpgMozilla/5.0(Linux;X11)en. HTTP Header

Custom Variables

Example 1

The name of the variable is 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. Custom variables
If a request destined for http://yourdomain.com/a/b/test.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. Consequently, variable enaa with the alias language is generated. After CDN appends the variable to the URL, the final cache key is: http://yourdomain.com/a/b/test.jpgenaa.
Note The definition of $n in the variable expression refers to the matched content 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

The name of the custom variable is expired. The source of the variable is Request Cooike, the source field is a, the match rule is [%w]+:(. *), and the variable expression is $1. Custom variables

If a request destined for http://yourdomain.com/a/b/test.jpg carries the Cookie a=expired_time:12635187 cookie, 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://yourdomain.com/a/b/test.jpg12635187.

Example 3

Set URI rules and custom variables at the same time

URI:

Transform /abc/.*/abc in all request URIs to /abc. Example 3

Custom variable:

The name of the custom variable is testname. The source of the variable is Path, the match rule is /abc/xyz/(. *), and the variable expression is $1. Example 3

If a request is destined for http://yourdomain.com/abc/xyz/abc/test.jpg, the URL is transformed to http://yourdomain.com/abc/test.jpg based on the URI rule. Based on the custom variable rule, the URL matches /abc/xyz/(. *). In this case, $1 is assigned the value abc and then appended to the cache key. The generated cache key is http://yourdomain.com/abc/test.jpgabc. Therefore, two rules are applied at the same time to support more complicated caching logic.

Notice Custom cache keys do not modify the URLs of the requests redirected to origin servers. They only modify the cache identifiers in these requests. Requests redirected to origin servers and sent by clients carry the same content.