Help Center/ Content Delivery Network/ Best Practices/ Setting the Maximum Cache Age
Updated on 2023-12-07 GMT+08:00

Setting the Maximum Cache Age

CDN caches origin content on globally distributed PoPs so that users can obtain the content from nearby PoPs. On the CDN console, you can set the maximum cache age for origin content of different file types based on service requirements.

Impact of Origin Servers on CDN PoP Caches

  • If you have configured a cache rule on the origin server, the following scenarios are possible:
    • If you have set Cache-Control to no-cache, private, or no-store on the origin server and enabled Origin Cache Control on the CDN console, CDN PoPs do not cache origin content. Instead, CDN PoPs retrieve content from the origin server each time the content is requested. This does not achieve acceleration.

      By default, Origin Cache Control is disabled on the CDN console.

    • If you have set a specific maximum cache age on the origin server, this maximum cache age will be overwritten by that set on the CDN console.
  • If no cache rules are set on the origin server, cache rules set on the CDN console are used.

Setting the Maximum Cache Age Based on Service Type

Default maximum cache age

  • If the service type is website acceleration, file download acceleration, or on-demand service acceleration, and the origin server address is an origin IP address or origin domain, the following default cache rules are available:
    • The default maximum cache age for common dynamic files (for example, .php, .jsp, .asp, and .aspx files) is 0. CDN pulls content from the origin server directly when receiving requests for such dynamic files. You can modify and delete this rule.
    • The default maximum cache age for files other than common dynamic files is 30 days. You can modify but cannot delete this rule.
  • If your origin server is an OBS bucket, the default maximum cache age for all files is 30 days. You can modify but cannot delete this rule.

    You can add a custom cache rule with a higher priority so that custom rule will be used.

  • If the service type is whole site acceleration, a cache rule with Type set to All files and Maximum Age set to 0 is available by default. You can modify and delete this rule.

You can configure the maximum cache age based on the service type.

  • You are advised to set the maximum cache age for website acceleration so that CDN PoPs proceed as follows:
    • Do not cache dynamic files such as .php, .aspx, .asp, .jsp, .do, .dwr, .cgi, .fcgi, .action, .ashx, .axd, and .json files.
    • Cache .shtml, .html, .htm, and .js files for 7 days.
    • Cache other static files for 30 days.
  • You are advised to set the maximum cache age for download acceleration so that CDN PoPs proceed as follows:
    • Do not cache dynamic files such as php, aspx, asp, jsp, and .do files.
    • Cache the following file types for 30 days: .7z, .apk, .wdf, .cab, .dhp, .exe, .flv, .gz, .ipa, .iso, .mpk, .mpq, .pbcv, .pxl, .qnp, .r00, .rar, .xy, .xy2, .zip, and .cab.
  • You are advised to set the maximum cache age for VOD acceleration so that CDN PoPs proceed as follows:
    • Do not cache dynamic files such as .php, .aspx, .asp, .jsp, and .do files.
    • Cache the following file types for 7 days: .mwv, .html, .htm, .shtml, .hml, .gif, .swf, .png, .bmp, and .js.
    • Cache the following file types for 30 days: .mp3, .wma, .7z, .apk, .wdf, .cab, .dhp, .exe, .flv, .gz, .ipa, .iso, .mpk, .mpq, .pbcv, .pxl, .qnp, .r00, .rar, .xy, .xy2, .zip, and .cab.

