http缓存之304 last-modified,cache-control:max-age,Etag等
因最近客户端慢的问题,系统分析了下http协议缓存问题。本文主要记录总结http缓存相关知识。
1. 讨论涉及的要点
访问返回类
> 访问返回200 OK
> 访问返回200 (from memory cache)
> 访问返回200 (from disk cache)
> 访问返回304 Not Modified
头设置类
> Cache-Control: max-age=604800
> Expires: Thu, 05 Jan 2017 12:24:24 GMT
2. 测试环境1
机器A作为客户端,IP: 10.0.0.2 chrome 版本 55.0.2883.87 m
机器B作为服务断, IP: 10.0.0.102 nginx 1.1.19 ubuntu 12.04
3. 测试1 nginx直接作为后端服务器
1) 修改nginx的配置文件
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
expires 7d;
}
增加了expires 7d;
2) 修改index.html
<html>
<head>
<title>Welcome to nginx!</title>
<script type="text/javascript" src="1.js"></script>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!123456</h1></center>
<a href="1.txt">1.txt</a>
</body>
</html>
增加一个引入js 1.js
增加一个超链接1.tx
4. 第一次访问http://10.0.0.102/index.html (记得清理浏览器缓存)


上图为index.html请求包

上图为index.html响应包


上图为1.js请求包

上图为1.js响应包
这是index.html和1.js都返回200 ok的情况
5. 第二次访问http://10.0.0.102/index.html 在地址栏敲回车键


上图为index.html请求包

上图为index.html响应包,此时 回 304 Not Modified ,并且服务器仅仅是回了状态码,没有将index.html数据写回客户端。

