Updated on 2024-03-13 GMT+08:00

Cache Rules

You can configure the TTL for one or more cached resources on CDN PoPs. If the TTL of a file cached on CDN PoPs has reached, CDN requests the most recent content of the file from the origin server when a user requests the file. CDN returns the content to the user and caches it on CDN PoPs. You can cache all files and the homepage, or cache desired content by directory, file type, and full path. You can configure URL parameter filtering for different cache rules to improve the cache hit ratio and distribution efficiency.

Background

Cache policies on CDN PoPs comply with HTTP. You can control cache aging by configuring the Cache-Control: max-age field in an HTTP response header. By leveraging cache rules, you can optimize cache periods for different services. Appropriate cache policies can increase the hit ratio and reduce the origin pull rate, which reduces bandwidth utilization.

After receiving a request, a CDN PoP will check whether the requested content has expired in the cache. If the requested content is valid in the cache, it will be returned directly from that CDN PoP to the user, speeding up site response. If the requested content in the cache has expired, the CDN PoP will send a request to obtain new content from an origin server so it can update its local cache and serve new content to the user.

Precautions

  • Up to 60 cache rules can be added to each domain name.
  • The cache TTL affects the origin pull rate directly. If the TTL is short, cached content on CDN PoPs becomes invalid in a short time, resulting in frequent origin pulls, which increases the origin server load and prolong the access latency. However, if the TTL is too long, cached content may be outdated as a result.
  • If the TTL is set to 0, CDN pulls content from the origin server for all user requests, which may interrupt the acceleration service.
  • Resources cached on PoPs may be deleted due to infrequent access.
  • If you have modified the cache rule,
    • The new rule does not apply to content that has been cached but only applies to new content.
    • You can refresh the cache to make the modification immediately take effect for new content and the content that has been cached.

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. See Figure 1. Table 1 describes the parameters. You can click Suggested Rules to view the recommended configuration.
    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 TTL for All files is 30 days (the default TTL of content on a domain name with whole site acceleration is 0). 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 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 50 file name extensions.
    • Enter up to 1,000 characters.
    • File name extensions are case-insensitive.

    Example: .JPG;.zip;.exe

    NOTE:

    If your domain name has special configurations, enter up to 20 file name extensions and up to 255 characters.

    Directory

    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

    • Wildcard matching is supported. Rules for using wildcards (*):
      • Only one directory with one wildcard can be set for each rule. Example: /test/*
      • CDN uses prefix match. For example, when the path of a cache rule is /test/*, /test/abc and /test/abc/001 also use this rule.
      • Wildcards cannot be set for domain names with special configurations.
      • Wildcards cannot match slashes (/). For example, /test/*/abc cannot match /test/folder01/folder02/abc.
      • A wildcard can match one or more characters but cannot match zero characters. For example, /test* cannot match /test.
      • /* cannot be set as a path.

    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. The priority setting is mandatory. 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 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 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, do not ignore 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.

    Origin Cache Control

    If Cache-Control: max-age or Expires has been configured for the origin server and you want the cache TTL on the CDN side to be the same as that configured on the origin server, you can switch on Origin Cache Control. Then Cache-Control: max-age or Expires determines how long content is cached on CDN PoPs.

    NOTE:
    • If both Cache-Control and Expires are configured on the origin server, Cache-Control is preferentially used.
    • If origin cache control is enabled on CDN but neither Cache-Control nor Expires is configured on the origin server, CDN PoPs use the cache rules configured on CDN.

    By default, this function is disabled.

    Forcible Cache

    Whether CDN PoP cache ignores the no-cache, private, and no-store fields in the Cache-Control response header sent from the origin server. Forcible cache supplements origin cache control. The default rules are as follows:

    1. When origin cache control is enabled, 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 origin cache control is disabled, forcible cache is enabled.
      • CDN ignores response headers from the origin server and uses the cache TTL configured on the CDN console.
    NOTE:
    • This parameter is a special configuration parameter and is for display only. You cannot modify it. If necessary, submit a service ticket to modify it.
    • When both origin cache control and forcible cache are disabled, there are two cases:
      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.

    Enabled

  7. (Optional) Delete a cache rule if you no long use it.
  8. Click OK.

Examples

Scenario 1: Assume that you have configured CDN acceleration for the domain name www.example.com. The following figure shows the cache rule configuration.

The homepage of the website is not cached, and URL parameters are not ignored in requests for all pages.

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

  1. You have configured CDN acceleration for the domain name www.example.com. Due to service requirements, files in .do format do not need to be cached, and URL parameters should be ignored in requests for all files.

    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, 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 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.