ETag
ETagHTTP 响应头是资源的特定版本的标识符,这可以让缓存更高效,并节省带宽,因为如果内容没有改变,web 服务器不需要发送完整的响应。

第二次请求的时候会添加一个 If-None-Match 请求头,去判断文件是否发生过变化。

修改 nginx 配置文件
// conf\nginx.conf
http {
etag off;
}
这样就没有了

Last-Modified
Last-Modified 是一个响应首部,其中包含源头服务器认定的资源做出修改的日期及时间。

第二次请求的时候会添加一个 If-Modified-Since 请求头,去判断文件是否发生过变化。

修改 nginx 配置文件
// conf\nginx.conf
http {
add_header Last-Modified "";
}
这样就没有了

Expires
Expires 响应头包含日期/时间, 即在此时候之后,响应过期。
使用 nginx 的 expires 指令添加对应的响应头。
修改 nginx 配置文件
// conf\nginx.conf
http {
expires 5s;
}

当我们再次访问的时候,可以看到 from memory cache

可以看到时间是非常快的

但是好像 html 文件并没有被缓存。是因为浏览器自动添加了请求头 Cache-Control:max-age=0,导致没有缓存起来。

Cache-Control
Cache-Control 通用消息头字段,被用于在 http 请求和响应中,通过指定指令来实现缓存机制。
修改 nginx 配置文件
// conf\nginx.conf
http {
add_header Cache-Control no-store;
}
总结










