美文网首页CDN
CDN | CDN缓存规则及优先级

CDN | CDN缓存规则及优先级

作者: 奶茶不要奶不要茶 | 来源:发表于2022-06-21 23:11 被阅读0次

    阿里云CDN

    image.png

    参考文档:阿里云CDN - 配置缓存过期时间


    1.源站响应pragma:no-cachecache-control:no-cache(或者no-store,或者max-age=0)时,不缓存。

    一条缓存规则,该规则表示html后缀的文件会缓存5分钟。

    image.png
    源站配置 Pragma 响应头部
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        keepalive_timeout   65;
        types_hash_max_size 4096;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        server {
            listen       80;
            server_name  alicdn.xxx.xyz;
            root         /usr/share/nginx/html;
            add_header   Pragma "no-cache";
            #add_header   Cache-Control "no-cache";
            #add_header   Cache-Control "no-store";
            #add_header   Cache-Control "max-age=0";
    
            location / {
            }
        }
    }
    
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/index.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /index.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 4833
    < Connection: keep-alive
    < Vary: Accept-Encoding
    < Date: Tue, 21 Jun 2022 11:50:41 GMT
    < Last-Modified: Fri, 16 May 2014 15:12:48 GMT
    < ETag: "53762af0-12e1"
    < Pragma: no-cache
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655812241
    < Via: cache55.l2cn2630[28,28,200-0,M], cache39.l2cn2630[30,0], vcache10.cn838[85,85,200-0,M], vcache15.cn838[86,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 11:50:41 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42316558122416256405e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/index.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /index.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 4833
    < Connection: keep-alive
    < Vary: Accept-Encoding
    < Date: Tue, 21 Jun 2022 11:50:45 GMT
    < Last-Modified: Fri, 16 May 2014 15:12:48 GMT
    < ETag: "53762af0-12e1"
    < Pragma: no-cache
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655812245
    < Via: cache55.l2cn2630[36,36,200-0,M], cache58.l2cn2630[38,0], vcache10.cn838[48,48,200-0,M], vcache16.cn838[56,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 11:50:45 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42416558122452462110e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/index.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /index.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 4833
    < Connection: keep-alive
    < Vary: Accept-Encoding
    < Date: Tue, 21 Jun 2022 11:50:50 GMT
    < Last-Modified: Fri, 16 May 2014 15:12:48 GMT
    < ETag: "53762af0-12e1"
    < Pragma: no-cache
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655812250
    < Via: cache55.l2cn2630[35,34,200-0,M], cache29.l2cn2630[36,0], vcache10.cn838[46,46,200-0,M], vcache8.cn838[48,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 11:50:50 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41c16558122499894634e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    不管访问多少次,X-Cache 始终是 MISS,X-Swift-CacheTime 也一直为 0。


    源站配置 Cache-Control: no-cache 响应头部

    nginx配置文件内容就不贴了。

    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/hello.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /hello.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 6
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 12:00:53 GMT
    < Last-Modified: Tue, 21 Jun 2022 12:00:49 GMT
    < ETag: "62b1b2f1-6"
    < Cache-Control: no-cache
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655812853
    < Via: cache16.l2cn1851[146,145,200-0,M], cache25.l2cn1851[148,0], vcache16.cn838[218,218,200-0,M], vcache19.cn838[221,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 12:00:53 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42716558128532017622e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/hello.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /hello.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 6
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 12:01:02 GMT
    < Last-Modified: Tue, 21 Jun 2022 12:00:49 GMT
    < ETag: "62b1b2f1-6"
    < Cache-Control: no-cache
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655812862
    < Via: cache16.l2cn1851[128,128,200-0,M], cache12.l2cn1851[129,0], vcache16.cn838[148,149,200-0,M], vcache20.cn838[151,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 12:01:02 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42816558128623182700e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Cache 为 MISS,X-Swift-CacheTime 为 0,CDN不缓存。


    源站配置 Cache-Control: no-store 响应头部
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/hello.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /hello.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 6
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 12:05:30 GMT
    < Last-Modified: Tue, 21 Jun 2022 12:00:49 GMT
    < ETag: "62b1b2f1-6"
    < Cache-Control: no-store
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655813130
    < Via: cache16.l2cn1851[53,52,200-0,M], cache7.l2cn1851[55,0], vcache16.cn838[71,70,200-0,M], vcache9.cn838[74,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 12:05:30 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41d16558131302937074e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Cache 为 MISS,X-Swift-CacheTime 为 0,CDN不缓存。


    源站配置 Cache-Control: max-age=0 响应头部
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/hello.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /hello.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 6
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 12:06:28 GMT
    < Last-Modified: Tue, 21 Jun 2022 12:00:49 GMT
    < ETag: "62b1b2f1-6"
    < Cache-Control: max-age=0
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655813189
    < Via: cache16.l2cn1851[57,57,200-0,M], cache13.l2cn1851[59,0], vcache16.cn838[74,74,200-0,M], vcache13.cn838[75,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 12:06:29 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42116558131889331821e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Cache 为 MISS,X-Swift-CacheTime 为 0,CDN不缓存。


    干掉 Pragma 和 Cache-Control 响应头部

    试下能否正常被CDN缓存。

    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/hello.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /hello.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 6
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 12:08:21 GMT
    < Last-Modified: Tue, 21 Jun 2022 12:00:49 GMT
    < ETag: "62b1b2f1-6"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655813301
    < Via: cache16.l2cn1851[68,68,200-0,M], cache8.l2cn1851[70,0], vcache16.cn838[86,85,200-0,M], vcache17.cn838[87,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 12:08:21 GMT
    < X-Swift-CacheTime: 300
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42516558133016826681e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/hello.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /hello.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 6
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 12:08:21 GMT
    < Last-Modified: Tue, 21 Jun 2022 12:00:49 GMT
    < ETag: "62b1b2f1-6"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655813301
    < Via: cache16.l2cn1851[68,68,200-0,M], cache8.l2cn1851[70,0], vcache16.cn838[0,0,200-0,H], vcache7.cn838[1,0]
    < Age: 4
    < X-Cache: HIT TCP_MEM_HIT dirn:11:196965349
    < X-Swift-SaveTime: Tue, 21 Jun 2022 12:08:21 GMT
    < X-Swift-CacheTime: 300
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41b16558133051123575e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Cache 变成了 HIT,X-Swift-CacheTime 变成了 300,也就是CDN缓存5分钟。


    响应头部:

    • Pragma: no-cache
    • Cache-Control: no-cache
    • Cache-Control: no-store
    • Cache-Control: max-age=0

    总结:源站如果有这4个响应头部,CDN不缓存。


    2.CDN控制台设置的缓存过期时间或者状态码过期时间

    缓存规则权重不同,有两条缓存规则,其中 /static/ 目录类型的权重最大,意味着优先级最高。

    请求 /static/ 目录下 html 后缀的文件,看下匹配到哪条规则?

    image.png
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/tt.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/tt.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 13:07:46 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:01:27 GMT
    < ETag: "62b1c127-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655816866
    < Via: cache26.l2cn1851[146,145,200-0,M], cache10.l2cn1851[148,0], vcache18.cn838[208,208,200-0,M], vcache2.cn838[210,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 13:07:46 GMT
    < X-Swift-CacheTime: 120
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41616558168662691031e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/tt.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/tt.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 13:14:02 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:01:27 GMT
    < ETag: "62b1c127-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655817242
    < Via: cache22.l2cn3017[191,191,304-0,M], cache15.l2cn3017[193,0], vcache18.cn838[0,0,200-0,H], vcache19.cn838[1,0]
    < Age: 3
    < X-Cache: HIT TCP_MEM_HIT dirn:11:264004211
    < X-Swift-SaveTime: Tue, 21 Jun 2022 13:14:02 GMT
    < X-Swift-CacheTime: 120
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42716558172454185948e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Swift-CacheTime: 120,这是匹配到了 /static/ 目录类型的规则。


    缓存规则权重相同,html 文件后缀名的规则创建时间最早,正常情况应该是匹配到该条规则。

    image.png
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/kk.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/kk.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 13:18:34 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:18:30 GMT
    < ETag: "62b1c526-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655817514
    < Via: cache42.l2cn1851[418,418,200-0,M], cache23.l2cn1851[420,0], vcache2.cn838[443,443,200-0,M], vcache6.cn838[446,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 13:18:34 GMT
    < X-Swift-CacheTime: 300
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41a16558175139287275e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/kk.html
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/kk.html HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/html
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 13:18:34 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:18:30 GMT
    < ETag: "62b1c526-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655817514
    < Via: cache42.l2cn1851[418,418,200-0,M], cache23.l2cn1851[420,0], vcache2.cn838[0,0,200-0,H], vcache8.cn838[1,0]
    < Age: 3
    < X-Cache: HIT TCP_MEM_HIT dirn:11:50340861
    < X-Swift-SaveTime: Tue, 21 Jun 2022 13:18:34 GMT
    < X-Swift-CacheTime: 300
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41c16558175176094995e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Swift-CacheTime: 300,确实匹配到了 html 文件后缀名规则。

    总结:权重不同,权重最大的规则优先级最高;权重相同,最早创建的规则的优先级最高。

    页面也没显示规则的创建时间,有时候根本不知道哪条是最早创建的,推荐使用不同权重的缓存规则会比较好。


    3.源站配置其他缓存规则,优先级由高至低为:cache-control>expires>last-modified>etag

    CDN不要设置缓存规则,然后nginx配置内容如下,Cache-Control设置为60秒,Expires设置为120秒。

    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
    
        access_log  /var/log/nginx/access.log  main;
    
        keepalive_timeout   65;
        types_hash_max_size 4096;
    
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
    
        server {
            listen       80;
            server_name  alicdn.xxx.xyz;
            root         /usr/share/nginx/html;
            add_header   Cache-Control "max-age=60";
    
            location / {
                expires 120s;
            }
        }
    }
    
    [root@cdn ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/uu.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 1.13.19.212...
    * Connected to alicdn.xxx.xyz (1.13.19.212) port 80 (#0)
    > GET /static/uu.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    > 
    < HTTP/1.1 200 OK
    < Server: nginx/1.20.1
    < Date: Tue, 21 Jun 2022 13:42:52 GMT
    < Content-Type: text/plain
    < Content-Length: 7
    < Last-Modified: Tue, 21 Jun 2022 13:38:58 GMT
    < Connection: keep-alive
    < ETag: "62b1c9f2-7"
    < Expires: Tue, 21 Jun 2022 13:44:52 GMT
    < Cache-Control: max-age=120
    < Cache-Control: max-age=60
    < Accept-Ranges: bytes
    < 
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@cdn ~]# 
    

    注意IP地址,这是源站服务器的。


    源站响应头部有 Cache-ControlExpiresLast-ModifiedETag,测试看看哪个优先级最高。

    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/uu.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/uu.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 13:39:04 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:38:58 GMT
    < ETag: "62b1c9f2-7"
    < Expires: Tue, 21 Jun 2022 13:41:04 GMT
    < Cache-Control: max-age=120
    < Cache-Control: max-age=60
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655818744
    < Via: cache64.l2cn3014[151,151,200-0,M], cache25.l2cn3014[152,0], vcache6.cn838[0,0,200-0,H], vcache13.cn838[1,0]
    < Age: 12
    < X-Cache: HIT TCP_MEM_HIT dirn:11:118732097
    < X-Swift-SaveTime: Tue, 21 Jun 2022 13:39:04 GMT
    < X-Swift-CacheTime: 60
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42116558187565354965e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Swift-CacheTime: 60,Cache-Control 的优先级高一些。


    在nginx配置中将 add_header Cache-Control "max-age=60"; 去掉再试试。

    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/uu.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/uu.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 13:54:21 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:38:58 GMT
    < ETag: "62b1c9f2-7"
    < Expires: Tue, 21 Jun 2022 13:56:21 GMT
    < Cache-Control: max-age=120
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655819661
    < Via: cache64.l2cn3014[140,139,304-0,M], cache74.l2cn3014[141,0], vcache6.cn838[0,0,200-0,H], vcache4.cn838[2,0]
    < Age: 7
    < X-Cache: HIT TCP_MEM_HIT dirn:11:118732097
    < X-Swift-SaveTime: Tue, 21 Jun 2022 13:54:21 GMT
    < X-Swift-CacheTime: 120
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41816558196681797719e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Swift-CacheTime: 120,缓存规则是用 Expires 的时间了。


    去掉nginx配置中的 expires 120s;

    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/ss.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/ss.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 13:58:35 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:58:31 GMT
    < ETag: "62b1ce87-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655819915
    < Via: cache24.l2cn3017[66,65,200-0,M], cache68.l2cn3017[67,0], vcache20.cn838[0,0,200-0,H], vcache9.cn838[3,0]
    < Age: 2
    < X-Cache: HIT TCP_MEM_HIT dirn:9:322963682
    < X-Swift-SaveTime: Tue, 21 Jun 2022 13:58:35 GMT
    < X-Swift-CacheTime: 10
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41d16558199170157504e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/ss.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/ss.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 14:09:13 GMT
    < Last-Modified: Tue, 21 Jun 2022 13:58:31 GMT
    < ETag: "62b1ce87-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655820553
    < Via: cache20.l2cn3014[60,60,304-0,M], cache9.l2cn3014[61,0], vcache20.cn838[72,77,200-0,H], vcache2.cn838[80,0]
    < Age: 0
    < X-Cache: HIT TCP_REFRESH_HIT dirn:2:971844841
    < X-Swift-SaveTime: Tue, 21 Jun 2022 14:09:13 GMT
    < X-Swift-CacheTime: 64
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41616558205535634889e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    X-Swift-CacheTime 会随着时间变化,这是因为缓存过期了,CDN重新去源站拉取,然后重新计算缓存过期时间。

    缓存过期时间的计算方式:

    1.先将 Last-Modified: Tue, 21 Jun 2022 13:58:31 GMT 时间转换为北京时间,+8小时得到时间是 2022-06-21 21:58:31。

    2.将北京时间再转换为时间戳,时间戳是 1655819911。

    3.用 Ali-Swift-Global-Savetime: 1655820553 减去 1655819911 得到 642。

    4.最后用 642 * 01 得到 64,也就是 X-Swift-CacheTime: 64


    ok,最后一步,将 Last-Modified 响应头部给干掉。

    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/gg.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/gg.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 14:29:35 GMT
    < ETag: "62b1d5c2-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655821775
    < Via: cache32.l2cn3017[59,58,304-0,M], cache27.l2cn3017[60,0], vcache1.cn838[0,0,200-0,H], vcache20.cn838[0,0]
    < Age: 1
    < X-Cache: HIT TCP_MEM_HIT dirn:9:210872654
    < X-Swift-SaveTime: Tue, 21 Jun 2022 14:29:35 GMT
    < X-Swift-CacheTime: 10
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42816558217766456558e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/gg.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/gg.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 14:31:37 GMT
    < ETag: "62b1d5c2-7"
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655821897
    < Via: cache32.l2cn3017[59,59,304-0,M], cache37.l2cn3017[61,0], vcache1.cn838[116,115,200-0,H], vcache4.cn838[117,0]
    < Age: 0
    < X-Cache: HIT TCP_REFRESH_HIT dirn:9:210872654
    < X-Swift-SaveTime: Tue, 21 Jun 2022 14:31:37 GMT
    < X-Swift-CacheTime: 10
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41816558218973346410e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    只有 Etag 响应头部,不管访问几次,依旧是 X-Swift-CacheTime: 10。

    响应头部:

    • Cache-Control
    • Expires
    • Last-Modified
    • Etag

    总结:源站如果有这4个响应头部,则 Cache-Control 的优先级最高,建议源站配置。


    4.源站返回的数据中ETaglast-modifiedcache-controlexpires这些缓存相关的响应头都没有携带,则默认不缓存

    将4个响应头部都干掉,看下CDN是否会缓存。

    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/ww.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/ww.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 14:41:13 GMT
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655822473
    < Via: cache56.l2cn3017[137,137,200-0,M], cache28.l2cn3017[138,0], vcache7.cn838[164,164,200-0,M], vcache19.cn838[166,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 14:41:13 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c42716558224729836659e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]# curl -svo /dev/null http://alicdn.xxx.xyz/static/ww.txt
    * About to connect() to alicdn.xxx.xyz port 80 (#0)
    *   Trying 110.52.196.216...
    * Connected to alicdn.xxx.xyz (110.52.196.216) port 80 (#0)
    > GET /static/ww.txt HTTP/1.1
    > User-Agent: curl/7.29.0
    > Host: alicdn.xxx.xyz
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: Tengine
    < Content-Type: text/plain
    < Content-Length: 7
    < Connection: keep-alive
    < Date: Tue, 21 Jun 2022 14:41:18 GMT
    < Accept-Ranges: bytes
    < Ali-Swift-Global-Savetime: 1655822478
    < Via: cache56.l2cn3017[55,55,200-0,M], cache56.l2cn3017[56,0], vcache7.cn838[152,151,200-0,M], vcache8.cn838[154,0]
    < X-Cache: MISS TCP_MISS dirn:-2:-2
    < X-Swift-SaveTime: Tue, 21 Jun 2022 14:41:18 GMT
    < X-Swift-CacheTime: 0
    < Timing-Allow-Origin: *
    < EagleId: 6e34c41c16558224780457425e
    <
    { [data not shown]
    * Connection #0 to host alicdn.xxx.xyz left intact
    [root@ov ~]#
    

    多次测试始终是 X-Cache: MISS 和 X-Swift-CacheTime: 0,这说明如果这4个响应头部都没了,CDN是不缓存的。

    响应头部:

    • Cache-Control
    • Expires
    • Last-Modified
    • Etag

    总结:源站如果没有有这4个响应头部,CDN不缓存。

    温馨提示
    Cache-Control Last-Modified Etag 这3个响应头部对CDN意义很大,建议源站都配置。

    相关文章

      网友评论

        本文标题:CDN | CDN缓存规则及优先级

        本文链接:https://www.haomeiwen.com/subject/wstkvrtx.html