All Products
Search
Document Center

CDN:Create custom cache keys

Last Updated:Sep 05, 2025

You can configure custom cache keys based on different parts of an HTTP request, such as the URI, request parameters, HTTP request headers, or custom variables. This feature converts different requests for the same file into a single cache key, which improves the cache hit rate, reduces origin fetches, and decreases response times and bandwidth consumption.

Notes

  • The custom cache key and ignore parameter features conflict with each other. If you have configured ignore parameters, Alibaba Cloud CDN points of presence (POPs) remove parameters that follow the question mark (?) in request URLs. This makes the request parameters configured in the cache key unavailable. Before you enable custom cache keys, make sure that you have not configured any ignore parameter settings.

  • The custom cache key feature does not change the origin fetch URL. It only modifies the cache identifier of the request. The content of the origin fetch request remains the same as the client request.

  • The URL purge feature cannot purge cached content after a custom cache key is set.

Scenarios

Important

The custom cache key feature does not change the origin fetch URL. It only modifies the cache identifier of the request. The content of the origin fetch request remains the same as the client request.

A cache key is the unique ID for a file cached on a CDN POP. Each cached file has a corresponding cache key. By default, a file's cache key is the request URL, including its parameters.

Scenario 1

Request URLs can contain complex parameters. Even if multiple requests are for the same file, different URL parameters cause POPs to treat them as requests for different files. This results in multiple cached copies and an increase in origin fetches.图一

You can create a custom cache key rule to unify the cache key for these requests. This reduces origin fetches.图二

Scenario 2

When requests have the same URL, CDN considers them requests for the same file. However, the HTTP header might contain a client field that distinguishes between client systems, which means the requests are actually for different files.场景一

In this case, you can create a custom cache key that includes the value of the client field. This allows the two requests to be identified by two different cache keys.场景二

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 to set a cache key.

    Parameter Type

    Description

    Rule Condition

    Rule conditions can identify parameters in a request to determine whether a configuration applies to the request.

    • Do not use conditions

    • If you want to add or edit rules conditions, see Rules engine.

    URIs

    • Source URI: The URI that starts with a forward slash (/). It does not include the `http://` prefix or the domain name. Perl Compatible Regular Expressions (PCRE) are supported.

    • Final URI: The URI that starts with a forward slash (/). It does not include the `http://` prefix or the domain name. The rewritten destination URI is used to construct the cache key.

    Request Parameter

    The object of the operation is the parameters in the original request URL. You can add, delete, modify, or retain parameters. The result is appended to the cache key.

    HTTP Headers

    The object of the operation is the HTTP header in the original request. You can append the values of multiple HTTP headers to the cache key.

    Custom Variables

    Use a regular expression to extract any field from the request parameters, HTTP header, cookie, or URI of the original request and append it to the cache key.

    自定义Cachekey

  6. Click OK.

Configuration examples

URIs

Requests for http://aliyundoc.com/a/b/image.jpg and http://aliyundoc.com/a/b/c/image.jpg are treated as requests for the same file. The cache key for the file is http://aliyundoc.com/c/image.jpg.URI

Request Parameter

For a request to http://aliyundoc.com/a/b/image.jpg?delete_par=1&modify_par=1 , the rule adds add_par=1, deletes delete_par, and changes the value of modify_par to 2. The final cache key is based on the URL http://aliyundoc.com/a/b/image.jpg?modify_par=2&add_par=1.

Important

If multiple operations are performed on the same variable, the order of priority is: Add > Delete > Retain > Modify.

参数操作

HTTP Headers

The values of the User-Agent and Accept-Language HTTP headers are appended to the cache key. For example, a request for http://aliyundoc.com/a/b/image.jpg has User-Agent=Mozilla/5.0 (Linux; X11) and Accept-Language=en. The cache key for this request is: http://aliyundoc.com/a/b/image.jpgMozilla/5.0(Linux;X11)en.HTTP Header

Custom Variables

Example 1

Set the variable name to language, the source to Request Header, the source field to Accept-Language, the match rule to ([%w]+),([%w]+), and the variable expression to $1aa.自定义变量

A client requests http://aliyundoc.com/a/b/image.jpg with the HTTP header Accept-Language=en,ch. The match rule captures en as $1. The variable expression uses this capture group and appends aa, resulting in the value enaa. This value, identified by the alias language, is appended to the URL to form the final cache key: http://aliyundoc.com/a/b/image.jpgenaa.

Note

In a variable expression, $n refers to the content matched by the n-th group in the matching rule. For example, if the string is Accept-Language=en,ch and the matching rule is ([%w]+),([%w]+), then $1=en and $2=ch.

Example 2

Set the variable name to expired, the source to Request Cookie, the source field to a, the match rule to [%w]+:(.*), and the variable expression to $1.自定义变量

A client requests http://aliyundoc.com/a/b/image.jpg with the cookie Cookie a=expired_time:12635187. The match rule extracts the value 12635187 and assigns it to the variable expression $1, which has the alias expired. This value is then appended to the URL to create the final cache key: http://aliyundoc.com/a/b/image.jpg12635187.

Example 3

Set both a URI rule and a custom variable.

URI:

Merge all requests whose URIs match /abc/.*/abc into /abc.示例三

Custom variable:

Set the variable name to testname, the source to Path, the match rule to /abc/xyz/(.*), and the variable expression to $1.示例三

A client requests the URL http://aliyundoc.com/abc/xyz/abc/image.jpg. Based on the URI configuration, the URL is merged, and the resulting cache key is http://aliyundoc.com/abc/image.jpg. Then, based on the custom variable configuration, the original URL matches the pattern /abc/xyz/(.*). In this case, the variable $1 is assigned the value abc and appended to the cache key. The final cache key is http://aliyundoc.com/abc/image.jpgabc. This combination of two rules allows for more complex caching logic.

If the custom variable's match rule is not met, the variable expression $1 is not appended to the cache key.

Example 4

You can set a rule condition and a custom variable at the same time to generate different cache keys for requests from mobile and PC clients.

Mobile rule condition:

User-Agent contains any of *Mobile*,*Android*,*iPhone*,*ipad*

image

PC rule condition:

User-Agent does not contain any of *Mobile*,*Android*,*iPhone*,*ipad*

image

Mobile custom cache key:

Set Rule Condition to Mobile. Then, for Custom Variables, set Variable Name to Mobile, Source to Path, Matching Rule to /, and Variable Expression to +mobile.

image

PC custom cache key:

Set Rule Condition to PC. For Custom Variables, set Variable Name to PC, Source to Path, Matching Rule to /, and Variable Expression to +pc.

image

A client requests the URL http://aliyundoc.com/image.jpg. Based on the User-Agent value, the request matches either the Mobile or PC custom cache key rule. The final cache key for a Mobile client is: http://aliyundoc.com/image.jpg+mobile. The final cache key for a PC client is: http://aliyundoc.com/image.jpg+pc.