从整个抓包来看,没有看到1.js的请求包,证明此时针对1.js这个文件 连http请求都没有发。
再通过那个超链接做实验发现和1.js一样的结论,第一次回200 OK 第二次之后就会200 from xxx cache
cache有memory和disk之分,这个很好理解。
在已经缓存了的情况下:
在地址栏敲回车,是回304 。 被别人引用或者点击超链接时,回 200 form xxx cache
参考 http://guojianxiang.com/posts/2015-09-01-About_HttpStatus200_FromCache_And_304_NotModified.html 弯路2
6. 关于etag
nginx默认不支持etag, 可以使用插件完成。
nginx作者对其解释,参考 http://blog.csdn.net/21aspnet/article/details/6604805
“
Nginx中默认没有添加对Etag标识.Igor Sysoev的观点”在对静态文件处理上看不出如何Etag好于Last-Modified标识。”
Note:
Yes, it's addition,and it's easy to add, however, I do not see howETag is better than Last-Modified for static files. -Igor Sysoev
A nice short description is here:
http://www.mnot.net/cache_docs/#WORK
”
7. expires和cache-control中max-age区别
nginx是在后台指定expires的间隔(比如7天后超期),然后nginx会自动在http头中转换成max-gae的时间,和头中Expires会变换成具体的超期时刻(不是间隔了)
参考 https://www.bokeyy.com/post/high-performance-web-sites-rule3.html
“
Max-age:在 HTTP 头中按秒指定失效的时间,如下图。好处是不像方法 1(expries) 一样需要定时检查是否过期,一旦过期就需要指定新的时间,坏处是只有 HTTP/1.1 支持
”
8. tomcat对304的处理
分析了tomcat6 阅读其代码 一目了然
protected boolean checkIfModifiedSince(HttpServletRequest request,
HttpServletResponse response,
ResourceAttributes resourceAttributes)
throws IOException {
try {
long headerValue = request.getDateHeader("If-Modified-Since");
long lastModified = resourceAttributes.getLastModified();
if (headerValue != -1) { // If an If-None-Match header has been specified, if modified since
// is ignored.
if ((request.getHeader("If-None-Match") == null)
&& (lastModified < headerValue + 1000)) {
// The entity has not been modified since the date
// specified by the client. This is not an error case.
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.setHeader("ETag", resourceAttributes.getETag()); return false;
}
}
} catch (IllegalArgumentException illegalArgument) {
return true;
}
return true; }
HttpServletResponse.SC_NOT_MODIFIED 就是304状态
resourceAttributes.getLastModified(); 读取文件上次修改时间。
有兴趣的话可以继续分析tomcat对etag的处理。
--EOF--
http缓存之304 last-modified,cache-control:max-age,Etag等的更多相关文章
- [web] 200 OK (from cache) 与 304 Not Modified
为什么有的缓存是 200 OK (from cache),有的缓存是 304 Not Modified 呢?很简单,看运维是否移除了 Entity Tag.移除了,就总是 200 OK (from c ...
- 200 OK (from cache) 与 304 Not Modified
解释: 200 OK (from cache) 是浏览器没有跟服务器确认,直接用了浏览器缓存: 304 Not Modified 是浏览器和服务器多确认了一次缓存有效性,再用的缓存. 触发区别: 2 ...
- [HTTP Protocol] 200 OK (from cache)和304 Not Modified
含义 200 OK (from cache)直接从缓存中获取的内容并未请求服务器 304 Not Modified 请求服务器并和服务器比较 If-Modified-Since,若文件未改变,服务器返 ...
- 状态码为 200 from cache和304 Not modified的区别
1.请求状态码为 200 from cache: 表示该资源已经被缓存过,并且在有效期内,所以不再向浏览器发出请求,直接使用本地缓存. 如下图: 2.状态码为 304 Not modified: 表 ...
- SpringMVC的缓存对静态资源的影响 304 Not Modified
我们知道在springmvc的配置中,可以添加缓存,但是缓存到底对静态资源有什么影响? 测试 没有添加缓存 <mvc:resources mapping="/image/**" ...
- 图片缓存:浏览器刷新 和 304 Not Modified 与 If-Modified-Since 及 Cache-Control
关键词:浏览器刷新,304 Not Modified ,If-Modified-Since,Cache-Control,Expires 今天在用chrome浏览淘宝页面的时候,发现很多来自淘宝图片HT ...
- 200 from memory cache / from disk cache / 304 Not Modified 区别
三者情况有什么区别和联系,什么情况下会发生200 from memory cache 或 200 from disk cache 或 304 Not Modified? 200 from memory ...
- 304 Not Modified 简述
在客户端向服务端发送http请求时,若返回状态码为304 Not Modified 则表明此次请求为条件请求.在请求头中有两个请求参数:If-Modified-Since 和 If-None-Matc ...
- HTTP 200 OK和HTTP 304 Not modified的由来
这两个字段都和HTTP协议的缓存控制相关. 浏览器缓存机制是通过HTTP协议Header里的Cache-Control(或Expires)和Last-Modified(或 Etag)等字段来实现. 这 ...
随机推荐
- 【淘淘】Quartz作业存储与管理
一.Quartz作业管理和存储方式简介: 作业一旦被调度,调度器需要记住并且跟踪作业和它们的执行次数.如果你的作业是30分钟后或每30秒调用,这不是很有用.事实上,作业执行需要非常准确和即时调用在被调 ...
- 关于MFi认证你所必须要知道的事情
MFiLogo 前面我写了一篇文章<iOS App连接外设的几种方式>,其中EAP和NCM是都需要做MFi认证.对于苹果的MFI认证,对iOS开发的同学来说其实是一个比较陌生并且繁琐的to ...
- JS 之Blob 对象类型
原文 http://blog.csdn.net/oscar999/article/details/36373183 什么是Blob? Blob 是什么? 这里说的是一种Javascript的对象类型. ...
- 开篇一文 php 和 js 默认参数
啥话不说... <?php function setheight($height=50){ echo "这个高度是 $height <br />"; } seth ...
- Linux学习总结
1.软链接和硬链接 ln 命令可用来创建硬链接或是符号链接.它的使用方式有两种. ln file link 用来创建硬链接 ln -s item link 用来创建符号链接,这里的item可以是文件也 ...
- 通过Gulp使用Browsersync实现浏览器实时响应文件更改
Gulp是什么鬼 Browsersync又是什么鬼 如何安装使用Browsersync 安装 使用 效果图 参考 Gulp是什么鬼 Gulp是一种基于node.js的构建工具,有关构建工具的概念请移步 ...
- 简明 Git 命令速查表(中文版)
原文引用地址:https://github.com/flyhigher139/Git-Cheat-Sheet/blob/master/Git%20Cheat%20Sheet-Zh.md在Github上 ...
- F-并查集
Problem F Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) Total Sub ...
- scrollview 嵌套 折叠效果
------------------------------- --@ CreateDate: 2015.08.05 --@ Author: 王成成 --@ FileName: BaoSh ...
- php多线程详解
在说明多线程的题前,需要弄清楚以下几个问题 1,ts 和 nts的区别 Thread Safe和NoneThread Safe 先说windows的,在php官网,在windows区域有在文件下在有 ...