When the ETag or Last-Modified response header is not returned, the requested static resource fails to be cached. This reduces the cache hit ratio in Alibaba Cloud Content Delivery Network (CDN). To improve the cache hit ratio of Alibaba Cloud CDN, you can configure a cache rule for this resource. As a result, the resource can be cached on CDN nodes based on the cache rule. This topic describes how to configure cache rules for resources.
When no cache rule is configured, if the ETag and Last-Modified response headers are not returned, a static file cannot be cached on a CDN node. The following steps show how to configure a cache rule for the static file.
- One of
- Log on to the Alibaba Cloud CDN console.
- In the left-side navigation pane, click Domain Names.
- On the Domain Names page, find the domain name that you want to manage and click Manage in the Actions column of the domain name.
- In the management pane of the domain name, click Cache.
- On the Cache Expiration tab, click Create Rule.
- In the Create Expiration Rule dialog box, set Type to Directory or File Extension,
and set the parameters that are described in the following table.
Parameter Description Type
- Directory: specifies the resources that are cached in the specified directory.
- File Extension: specifies the resources that are cached in files with the specified file extensions.
- If you set Type to Directory, enter a directory name in the Object field. The directory name must start with a forward slash (/), for example, /directory/aaa.
- If you set Type to File Extension, enter one or more file extensions in the File Extension
field. Separate multiple file extensions with commas (,), for example,
Expire In Set a TTL value of the cached resources. A CDN node can cache resources for up to three years. We recommend that you set this parameter based on the following rules:
- Specify a TTL value of one month or longer for static files such as images and applications that are not frequently updated.
- Specify a TTL value of 0 seconds to disable caching for dynamic files such as PHP, JSP, and ASP files.
Weight Set the priority of the cache expiration rule.Note
For example, if you set the following rules for
- The value must be an integer from 1 to 99. A higher value specifies a higher priority. A rule with a higher priority prevails over rules with lower priorities.
- We recommend that you do not set the same priority for different rules. If different rules have the same priority value, one of these rules is applied at random.
- After a cache expiration rule is applied, other cache expiration rules are no longer matched.
example.aliyun.com, Rule 1 is preferentially applied over the other two rules:
- Rule 1: Type is set to File Extension, File Extension is set to jpg,png, Expire In is set to 1 month, and Weight is set to 90.
- Rule 2: Type is set to Directory, Object is set to /www/dir/aaa, Expire In is set to 1 hour, and Weight is set to 70.
- Rule 3: Type is set to Directory, Object is set to /www/dir/aaa/example.php, Expire In is set to 0 seconds, and Weight is set to 80.
- Click OK.
What to do next
To check whether the cached resource can be hit, perform the following steps:
- Open the browser Google Chrome and press F12.
- In the panel that appears, click the Network tab.
- Check the value of the
X-Cachefield in the response header of the specified URL.
- A value of
HITindicates that the resource that is cached on the CDN node is hit.
- A value of
MISSindicates that the requested resource has not been cached on the CDN node and is retrieved from the origin server.
- A value of