tencent cloud

Feedback

Node Cache Validity Configuration

Last updated: 2022-09-15 16:12:59

    You can set the cache validity period of resources on the origin server on CDN nodes in Node Cache Validity to adjust the cache update frequency of origin server resources on the CDN nodes. You can configure the resource cache validity period by directory, file extension, and full file path based on your business needs.

    Overview

    CDN will determine whether a resource cached on a CDN node expires based on the cache validity period configured in Node Cache Validity.

    • If the cache of a resource accessed by an end user doesn't expire on the CDN node, the node will directly return the cached resource to the user.
    • If a resource accessed by an end user is not cached, or the resource cache has expired on the CDN node, the node will pull the latest resource from the origin server to cache it and return it to the user.

    After a resource on the origin server is updated, its cache on the CDN node must be updated immediately. You can use the cache purge feature to update unexpired caches on the CDN node, so as to ensure that resources cached on the CDN node and stored on the origin server are consistent.

    Notes

    • As the cache validity period affects the origin-pull frequency, we recommend you set the resource cache validity period based on your business needs. If it is too short, CDN will perform origin-pull frequently, thereby increasing the bandwidth usage of the origin server. If it is too long, caches on CDN nodes will be updated slowly, making end users unable to get the latest resources.
    • CDN nodes cache resources based on the CDN cache rule and priority. However, resources cached on a CDN node may be deleted from the node before the expiration time due to a low request frequency.
    • We recommend you use different filenames before and after updating a resource on the origin server. For example, name resources with different content by version numbers img-v1.jpg and img-v2.jpg, to prevent CDN nodes from returning the old but unexpired resource to the end user after the resource content is changed on the origin server.
    • If you still use the legacy version (in basic mode) of the node cache validity configuration, we recommend you submit the advanced mode configuration to upgrade the node cache validity configuration to the latest version so as to support more features. Note that after the upgrade to the advanced mode, you cannot restore to the basic mode. For more information on the legacy version of the node cache validity configuration, see Node Caching Rule Configuration (Legacy).
    • The origin server can set the Cache-Control response header to control the cache validity period on CDN nodes (when Cache Option is Follow Origin Server). Then, CDN nodes will pass the Cache-Control header to the end user to control the browser cache validity period. If you want CDN nodes to set the browser cache validity period, you can modify the Cache-Control header returned by CDN nodes to the user in Browser Cache Validity.

    Configuration Description

    Directions

    1. Log in to the CDN console.
    2. Click Domain Management on the left sidebar to enter the domain name management list.
    3. Select the target domain name and click Manage to enter the domain name configuration page.
    4. Click Cache Configuration to switch to the Cache Configuration tab, and you can view the Node Cache Validity.

    5. Click Add Rule to enter the Add Rule page and add a node cache validity rule.

      Configuration Item Description
      Type You can select All Files, File Extension, File Directory, Full Path, or Homepage.
      All Files: Set the rule for all files. This is the default option.
      File Extension: Set the rule for the specified file extension.
      File Directory: Set the rule for the specified file directory.
      Full Path: Set the rule for the specified full file path.
      Homepage: Set the rule for the specified domain name root directory.
      Content Enter the content based on the selected file type.
      If Type is All Files, the content is fixed to all files.
      If Type is File Extension, you can enter one or multiple file extensions separated by ";", such as `jpg;png;css`.
      If Type is File Directory, you can enter one or multiple file directories separated by ";", and the entered content cannot end with "/", such as `/test;/a/b/c`.
      If Type is Full Path, you can enter one or multiple full file paths separated by ";", such as `/index.html;/test/.jpg`.
      Cache Option You can select Follow Origin Server, Cache, or Do not cache.
      Follow Origin Server: The CDN node cache validity period will be set based on the `Cache-Control` origin server header, and heuristic caching can be enabled.
      Cache: You can customize the CDN node cache validity period and enable force cache.
      Do not cache: CDN nodes will not cache any resources.

    CDN cache rule and priority

    Cache Option is Follow Origin Server

    The cache validity period will be set on CDN nodes based on the Cache-Control origin server response header.

    • If the field in the Cache-Control origin server response header is max-age, the CDN node cache validity period will be set based on the value of max-age. For example, if Cache-Control: max-age=300 is configured, the cache validity period will be 300 seconds.
      • If the field in the Cache-Control origin server response header is no-cache, no-store, or private, the CDN node will not cache resources.
      • If there is no Cache-Control or Expires origin server response header, the cache rule will be set based on the heuristic caching status:
        • If heuristic caching is disabled and there is no Cache-Control or Expires origin server response header, the cache validity period will be 600 seconds.
        • If heuristic caching is enabled and there is no Cache-Control or Expires origin server response header, the heuristic cache validity period will be set according to the following rules:
          i. Default Configuration: If there is the Last-Modified origin server response header, the cache validity period will be calculated as follows: (current time - Last-Modified) * 0.1. If there is no Last-Modified origin server response header, the cache validity period will be 600 seconds by default.

          ii. Custom Policy: You can customize the heuristic cache validity period.

    Cache Option is Cache

    You can customize the cache validity period on the CDN node.

    • If force cache is disabled:
      • If the field in the Cache-Control origin server response header is max-age or there is no Cache-Control header, resources will be cached according to the custom CDN node cache rule.
      • If the field in the Cache-Control origin server response header is no-cache, no-store, or private, CDN nodes will not cache resources.

    • If force cache is enabled: The Cache-Control origin server response header will be ignored, and resources will be cached according to the custom CDN node cache rule.

    Cache Option is Do not cache

    CDN nodes are configured to not to cache resources. For each user request to access a resource, CDN nodes will directly perform origin-pull to get the resource and return it to the user.


    Priority of multiple cache rules

    If multiple cache rules are configured, the lower the rule position, the higher the priority. You can click Adjust Priority to drag and drop cache rules to change their order and adjust the priority.

    • For seldom updated static files, such as images and large files, we recommend you set the cache validity period to 30 days.

    • For frequently updated static files, such as .js and .css files, we recommend you set the cache validity period based on the update frequency of your business.

    • For dynamic files, such as .php, .jsp, .asp, and .aspx files, you need to set Cache Option to Do not cache.

    • For other requests involving direct interaction with the origin server, such as site login (/wp-admin directory for WordPress backend login, for example) or API-based query, you need to set Cache Option to Do not cache; otherwise, an access error may occur.

      Configuration limitations

    • You can add up to 100 cache rules for a domain name.

    • If there are multiple cache rules, the lower the rule position, the higher the priority.

    • If Type is set to File Extension, File Directory, or Full Path, you can enter up to 100 items and separate them by ";", such as jpg;png (when Type is set to File Extension).

    • If no rules are configured or the request fails to hit any configured rules, the cache validity period will be set on CDN nodes based on the Cache-Control origin server response header. If there is no Cache-Control header, CDN nodes will cache the resource for 600 seconds.

    • CDN nodes only cache content requested by GET and HEAD requests. Content requested by POST and OPTIONS requests won't be cached on CDN nodes.

    Configuration Samples

    Sample 1

    The original cache rules specify that CDN doesn't cache .php, .jsp, .asp, and .aspx files and caches other files for 30 days.



    You need to add a rule to cache .jpg and .png files for 10 days while ignoring the Cache-Control origin server response header (i.e., enabling force cache), and change Cache Option in the cache rule for All Files to Follow Origin Server.

    1. Click Add Rule, set Type to File Extension, Content to jpg;png, Cache Option to Cache, Cache Validity to 10 days, and Force Cache to Yes, and click OK.



    2. Select the cache rule for All Files, click Modify, change Cache Option to Follow Origin Server, and click OK.



    3. After the adjustment, the cache rules are:

      • .jpg and .png files will be cached for 10 days with force cache enabled.

      • .php, .jsp, .asp, and .aspx won't be cached.

      • Other files will be cached for 30 days.



        Below are actual caching results:
      • The www.test.com/abc.jpg resource will be cached on the node for 10 days, even though the field in the Cache-Control origin server response header is no-cache, no-store, or private.

      • The www.test.com/def.php resource won't be cached to the node.

        Sample 2

    Suggestions on configuring node cache validity rules for a site built based on WordPress:

    • For resources under the domain name /wp-admin directory for backend login, you need to set Cache Option to Do not cache; otherwise, backend login resources will be cached and login errors will occur. If there are any API resources, you also need to set Cache Option to Do not cache for them.
    • For .php, .jsp, .asp, and .aspx dynamic files, you need to set Cache Option to Do not cache (default cache rule of CDN).
    • As .html, .js, and .css files are updated frequently, you need to set the cache validity period based on the update frequency. We recommend you set the cache validity period to 7 days and disable force cache.
    • Other files are cached for 30 days (default cache rule of CDN).

    Add cache rules while retaining the default CDN cache rules:

    1. Click Add Rule, set Type to File Directory, Content to /wp-admin, and Cache Option to Do not cache, and click OK.

    2. Click Add Rule, set Type to File Extension, Content to html;js;css, Cache Option to Cache, Cache Validity to 7 days, and Force Cache to No, and click OK.

    3. As the lower the rule position, the higher the priority, click Adjust Priority and drag and drop the rule of not caching files in the /wp-admin directory to the bottom to grant it the highest priority.

    4. After the adjustment, the cache rules are:
      • All resources under the /wp-admin directory will not be cached.
      • .html, .js, and .css files will be cached for 7 days.
      • .php, .jsp, .asp, and .aspx won't be cached.
      • Other files will be cached for 30 days.

    FAQs

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support