All Products
Search
Document Center

CDN:Create custom cache keys

Last Updated:Feb 29, 2024

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

Important

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 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 include complex parameters. Even if the requests are sent to retrieve the same file, POPs consider that the requests are sent to retrieve different files due to the variation of URL parameters, and then cache the files for all requests. As a result, a large number of requests are redirected to the origin server.图一

You can create a custom cache key for a type of request to reduce the number of requests that are redirected to the origin server.图二

Scenario 2

The requests have the same URL. In this case, Alibaba Cloud CDN considers that the requests are sent to retrieve the same file. However, the client OSs are specified in the client field of the HTTP header in the requests. The requests are sent to retrieve different files.场景一

To resolve this issue, you can create 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.场景二

Procedure

  1. Log on to the Alibaba Cloud CDN 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 Manage in the Actions column.

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

  5. 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, which starts with a forward slash (/). Source URIs do not contain http:// or domain names. Perl Compatible Regular Expressions (PCRE) are supported.

    • Final URI, which starts with a forward slash (/). Final URIs do not 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 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.

    自定义Cachekey

  6. Click OK.

Sample configurations

URIs

In this example, 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 retrieve the same file. The cache key of the file is http://aliyundoc.com/c/image.jpg.URI

Request Parameter

For requests that are destined for URI 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.

Important

The priorities of actions that are applied to the same variable are: Add > Delete > Reserve > 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 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.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.自定义变量

If a request that is destined for 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.

Note

$n in the variable expression refers to the matched content that is enclosed in the No. n bracket. 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 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:

Transform /abc/.*/abc in all request URIs into /abc.示例三

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.示例三

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.