When the ETag or Last-Modified response headers are not returned, the requested static resource fails to be cached. This reduces the CDN cache hit rate. To improve the CDN cache hit rate, you can configure a cache rule for this resource. As a result, the resource can be cached on CDN according to the cache rule.

Prerequisites

Before you perform the tasks described in this document, make sure that you have completed account registration and real-name authentication with Alibaba Cloud.

Background information

When no cache rule is configured on CDN, a static file cannot be cached on the CDN node if the ETag and Last-Modified response headers are not returned. Configure a cache rule for the static file as follows.

As shown in the following figure, the x-swift-cachetime response header shows that the cache duration of the static file on CDN is zero seconds. This indicates that CDN will not cache this static file because the ETag and Last-Modified response headers are not returned.Response header exception
If the origin server is configured with the following response headers, CDN will not cache the resource even if you have configured a cache rule. This is because these response headers have the highest priority in terms of CDN cache rules.
  • Any one of s-maxage=0, max-age=0, no-cache, no-store, and private
  • s-maxage or s-maxage=0
  • Pragma: no-cache

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 target domain name and click Manage.
  4. In the left-side navigation pane of the specified domain, click Cache.
  5. On the Cache Expiration tab, click Create Rule.
    Cache expiration
  6. In the Create Expiration Rule dialog box that appears, set Type to Directory or File Extension.
    Item Description
    Type
    • Directory: specifies resources cached in a specified directory.
    • File Extension: specifies resources cached in files with specified file extensions.
    Object
    • After 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.
    • After you set Type to File Extension, enter one or more file extensions in the Object field. Separate multiple file extensions with commas (,), for example, JPG,txt.
    Expire In Specifies 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 according to 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 as needed for static files such as JavaScript and CSS files that are frequently updated.
    • Specify a TTL value of 0 second for dynamic files such as PHP, JSP, and ASP files. As a result, the CDN node will not cache these files.
    Weight Specifies the priority of the rule.
    Note
    • Set this parameter to an integer from 1 to 99. A higher value indicates a higher priority and 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.
    For example, if you set the following rules for the example.aliyun.com domain, Rule 1 takes effect preferentially over the other two rules:
    • Rule 1: Type is set to File Extension, Object is set to jpg,png, Expire In is set to 1 Months, 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 Hours, 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.
    Cache Expiration
  7. Click OK.