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

PoP Cache Rules

You can configure the TTL for one or more cached resources on CDN PoPs. If the TTL of a file 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 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 periods 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 prolongs 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,
    • Your modifications are effective for new content cached.
    • You can purge to apply modifications to all resources (including the existing PoP cache).

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 a cache rule. 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 specific types.

    If the service type of a new domain name is Website, File download, or On-demand services and its origin server type is IP address or Domain name, 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 100 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

    Directories are matched by prefix. Start a directory with a slash (/), and separate multiple directories with semicolons (;). Enter up to 20 directories with up to 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 in 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.

    • You can configure only one cache rule for the homepage.

    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 TTL has reached, CDN pulls the most recent content of the file from the origin server when a user requests the file from a CDN PoP. Then, CDN caches that content on the PoP and serves it to the user.

    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 content (such as .php, .jsp, and .asp files and dynamic APIs), 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 Cache-Control and Expires are not 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.

    SWR

    If you have set Cache-Control to stale-while-revalidate=*** (specific duration) on your origin server, you can enable SWR on CDN. This allows clients to use stale resources cached on CDN PoPs, as long as the specified SWR duration has not elapsed. At the same time, CDN pulls and caches the latest resources from the origin server to serve future user requests.

    -

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