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)等字段来实现. 这 ...
随机推荐
- java中的继承与oc中的继承的区别
为什么要使用继承? 继承的好处: (1)抽取出了重复的代码,使代码更加灵活 (2)建立了类和类之间的联系 继承的缺点: 耦合性太强 OC中的继承 1.OC中不允许子类和父类拥有相同名称的成员变量名:( ...
- 前端SEO技巧
前几天在慕课网上学习了“SEO在网页制作中的应用”,觉得挺好.挺有用的,今天,特此做了一个小小的笔记,也算是对学习过后的一个总结. 一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时, ...
- C#操作注册服务卸载服务启动服务停止服务.. .
using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; usi ...
- 重置按钮小tip—为何不能重置表单数据呢
刚开始学html的同志有时候可能会遇到一个问题,就是为什么在编辑页面里面的重置按钮总是不起作用呢不清空数据呢?接下来就说明一下原因. Reset 对象 Reset 对象代表 HTML 表单中的一个重置 ...
- jQuery学习之:Validation表单验证插件
http://polaris.blog.51cto.com/1146394/258781/ 最近由于公司决定使用AJAX + Struts2来重构项目,让我仔细研究一下这两个,然后集中给同事讲讲,让每 ...
- 为什么angularjs使用ui-router时要使用html5Mode?
为什么我们要在使用angular ui-router时要使用html5Mode=true这个呢? 在angular中,你在访问链接时,可能访问的链接为"#/link". 如果你设置 ...
- Screen Orientation for Windows Phone
http://msdn.microsoft.com/en-us/library/windows/apps/jj207002(v=vs.105).aspx
- Java 对象拷贝方式
(1)BeanUtils.cloneBean()使用: http://www.cnblogs.com/fervour/archive/2009/12/18/1627868.html package c ...
- chrome浏览器 模拟访问移动端
谷歌Chrome浏览器,可以很方便地用来当3G手机模拟器.在Windows的[开始]-->[运行]中输入以下命令,启动谷歌浏览器,即可模拟相应手机的浏览器去访问3G手机网页: 谷歌Android ...
- 完成对数据库的CRUD操作
PS:查询相对复杂,要处理结果集,增删改则不用. package it.cast.jdbc; import java.sql.Connection; import java.sql.ResultSet ...