All Products
Search
Document Center

:How to set the server-side cache expiration time when Alibaba Cloud Content Delivery Network accelerate static resources

Last Updated:May 19, 2022

Description

When Alibaba Cloud Content Delivery Network (CDN) accelerates static resource delivery, it loads resources from an origin server to the available CDN node that is closest to the visitor. When you access this static resource, you can directly obtain it from the cache. This topic describes how to set the cache expiration time on the server side.

References

Valid values: Console Settings > Origin Header Settings > Default Cache Policy Settings.

WebServer cache policy settings

How to Set up IIS cache policy

To set an IIS cache policy, perform the following steps:
  1. Because the whole site only caches files such as HTML, JPG, PNG, GIF, APK, etc., first set the whole site to not cache. The specific operations are as follows.
    1. Open IIS Information Manager and right-click the properties of the service website example.aliyundoc.com. This topic uses the example.aliyundoc.com service website as an example.
    2. Click HTTP Header, select Enable Content Expiration, select Immediately Expiration, and click OK.
  2. After these settings are applied, Alibaba Cloud CDN will not cache any content of the website. Then, you can configure cache policies for HTML, JPG, PNG, GIF, and APK files.
    • Files with different extensions are placed under different directories. A directory contains only files with the same extension. To configure cache expiration for a directory that contains files with a specified extension, Syntax:
      1. Open IIS Manager.
      2. Expand the directory of the website example.aliyundoc.com and select the directory for which you want to set the cache time. If all jpg files are stored in the img directory, right-click the directory and select Properties, and then click HTTP Header.
        Note: The entire website is not cached in step 1. Therefore, the cache settings under HTTP header are the same as those in step 1.
      3. Select Expiration, specify an expiration time, and click OK.
    • Files with the specified extension are not stored in a unique directory, but in a directory that contains files with different extensions. The test.jpg in the bin directory is used as an example to describe the procedure of cache settings.
      Note: To avoid configuring files with specific extensions one by one, you must set IIS to support wildcard characters.
      1. Enable IIS to support wildcard characters.
        1. Open IIS Information Manager, right-click the properties of the service website example.aliyundoc.com, select Home Directory, and click Configure.
        2. In the dialog box that appears, click Mapping and click Insert.
        3. In the dialog box that appears, choose C:\WINDOWS\Microsoft. NET\Framework\v4 the. 0.30319\aspnet_isapi.dll file and click OK.
          Note: Do not select Check whether the file exists.
        4. Click OK in the two dialog boxes to complete the IIS wildcard support configuration.
      2. In the bin directory, right-click the test.jpg and select Properties.
      3. Click HTTP Header.
      4. Select Expiration, specify an expiration time, and click OK.
      5. To set a TTL value for other formats of files in the bin directory, you must modify the IIS configuration file. Syntax:
        Note: To modify a file, you need to disable the IIS admin Service in the service.
        1. Use Notepad to open IIS's configuration file, which is in the C:\WINDOWS\system32\inetsrv\MetaBase.xml (IIS6 Settings) directory.
        2. Look for the /bin/test.jpg and find the cache settings for test.jpg files in the bin directory.
        3. Change the test.jpg to *.jpg and save.
      6. Perform the preceding steps to set a TTL value for other formats of files.

How to Set Nginx cache policy

The HTTP Headers module allows you to set custom HTTP headers. You can use the add_header and expires directives to set an NGINX cache policy.

 
Commands Syntax Default value Parameter
add_header add_header name value none
  • http
  • server
  • location
expires
Note: This command specifies whether to mark an expiration time in the response and how to mark it.
expires [time|epoch|max|off]
 
Description
  • time: controls the value of the Cache-Control header. A negative value indicates no-cache.
  • epoch: Set the Expires header to 1 January, 1970 00:00:01 GMT.
  • max: Set the Expires header to 31 December 2037 23:59:59 GMT to maximize Cache-Control to 10 years.
  • off: disables adding or modifying the Expires and Cache-Control response headers.
expires off
  • http
  • server
  • location
 
  • Use the expires directive to set an NGINX cache policy. Examples:
    • Set the expiration time of the PHP file type to 1 hour. After

      is set, the system displays a similar message as follows.

    • Set the file type of PHP to no-cache. The cache server does not cache the file. After

      is set, the system displays a similar message as follows.

  • Use add_header settings to set dynamic PHP files to not cache.
    location ~ .*\.php$ {
        if ($request_uri !~ ^/dynamicimg/) {
            add_header              Cache-Control "no-cache";
            add_header              Pragma no-cache;
        }
      }

How to set up Apache cache policy

