当前位置:编程学习 > C#/ASP.NET >>

Firefox访问CSDN论坛所遇到的页面缓存问题

Firefox访问CSDN论坛的时候总是显示缓存的内容,不知道大家是否也遇到这个问题?我已经测试了多台机器,都有这个问题。不过IE访问就没有问题。

以下是我的详细分析:

第一遍访问论坛(比如http://forum.csdn.net/SList/IIS/)没有任何问题;
然后按F5对页面进行刷新,服务器返回304。


这个问题关键点在于Request Header里面的If-None-Match
1. 当Firefox第一次访问网页的时候,服务器端返回的ETag内容为””
GET /SList/IIS/ HTTP/1.1
Host: forum.csdn.net
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0) Gecko/20100101 Firefox/7.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive

HTTP/1.1 200 OK
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Content-Length: 112624
Via: 1.1 APS-PRXY-08
Expires: Thu, 29 Sep 2011 05:17:06 GMT
Date: Thu, 29 Sep 2011 05:18:51 GMT
Content-Type: text/html; charset=utf-8
ETag: ""
Server: nginx/0.7.68
Vary: Accept-Encoding
X-UA-Compatible: IE=EmulateIE7
X-Powered-By: ASP.NET
Cache-Control: public, max-age=30
Last-Modified: Thu, 29 Sep 2011 05:16:36 GMT
Vary: *

2. 第二次请求的时候,Firefox发出If-None-Match的头来匹配之前的ETag。ETag就相当于被访问的资源的版本号,如果第二次的时候资源确实被更新过的话,ETag的值应该是另外一个新值。但是CSDN每一次返回的Etag都是””,所以If-None-Match发过去之后服务器认为与ETag匹配上了,于是服务器返回304
GET /SList/IIS/ HTTP/1.1
Host: forum.csdn.net
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:7.0) Gecko/20100101 Firefox/7.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive
If-None-Match: ""

HTTP/1.1 304 Not Modified
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Via: 1.1 APS-PRXY-08
Age: 0
Expires: Thu, 29 Sep 2011 05:18:05 GMT
Date: Thu, 29 Sep 2011 05:19:50 GMT


而对于采用IE浏览器访问正常显示的情况,Request Header有点不一样,它多了一个If-Modified-Since的头,具体数据如下:
GET /SList/IIS/ HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Accept-Encoding: gzip, deflate, peerdist
Proxy-Connection: Keep-Alive
If-Modified-Since: Thu, 29 Sep 2011 06:41:10 GMT
If-None-Match: ""
Host: forum.csdn.net
Pragma: no-cache

根据RFC2616的定义,当If-None-Match匹配上之后,服务器还会去校验If-Modified-Since和其它的Header。所以IE访问的时候就没有问题。
If any of the entity tags match the entity tag of the entity that would have been returned in the response to a similar GET request (without the If-None-Match header) on that resource, or if "*" is given and any current entity exists for that resource, then the server MUST NOT perform the requested method, unless required to do so because the resource's modification date fails to match that supplied in an If-Modified-Since header field in the request.

结论
========
这完全不是浏览器的问题,而是CSDN的Web Server对于If-None-Match和Etag头的处理不恰当,建议干脆去掉Etag头,或者正确更新Etag头。
--------------------编程问答-------------------- 这个我用自己的firefox试了一下,没有发现lz的问题啊 --------------------编程问答-------------------- 进一步分析了一下,在家里直连CSDN不存在这个问题。应该是公司的代理服务器问题。因为Response里面包含Cache-Control: public, max-age=30

所以Response被缓存在代理服务器上了。 --------------------编程问答-------------------- 呵呵,那就不要用代理 --------------------编程问答-------------------- 代理缓存很多会有这个问题,比如微软的 ISA/TMG 就会出现  60错误,如果人多了访问相同的论坛也会出现缓存问题,代理这个东西真的不能满足所有的应用。人越多问题越多的。 --------------------编程问答-------------------- 这种问题只能当成一个话题 --------------------编程问答--------------------
引用 5 楼 lmaohuanl 的回复:
这种问题只能当成一个话题


为什么只能是一个话题而不是一个技术讨论呢?

提出这个问题只是希望大家能讨论一下HTTP Cache的机制。

比如你知道页面何时会被缓存在浏览器端?何时缓存在代理服务器上?何时又缓存在HTTP服务器端?
有哪些HTTP头会影响页面的缓存?分别是如何影响的?
什么样的页面可以缓存?什么样的页面不建议缓存?缓存超时时间设多长比较合适?
不同的浏览器,又是如何来处理缓存页面的?比如IE的"检查所存网页较新版本"的各项设置分别是什么含义?


对于Web开发人员来讲,这些基本的知识点如果你能掌握清楚了,能大大改善Web应用的访问速度。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,