[CDN Best Practices] Interpretation and Configuration Policy of CDN Cache Policy

Abstract:  As a content distribution network, CDN mainly caches resources on CDN nodes, and then subsequent access can directly return resources to clients through CDN nodes, instead of returning to the origin server to speed up requests. So what requests does CDN accelerate? What are its caching rules and caching time? What kind of caching rules are more reasonable? This article explains the caching rules of CDN.

CDN cache availability

CDN supports all common HTTP requests. Please refer to Table 1 for details on the support of different request methods. But it should be noted that although these types of HTTP requests are supported, not all request methods will be cached. CDN only caches GET requests, and does not cache other requests. It only serves as an intermediate proxy and forwarding function. Therefore, we recommend that the origin of the site added to the CDN should be able to achieve dynamic and static separation. Separate the content of dynamic requests and static requests into two sites, and CDN only accelerates the resources in the static site.

Table 1. CDN support for HTTP requests

 

 

Note :

1. When the CDN processes the HEAD request, it will convert the HEAD request into a GET request back to the origin, so the origin log records the GET request.

2. CDN only supports POST and PUT to send HTTP requests with a request body (BODY).

 

CDN cache configuration

A screenshot of CDN's cache configuration is shown in Figure 1. The cache configuration includes two forms of directory and suffix name, and different weights can be set for different cache configurations to determine their priority. The suffix name is the cache rule set for a specific suffix name, and the directory is valid for all files in the directory and its subdirectories. When the cache of the suffix name and the directory is set for the same file at the same time, it will be selected according to the priority weight first. If the priority weight is the same, the suffix name policy will take precedence.

 

Figure 1. Schematic diagram of CDN cache configuration

 

CDN Caching Rules

In general, all the caching rules of CDN can be judged according to the content expressed in Figure 2. Below we interpret the detailed content information in Figure 2.

 

Figure 2. Schematic diagram of CDN caching rules

 

1. Whether a resource is cached by a CDN first depends on the cache settings configured by the origin site for the resource. Because the non-cache policy of the origin site is controlled by the user, it has the highest priority. When the origin site is configured with a cache rule, it will go to 2. And if there is no configuration, go to 3, and for browser caching, go to 7.

2. When the origin site is configured with the following rules, the CDN will consider that the resource origin site does not allow CDN caching, and the browser will not do caching in this case, so it will return to the origin site every time the resource is requested. , cache acceleration is not possible, if not, turn to 4:

1) There is one of s-maxage=0, no-cache, no-store, private

2) If there is no s-maxage or s-maxage=0, and there is max-age=0.

3) With Pragma: no-cache

3. If there is no cache rule configured on the origin site, you need to determine whether the CDN console is configured with a cache rule. If a cache rule is configured, go to 4, and if no cache rule is configured, go to 5.

4. It is necessary to determine whether the CDN console has a cache rule configured. If a cache rule is configured, then

The cache policy on the CDN will override the cache policy of the origin site, and the priority of the console cache configuration is:

1) The higher the weight, the better the priority;

2) The same priority suffix name priority is higher than the directory priority;

3) The same weight and the same priority are randomly matched (it is recommended to avoid the third scenario), if no cache rule is configured, go to 6.

5. It needs to be cached according to the cache rules of the origin site. Common origin site cache rules include Cache-Control and Expires headers. According to the HTTP protocol, the priority of Cache-Control is higher than the Expires header, and the s-maxage setting is higher than max-age set up.

6. The CDN default caching rules must be followed. The default caching rules include:

1) Files whose response headers do not include Etag or Last-Modified headers are not cached by default (such files are generally considered to be dynamic files);

2) Files without Last-Modified headers are cached for 10 seconds by default but have Etag headers;

3)对于有 Last-Modified 头的会按照(当前时间 - Last-Modified 时间) * 0.1,并且将其限制在 [10,3600] 区间内。

7. 对于源站设置了不缓存的规则时浏览器是不缓存的;如果 CDN 修改了 Cache-Control 或者 Expires 头时,浏览器会按照该修改头缓存;如果没有修改即会按照源站的策略缓存。

CDN 缓存情况查看

常见通过 HTTP 的响应头查看具体 CDN 的缓存情况,如图 3 即是常见的通过 CDN 访问的响应头,可以根据 X-Cache 查看当前该资源是否有在 CDN的 L1 节点缓存,而查看 L2 节点的缓存情况则需要根据 Via 头中的第一部分查看,如果是 M 即是 MISS,没有命中的状态;而出现 H 即是命中缓存。X-Swift-SaveTime 表示该资源缓存到 CDN 节点上的 GMT 时间(相比于北京时间晚8小时);而 X-Swift-CacheTime 表示该资源在 CDN 节点上缓存多长时间会过期。

 

图 3. CDN 缓存情况示意图

作者:烨烁

本文为云栖社区原创内容,未经允许不得转载,如需转载请发送邮件至[email protected]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444701&siteId=291194637
cdn