1. 浏览器缓存策略

1.1. 缓存校验

  • 浏览器无本地缓存的情况下:

201211281402437422.png

  • 浏览器存在本地缓存的情况下:

201211281402437422.png

1.2. 与缓存相关的HTTP字段

Fields Instroduction
Date 服务器返回响应的时间,UTC时区
Expires 1.0 中常用
Cache-Control 1.1 中常用
Last-Modified 请求的文件最后被修改的时间,精确到秒
Etag 由mtime,inode(apache)和size组成的特称字符串
If-None-Match 请求头中,用于匹配etag
If-Modified-Since 请求头中,用于匹配last-modified

1.3. 用户行为与缓存更新策略(google)

用户操作 Expires/Cache-Control Last-Modified/Etag
地址栏回车 有效 有效
页面链接跳转 有效 有效
新打开窗口 有效 有效
前进后退 有效 有效
F5 无效 有效
Ctrl+F5 无效 无效

2. 指令

2.1. expires

  • 指令

    1. Syntax: expires [modified] time ;
    2. Syntax: expires epoch | max | off ;
    3. Default: expires off ;
    4. Context: http,server,location,if in location
  • 说明

用于在响应头中添加 Expires 和 Cache-Control 字段,其中时间可以时正的也可以时负的.
Cache-COntrol的值取决于expires中的值:

expires Expires Cache-control
t <

0 | Date - t | Cache-Control: no-cache | | t

= 0 | Date + t | Cache-Control: max-age=t | | epoch | Thu, 01 Jan 1970 00:00:01 GMT | Cache-Control: no-cache | | max | Thu, 31 Dec 2037 23:55:55 GMT | Cache-Control: max-age=315360000 | | off | disable | disable |

时间格式:

m min
h hour
d day
M month
y 356

days |

2.2. etag

官方文档:https://nginx.org/en/docs/http/ngx_http_core_module.html#etag
nginx etag计算方式:文件最后修改时间(秒级)16进制,文件长度16进制。不涉及文件的md5信息。
apache etag计算方式:文件inode,最后修改时间(秒级)16进制,文件长度16进制。不涉及文件的md5信息。
默认开启etag,一般场景下无需配置。在负载均衡的场景中,静态资源建议使用共享存储,否则etag值会不一致。

3. 案例

3.1. 配置

针对静态文件: text/. image/. vedio/. js zip pdf 进行缓存,但是涉及到登陆相关的js不能缓存(^/gt..js$):[root@centos-81 ~]# cat /etc/nginx/nginx.conf

  1. map $sent_http_content_type $expires {
  2. default off;
  3. ~image/ max ;
  4. ~vedio/ 7d ;
  5. text/plain 24h;
  6. # text/css modified 24h; 不支持
  7. text/css 24h;
  8. text/html 24h;
  9. application/javascript 24h;
  10. application/pdf 30d;
  11. application/zip 30d;
  12. }
  13. expires $expires ;
  14. include /etc/nginx/conf.d/*.conf;

[root@centos-81 ~]# cat /etc/nginx/conf.d/localhost.conf

  1. location ~ ^/gt[a-z]+ {
  2. if ($request_uri ~ .*\.js$) {
  3. expires off ;
  4. }
  5. proxy_pass http://www.jsmlr.gov.cn ;
  6. }

3.2. 测试

[root@centos-50 ~]# curl -I -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81 # html 缓存成功

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 12:58:52 GMT
  4. Content-Type: text/html
  5. Content-Length: 24008
  6. Last-Modified: Tue, 14 Nov 2017 10:36:39 GMT
  7. Connection: keep-alive
  8. ETag: "5a0ac737-5dc8"
  9. Expires: Mon, 31 Dec 2018 12:58:52 GMT
  10. Cache-Control: max-age=86400
  11. Accept-Ranges: bytes

[root@centos-50 ~]# curl -I -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/images/jquery-1.9.1.min.js ## js 缓存成功

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 12:59:25 GMT
  4. Content-Type: application/javascript
  5. Content-Length: 92631
  6. Last-Modified: Wed, 26 Apr 2017 07:51:35 GMT
  7. Connection: keep-alive
  8. ETag: "59005187-169d7"
  9. Expires: Mon, 31 Dec 2018 12:59:25 GMT
  10. Cache-Control: max-age=86400
  11. Accept-Ranges: bytes

[root@centos-50 ~]# curl -I -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/a.pdf ## pdf 缓存成功

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 13:00:05 GMT
  4. Content-Type: application/pdf
  5. Content-Length: 192088019
  6. Last-Modified: Sat, 29 Dec 2018 04:17:00 GMT
  7. Connection: keep-alive
  8. ETag: "5c26f53c-b7307d3"
  9. Expires: Tue, 29 Jan 2019 13:00:05 GMT
  10. Cache-Control: max-age=2592000
  11. Accept-Ranges: bytes

[root@centos-50 ~]# curl -I -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/gtwzqxxgk/imagesls/xxgk-zfxx4.png ## 成功缓存

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 13:09:19 GMT
  4. Content-Type: image/png
  5. Content-Length: 15283
  6. Connection: keep-alive
  7. Last-Modified: Wed, 01 Mar 2017 05:44:28 GMT
  8. ETag: "3bb3-549a4ce4a6700"
  9. Accept-Ranges: bytes
  10. Access-Control-Allow-Origin: http://www.jsmlr.gov.cn:80/gtxx/
  11. Expires: Thu, 31 Dec 2037 23:55:55 GMT
  12. Cache-Control: max-age=315360000

[root@centos-50 ~]# curl -I -A “User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0” 192.168.1.81/gtwzqxxgk/js/Date/calendar.js ## 未缓存

  1. HTTP/1.1 200 OK
  2. Server: nginx/1.14.2
  3. Date: Sun, 30 Dec 2018 13:10:57 GMT
  4. Content-Type: application/javascript
  5. Content-Length: 18007
  6. Connection: keep-alive
  7. Last-Modified: Wed, 09 Jun 2010 01:33:28 GMT
  8. ETag: "4657-4888ee48caa00"
  9. Accept-Ranges: bytes
  10. Access-Control-Allow-Origin: http://www.jsmlr.gov.cn:80/gtxx/