Alibaba Cloud Content Delivery Network (CDN) allows you to set a time-to-live (TTL) value for static resources that are cached on CDN nodes. You can specify directories and files. CDN nodes automatically delete resources that are expired. Requests received by CDN nodes are considered invalid if the requested resources have already expired. These requests are redirected to origin servers to retrieve resources. The retrieved resources are cached on CDN nodes again. This topic describes how cache expiration rules work on CDN nodes and how to create a cache expiration rule.
Background information

- If you have not created a cache expiration rule on your origin server or in the Alibaba Cloud CDN console, the default cache expiration rule applies. The default TTL value for cached resources is 10 seconds and the maximum is 3,600 seconds. You can modify the TTL value in the Alibaba Cloud CDN console. The amount of back-to-origin network traffic and fees vary based on the TTL value. We recommend that you set a TTL value based on your business requirements. A small TTL value may cause CDN nodes to frequently redirect requests to the origin server and increase the amount of network traffic on the origin server.
- If an origin server has a cache expiration rule configured, the priority of this rule is lower than the cache expiration rules set on CDN nodes. If an origin server does not have a cache expiration rule configured, you can set a cache expiration rule by specifying the directory or file type. You can specify a full path.
- A CDN node may remove cached files that are not frequently requested before they expire.
- If the ETag value from the origin server and the If-Match value from the client are the same, the CDN node that is nearest to the client returns the cached content to the client. If the ETag value from the origin server and the If-Match value from the client are different, the CDN node retrieves the latest content from the origin server and returns it to the client. The CDN node also updates the cached content with the retrieved content. CDN nodes compare the If-Match value with the ETag value before the nodes apply the cache expiration rules.
Scenarios
CDN nodes respond to requests based on the cache status. If the requested resource is not cached on CDN nodes, the request is redirected to the origin server to retrieve the resource. After the resource is retrieved from the origin server, an HTTP 2XX status code is returned to the CDN node. The retrieved resource is cached on the CDN node and also returned to the client.
If resources on your origin server are updated but the resources cached on CDN nodes are not, CDN nodes may return outdated resourced to clients or respond to requests slowly. In this case, you can create a cache expiration rule. After resources expire based on the cache expiration rule, CDN nodes retrieve the latest resources from the origin server. We recommend that you set a greater TTL value for infrequently requested resources. This increases the cache hit ratio and accelerates content delivery.
Procedure
When you update resources on the origin server, we recommend that you use different names for different versions of the same resource. This separates outdated and updated resources on the origin server. You can use version numbers to indicate different versions of a resource, for example, img-v1.0.jpg and img-v2.1.jpg.
Examples
example.aliyun.com
. Rule 1 has the highest priority. If Rule 1 is applied, other rules are ignored.
- Rule 1: The TTL value of all JPG and PNG files is set to one month. The weight of the rule is set to 90.
- Rule 2: The TTL value of the directory example.aliyun.com is set to 1 hour. The weight of the rule is set to 70.
- Rule 3: The TTL value of the full path /www/dir/aaa/example.php is set to 0 seconds. The weight of the rule is set to 80.