You can set the cache policy of Apache by using the mod_expires and mod_headers modules of Apache.
  • By using the mod_expires module
    • The mod_expires module allows you to control the HTTP Expires and Cache-Control header content by using a configuration file. This module automatically generates the Expires and Cache-Control labels in the header information about a page. This can reduce the access frequency and the number of requests from the clients to minimize the amount of data transfer and accelerate content delivery.
    • Mod_expires is one of Apache's many modules that is relatively simple to configure. It includes three instructions in total.
      • ExpiresActive: enables or disables the generation of the Expires and Cache-Control headers.  
      • ExpiresByType: sets a TTL value for documents of the MIME type, such as text or HTML documents.
      • ExpiresDefault: sets a default TTL value for all documents.
    • The expiration time is written as follows.
      "access plus 1 month"
      "access plus 4 weeks"
      "now plus 30 days"
      "modification plus 5 hours 3 minutes"
      A2592000
      M604800
      Note:
      • The expressions of access, now, and A all specify that the TTL value starts from the access time.
      • Both the expressions of modification and M specify that the TTL value starts from the latest modification time of the accessed file.
    • This writing method only works for static files and is not valid for dynamic pages generated by scripts. Configure the instance as follows.
      ExpiresActive On (mod_expires function is enabled) 
      ExpiresDefault "access plus 6 months" (default expiration time is 6 months)
      ExpiresByType image/* "access plus 10 years" (file type cache time of image is 10 years)
      ExpiresByType text/* "access plus 10 years" (text type cache time is 10 years)
      ExpiresByType application/* "access plus 30 minutes" (application file type cache time is 30 minutes)
      The verification result is as follows. The image/jpeg cache time is 315360000s(10 years).

    • If the image/jpeg is set to not cache, the max-age is set to 0s.
      #ExpiresByType image/* "access plus 10 years"
      ExpiresByType image/*  A0
      If an output similar to the following one is displayed, Python is installed.

  • mod_headers module
     # YEAR
        Header set Cache-Control "max-age=2592000"
        # WEEK
        Header set Cache-Control "max-age=604800"
        # NEVER CACHE
        Header set Expires "Thu, 01 Dec 2003 16:00:00 GMT"
        Header set Cache-Control "no-store, no-cache, must-revalidate"
        Header set Pragma "no-cache"

Default cache policy of Alibaba Cloud Content Delivery Network nodes

The following is the default cache policy for Alibaba Cloud Content Delivery Network nodes:

Note:

  • Website developers and technical engineers have a better understanding of the business logic and static and dynamic factors of their websites. We recommend that you set the TTL through the Alibaba Cloud CDN console based on the file type and directory. For more information, see Set cache expiration time.
  • If you have already configured a cache policy, the default cache policy does not take effect.
  • By default, the Alibaba Cloud Content Delivery Network node calculates the default cache time based on the formula. For ease of understanding, the formula for calculating the default cache time and related examples are described here.
    • The calculation formula. The formula is divided into the following three steps.
      Note: The cache time is t, in seconds (s). Curtime is the current time, and Last_Modified is the Last-Modified response header of the origin server.
      1. Calculates the value of t1.
        t1 = (Curtime - Last_Modified) * 0.1
      2. Compare t1 calculated in the previous step with 10, and take the maximum value as t2.
        t2 = max(10,t1)
      3. The value of t2 calculated in the previous step is compared with the 3600 value. If the minimum value is t, t is the cache time.
        t = min(t2,3600)
    • An example is as follows:
      • If the Last-Modified of the object is 20140801 00:00:00 and the current time Curtime is 20140801 00:01:00, t1=(Curtime-Last_modified)*0.1=6s,t2=max(10,t1)=10s, and t=min(t 2,360 0)=10s, then the cache time is 10s.
      • If the Last-Modified of the object is 20140801 00:00:00 and the current time Curtime is 20140802 00:00:00, t1=(Curtime-Last_modified)*0.1=8640s,t2=max(10,t1)=8640s, and t=min(t 2,360)=3600s, then the cache time is 3600s.
      • If the Last-Modified of the object is 20140801 00:00:00 and the current time Curtime is 20140801 00:10:00, t1=(Curtime-Last_modified)*0.1=60s,t2=max(10,t1)=60s, and t=min(t 2,360 0)=60s, then the cache time is 60s.
  • If the response from the origin server does not contain the Last-Modified header but contains the ETag header, the retrieved object is more likely a static resource. The default TTL value for this object is set to the minimum value. You can use the dft_expires directive to configure the minimum value.
  • If the origin server does not Last-Modified the response header or ETag, the object is considered to be dynamic content, and its default cache time is set to 0.

Applicable scope

  • CDN