Updated on 2024-12-10 GMT+08:00

Setting the Cache TTL

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 cache TTL 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 pull 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 TTL on the origin server, this TTL 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 a Cache TTL Based on Service Type

Default cache TTL

  • If the service type is website acceleration, file download acceleration, or on-demand service acceleration, and the origin server address is an IP address or domain name, the following default cache rules are available:
    • The default cache TTL 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 cache TTL for other files is 30 days. You can modify but cannot delete this rule.
  • If your origin server is an OBS bucket, the default cache TTL 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 TTL set to 0 is available by default. You can modify and delete this rule.

You can configure a cache TTL based on the service type and the following suggestions.

  • Website acceleration
    • 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 seven days.
    • Cache other static files for 30 days.
  • Download acceleration
    • Do not cache dynamic files such as php, aspx, asp, jsp, and .do files.
    • Cache files of the following 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.
  • On-demand service acceleration
    • Do not cache dynamic files such as .php, .aspx, .asp, .jsp, and .do files.
    • Cache the following file types for seven 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 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. 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 cache TTL 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 cache TTL 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

    A full path must start with a slash (/) and cannot end with a wildcard (*). 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.

    TTL

    Duration that a file can be cached. If the cache TTL 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 cache TTL 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 TTL to more than one month.
    • For static files (such as JS and CSS files) that are frequently updated, set the TTL based on service requirements.
    • For dynamic files (such as PHP, JSP, and ASP files), set the TTL to 0 seconds.

    Query Parameters

    Most web page requests carry URL parameters starting with a question mark (?). If parameters do not contain important information (such as version), you can ignore them to improve the cache hit ratio and speed up delivery.

    Configuration rules:

    • If resources do not change with URL parameters, ignore query parameters.
    • If resources change with URL parameters, retain query parameters.
    • If you have enabled video seek, set Query Parameters to Ignore all for your video resources.
    • Retain all: CDN retains all parameters following the question mark (?).
    • Ignore all: CDN ignores all parameters following the question mark (?) in request URLs, improving the cache hit ratio.
    • Ignore specific: CDN ignores the specified parameters in request URLs but retains other parameters.
    • Retain specific: CDN retains the specified parameters in request URLs but ignores other parameters.

    URL Parameters

    Parameters to be ignored or retained. Leave this parameter blank when Query Parameters is set to Retain all or Ignore all.

    • Enter up to 10 parameter names separated by semicolons (;).
    • Only letters, digits, periods (.), underscores (_), and tildes (~) are supported.

    TTL Source, that is, the original Origin Cache Control field

    If Cache-Control: max-age or Expires has been configured on the origin server, you can set TTL Source on CDN to synchronize the cache TTL from the origin server to CDN or force CDN to use the shorter TTL between the cache TTL in the cache rule and that on the origin server. By default, the cache TTL in the CDN cache rule is used. TTL Source values include:

    • Origin server: CDN PoPs use the cache TTL set on the origin server.
    • CDN: CDN PoPs use the cache TTL set in the cache rule.
    • Whichever is shorter: CDN PoPs use the shorter TTL between the cache TTL in the cache rule and that on the origin server.
    NOTE:
    • If both Cache-Control and Expires are configured on the origin server, Cache-Control is preferentially used.
    • If TTL Source is set to Origin server, but neither Cache-Control nor Expires is configured on the origin server, CDN PoPs use the cache rule configured on CDN.

    The default TTL source is CDN.

    Forcible Cache

    Whether to ignore the no-cache, private, and no-store fields in the Cache-Control response header of the origin server. When this function is enabled, these fields are ignored. Forcible cache supplements TTL source. The rules are as follows:

    1. When TTL Source is set to Origin server and Forcible Cache is disabled:
      • If no-cache, private, or no-store is set in the Cache-Control response header, CDN PoPs do not cache resources.
      • If other response headers are set, the priority is s-maxage > max-age > expires. For example, if Cache-Control: max-age=500, s-maxage=400 is set on the origin server, the cache TTL on CDN PoPs is 400s.
      • If the preceding response headers are not set, the cache TTL configured on the CDN console is used.
    2. When TTL Source is set to Origin server and Forcible Cache is enabled:
      • If cache directives are set in the response header of the origin server, the priority is s-maxage > max-age > expires. For example, if Cache-Control: max-age=500, s-maxage=400 is set on the origin server, the cache TTL on CDN PoPs is 400s.
      • If the preceding response headers are not set, the cache TTL configured on the CDN console is used.
    3. When TTL Source is set to CDN and Forcible Cache is enabled:
      • CDN ignores response headers from the origin server and uses the cache TTL configured on the CDN console.
    4. When TTL Source is set to CDN and Forcible Cache is disabled:
      1. If no-cache, private, or no-store is set in the Cache-Control response header sent from the origin server, CDN PoPs do not cache resources.
      2. If no-cache, private, or no-store is not set, CDN uses the cache TTL configured on the CDN console.
    5. When TTL Source is set to Whichever is shorter and Forcible Cache is disabled:
      • If the cache TTL set on CDN is shorter, the rule 4 is used.
      • If the cache TTL set on the origin server is shorter, the rule 1 is used.
    6. When TTL Source is set to Whichever is shorter and Forcible Cache is enabled:
      • If the cache TTL set on CDN is shorter, the rule 3 is used.
      • If the cache TTL set on the origin server is shorter, the rule 2 is used.

    By default, this function is enabled.

  7. (Optional) Delete a cache rule if you no long use it.
  8. Click OK.
    If you have modified a cache rule,
    • Your modifications are effective for new content cached.
    • You can purge to apply modifications to the existing cache.

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 TTL 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 cache TTL 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 TTL to 0.

    The new rule only applies to new content. After the new rule is added, purge 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 cache TTL 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 TTL 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.