Procedure

  1. Log in to the Huawei Cloud console. Choose Service List > Content Delivery & Edge Computing > Content Delivery Network.

    The CDN console is displayed.

  2. In the navigation pane, choose Domains.
  3. In the domain list, click the target domain name or click Configure in the Operation column.
  4. Click the Cache Settings tab.
  5. In the Cache Rules area, click Edit.

    The Configure Cache Rule dialog box is displayed.

  6. Click Add to add cache rules. See Figure 1. Table 1 describes the parameters.
    Figure 1 Configuring a cache rule
    Table 1 Cache rule parameters

    Parameter

    Description

    Configuration Rule

    All files

    All cached resources on CDN PoPs

    By default, CDN has a rule for every new domain name. The rule specifies that the maximum cache age for All files is 30 days. You can modify but cannot delete this rule.

    File type

    Files of a specific type

    If the service type of a new domain name is Website, File download, or On-demand service and its origin server is a private one, CDN adds a rule to it by default. The rule specifies that the maximum cache age is 0 for common dynamic files, such as .php .jsp .asp, and .aspx files. CDN pulls such files from the origin server for every request. You can modify and delete this rule.

    • All file types are supported.
    • Start each file name extension with a period (.), and separate file name extensions with semicolons (;).
    • Enter up to 255 characters.
    • Enter up to 20 file name extensions.
    • File name extensions are case-insensitive.

    Example: .JPG;.zip;.exe

    Directory

    All files in a directory

    Start a directory with a slash (/), and separate multiple directories with semicolons (;). Enter a maximum of 20 directories with a maximum of 255 characters in total.

    Example: /test/folder01;/test/folder02

    Full path

    A specific file

    Start a full path with a slash (/) and do not end it with an asterisk (*). A file in the specified directory or file with the wildcard * can be matched. Enter only one full path.

    Examples: /test/index.html or /test/*.jpg

    Homepage

    Root directory

    The root directory of a website is the top-level directory of the website folder, which contains all subfolders of the website.

    For example, for abc/file01/2.png, abc/ is the root directory, and a cache rule is configured for abc/.

    Priority

    Priority of a cache rule

    Each cache rule must have a unique priority. If a resource is specified in multiple cache rules, the rule with the highest priority is applied.

    Enter an integer ranging from 1 to 100. A greater number indicates a higher priority.

    Maximum Age

    Duration that a file can be cached. If the maximum age of the file has reached, CDN requests the most recent content of the file from the origin server when a user requests the file from a CDN PoP. In addition, the CDN caches that content on the CDN PoP.

    The age of a cached file cannot exceed 365 days. You are advised to set the time according to the following rules:

    • For static files (such as .jpg and .zip files) that are not frequently updated, set the maximum age to more than one month.
    • For static files (such as .js and .css files) that are frequently updated, set the maximum age based on service requirements.
    • For dynamic files (such as .php, .jsp, and .asp files), set the maximum age to 0 seconds.
  7. (Optional) Delete a cache rule if you no long use it.
  8. Click OK.

    If you have modified a cache rule, the new rule does not apply to content that has been cached but only applies to new content. If you want the modification to take effect immediately, refresh the cache after modifying the cache rule.

Examples

Scenario 1: Assume that you have added a web portal to Huawei Cloud CDN for acceleration, but you do not want to cache it.

You can add a cache rule for this web portal on the CDN console, with Type set to Homepage and Maximum Age to 0.

Scenario 2: Assume that you do not want to cache files of a specific type or a specific web page.

  1. You have configured CDN acceleration for your website and set the maximum cache age of .do files to one day. However, due to service requirements, you do not need to cache .do files anymore.

    You can add a cache rule for your website on the CDN console, with Type set to File type, Content to .do, and Maximum Age to 0.

    The new rule only applies to new content. After the new rule is added, refresh the cached URL or directory where the .do file is located on the CDN console so that the new rule can take effect for all .do files.

  1. You have configured CDN acceleration for your website, the login page of your website is displayed cyclically, and your customers cannot log in to the website. After CDN acceleration is disabled, customers can log in to the website.

    This is because CDN PoPs have cached the login page. To resolve the issue, add a cache rule for your website on the CDN console and set the maximum cache age of the login page to 0 in the rule. Take the login page of the Huawei Cloud console as an example. The login page of the Huawei Cloud console is https://auth.huaweicloud.com/authui/login.html#/login. You can add a cache rule on the CDN console, with Type set to Full path, Content to /authui/login.html#/login, and Maximum Age to 0.

Scenario 3: Assume that you have configured the following cache rules for your acceleration domain name www.example.com but do not know which rule takes effect.

When a user requests www.example.com/test/cdn.jpg, rules of the All files, File type, and Full path type are all matched. The priority of the Full path rule is 8, which is the highest among the three rules. Therefore, the rule of the Full path type (/test/*.jpg) is used.