Firefox访问CSDN论坛所遇到的页面缓存问题
archu 2011-09-30 04:41:22 Firefox访问CSDN论坛的时候总是显示缓存的内容,不知道大家是否也遇到这个问题?我已经测试了多台机器,都有这个问题。不过IE访问就没有问题。
以下是我的详细分析:
第一遍访问论坛(比如https://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头。