Heim  >  Artikel  >  Backend-Entwicklung  >  出现http 200 from cache的诡异问题

出现http 200 from cache的诡异问题

WBOY
WBOYOriginal
2016-09-05 08:59:541165Durchsuche

所用浏览器:Pc端chrome、手机里的各种浏览器
访问同一个这样的url地址:http://xxx.aaa.bbb.com/ship.html

在测试环境中:有的同学访问了这个地址,再次访问时,页面仍然显示的是上次访问该页面时的数据,并不是最新数据。查看其http,有几个重要的参数如下:200、from cache。而有的同学每次访问这个地址,页面都是最新数据,查看其http的重要几个参数,没有出现from cache。

from cache 懂些,搞不懂的是访问同一台服务器为什么有的同学访问时出现from cache,而有的同学访问时都是从服务器加载最新的页面数据。这跟什么有关,什么影响了它?

注:

  • 在正式的线上环境,不存在此问题,访问正常,每次都是从服务器加载最新的页面数据。

  • ship.html并不是直正的静态页面,是伪静态。

  • 已经设置过了如下头:
    <meta http-equiv="Expires" content="-1">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Pragma" content="no-cache">

回复内容:

所用浏览器:Pc端chrome、手机里的各种浏览器
访问同一个这样的url地址:http://xxx.aaa.bbb.com/ship.html

在测试环境中:有的同学访问了这个地址,再次访问时,页面仍然显示的是上次访问该页面时的数据,并不是最新数据。查看其http,有几个重要的参数如下:200、from cache。而有的同学每次访问这个地址,页面都是最新数据,查看其http的重要几个参数,没有出现from cache。

from cache 懂些,搞不懂的是访问同一台服务器为什么有的同学访问时出现from cache,而有的同学访问时都是从服务器加载最新的页面数据。这跟什么有关,什么影响了它?

注:

  • 在正式的线上环境,不存在此问题,访问正常,每次都是从服务器加载最新的页面数据。

  • ship.html并不是直正的静态页面,是伪静态。

  • 已经设置过了如下头:
    <meta http-equiv="Expires" content="-1">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Pragma" content="no-cache">

如果服务器没有明确定义缓存控制,不同的客户端可能会有不同的缓存机制。
如果你希望用户每次获取最新数据可以这样设置 meta tag :

<code class="html"><meta http-equiv="cache-control" content="max-age=0">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT">
<meta http-equiv="pragma" content="no-cache"></code>

设置meta tag只对html页面有效,更好的方法是在 http 响应头中设置缓存控制:

<code class="http">Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0</code>

php设置 http 响应头的代码:

<code class="php">header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.</code>

使用缓存有利于加快页面加载速度和减少资源消耗,你应该根据具体情况调整缓存策略。

参考

http://stackoverflow.com/ques...
http://stackoverflow.com/ques...

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn