nginx与浏览器缓存控制

1.浏览器缓存
http协议定义的缓存机制(Expires;Cache-control等)
2.浏览器无缓存
nginx与浏览器缓存控制_nginx
3.客户端有缓存

nginx与浏览器缓存控制_it_02

4.校验过期机制
校验是否过期 : Expires(1.0版本),Cache-Control(max-age) 1.1版本
协议中Etag头信息校验 : Etag
Last-Modified 头信息校验 : Last-modified

本地缓存失效后先Etag(有的话)校验

Etag:字符串
没etag时
Last-Modified校验
Last-Modified 年月日时间到秒

nginx与浏览器缓存控制_it_03

nginx对应的配置语法
expires
在response头里添加Cache-Control,Expires

Syntax: expires [modified] time;
expires epoch|max|off;
Default:expires off;
Context:http,server,location,if in location

配置
location ~.*\.html$ {
expires 24h;
}
不配置expires时

请求1.html 主要信息
第一次请求返回200

request
GET /1.html HTTP/1.1
Host: 192.168.1.250
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://192.168.1.250/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8

response
HTTP/1.1 200 OK
Server: nginx/1.12.1
Date: Thu, 16 Nov 2017 13:56:26 GMT
Content-Type: text/html
Content-Length: 0
Last-Modified: Thu, 16 Nov 2017 13:52:29 GMT
Connection: keep-alive
ETag: "5a0d981d-0"
Accept-Ranges: bytes
第二次请求返回304
request
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:192.168.1.250
If-Modified-Since:Thu, 16 Nov 2017 13:52:29 GMT
If-None-Match:"5a0d981d-0"
Referer:http://192.168.1.250/
Upgrade-Insecure-Requests:1

response
HTTP/1.1 304 Not Modified
Server: nginx/1.12.1
Date: Thu, 16 Nov 2017 13:56:28 GMT
Last-Modified: Thu, 16 Nov 2017 13:52:29 GMT
Connection: keep-alive
ETag: "5a0d981d-0"

Cache-Control:max-age=0 是浏览器加的头(与服务端无关) 表示每次都向服务端验证
请求头里
If-Modified-Since:Thu, 16 Nov 2017 13:52:29 GMT 和服务端文件修改时间做比较
不同(Last-Modified)就获取文件内容,没改变就返回304,改变了就重新获取文件内容

服务端改变该文件内容

request
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:192.168.1.250
If-Modified-Since:Thu, 16 Nov 2017 13:52:29 GMT
If-None-Match:"5a0d981d-0"
Referer:http://192.168.1.250/
Upgrade-Insecure-Requests:1

response

Accept-Ranges:bytes
Connection:keep-alive
Content-Length:328318
Content-Type:text/html
Date:Thu, 16 Nov 2017 14:06:12 GMT
ETag:"5a0d9b50-5027e"
Last-Modified:Thu, 16 Nov 2017 14:06:08 GMT
Server:nginx/1.12.1

发现
Last-Modified的时间和If-Modified-Since 不一致
ETag 和 If-None-Match 不一致
如果If-Modified-Since的时间比服务器当前时间(当前的请求时间request_time)还晚,会认为是个非法请求

当开启expires后

清缓存

请求response
Accept-Ranges:bytes
Cache-Control:max-age=86400
Connection:keep-alive
Content-Length:328318
Content-Type:text/html
Date:Thu, 16 Nov 2017 14:26:20 GMT
ETag:"5a0d9b50-5027e"
Expires:Fri, 17 Nov 2017 14:26:20 GMT
Last-Modified:Thu, 16 Nov 2017 14:06:08 GMT
Server:nginx/1.12.1

会多出 Expires:Fri, 17 Nov 2017 14:26:20 GMT
再次请求
request
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:192.168.1.250
If-Modified-Since:Thu, 16 Nov 2017 14:06:08 GMT
If-None-Match:"5a0d9b50-5027e"
Upgrade-Insecure-Requests:1
发现 Cache-Control:max-age=0 依然不变,和服务端返回无关

修改服务端文件内容
返回200

浏览器强制刷新
Cache-Control:no-cache
Connection:keep-alive
Host:192.168.1.250
Pragma:no-cache
返回200