如何控制网页页面缓存,在所有浏览器?
-
09-06-2019 - |
题
我们的调查已经向我们显示,不是所有的浏览器尊重http缓存指令在一个统一的方式。
出于安全原因,我们不希望某些页面,在我们的应用缓存, 有史以来, 通过网络浏览器。这个必须工作至少以下的浏览器:
- Internet Explorer6+
- Firefox1.5+
- Safari3+
- 歌剧9+
- 铬
我们要求来自一个安全的测试。登录后从我们的网站,你可以按下按钮回和缓存看的网页。
解决方案
介绍
正确的最低限度的标题,工作在所有提到的客户(和代理):
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
的 Cache-Control
是每HTTP1.1规范为客户代理人(和隐含地需要通过一些客户旁边 Expires
).的 Pragma
是每HTTP规格1.0史前的客户。的 Expires
是每HTTP1.0和1.1规范为客户和代理。在HTTP1.1, Cache-Control
优先于 Expires
, ,所以它的后所有HTTP1.0代理只。
如果你不关心IE6及其破碎的缓存服务时页HTTPS上只有 no-store
, 然后你可以忽略 Cache-Control: no-cache
.
Cache-Control: no-store, must-revalidate
Pragma: no-cache
Expires: 0
如果你不关心IE6也不HTTP1.0客户(HTTP1.1介绍了1997年),然后你可以忽略 Pragma
.
Cache-Control: no-store, must-revalidate
Expires: 0
如果你不关心HTTP1.0代理,然后你可以忽略 Expires
.
Cache-Control: no-store, must-revalidate
另一方面,如果服务器自动包括一个有效的 Date
标题,然后理论上可以忽略 Cache-Control
也和依靠 Expires
只。
Date: Wed, 24 Aug 2016 18:32:02 GMT
Expires: 0
但是,如果例如可能失败终端用户操作的操作系统中的日期与客户的软件是依靠它。
其他的 Cache-Control
等参数 max-age
是无关紧要,如果上述 Cache-Control
参数规定。的 Last-Modified
标题为包括在其他大多数的答案在这里是 只 如果你有趣 实际上想要的 来缓的请求,所以你不需要指定。
如何设置它?
使用PHP:
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1.
header("Pragma: no-cache"); // HTTP 1.0.
header("Expires: 0"); // Proxies.
使用Java Servlet,或Node.js:
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setHeader("Expires", "0"); // Proxies.
使用ASP。净视
Response.Cache.SetCacheability(HttpCacheability.NoCache); // HTTP 1.1.
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
Response.AppendHeader("Expires", "0"); // Proxies.
使用ASP.NET 网API:
// `response` is an instance of System.Net.Http.HttpResponseMessage
response.Headers.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
NoStore = true,
MustRevalidate = true
};
response.Headers.Pragma.ParseAdd("no-cache");
// We can't use `response.Content.Headers.Expires` directly
// since it allows only `DateTimeOffset?` values.
response.Content?.Headers.TryAddWithoutValidation("Expires", 0.ToString());
使用ASP.NET:
Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
Response.AppendHeader("Expires", "0"); // Proxies.
使用ASP:
Response.addHeader "Cache-Control", "no-cache, no-store, must-revalidate" ' HTTP 1.1.
Response.addHeader "Pragma", "no-cache" ' HTTP 1.0.
Response.addHeader "Expires", "0" ' Proxies.
用红宝石在轨道上,或Python/烧瓶:
headers["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
headers["Pragma"] = "no-cache" # HTTP 1.0.
headers["Expires"] = "0" # Proxies.
使用Python/Django:
response["Cache-Control"] = "no-cache, no-store, must-revalidate" # HTTP 1.1.
response["Pragma"] = "no-cache" # HTTP 1.0.
response["Expires"] = "0" # Proxies.
使用Python/金字塔:
request.response.headerlist.extend(
(
('Cache-Control', 'no-cache, no-store, must-revalidate'),
('Pragma', 'no-cache'),
('Expires', '0')
)
)
使用:
responseWriter.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
responseWriter.Header().Set("Pragma", "no-cache") // HTTP 1.0.
responseWriter.Header().Set("Expires", "0") // Proxies.
使用Apache .htaccess
文件:
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
使用HTML4:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
HTML元标记vs HTTP response headers
重要的是要知道的是,当一个HTML网页提供了一个HTTP连接,并一头是存在的 既 HTTP response headers和HTML <meta http-equiv>
标签,然后指定在HTTP响应的标题将得到优先于HTML元的标签。HTML元标记将只是使用网页时是从本地磁盘文件系统通过一个 file://
网址。也参看 W3HTML规章5.2.2.照顾这个时候你不要指定他们编程方式,因为该网络服务器可以即包括了一些缺省值。
一般来说,你最好只是 不 指定HTML元的标签,以避免混淆起动器,依靠硬HTTP response headers.此外,具体地说这些 <meta http-equiv>
标记 无效 在HTML5。只 http-equiv
中列出的值 HTML5 都是允许的。
验证的实际HTTP response headers
验证一个和其它的,你可以看到/debug他们在HTTP交通监测的网页浏览器的开发工具。你可以得到有按F12在铬/Firefox23+/IE9,然后开口"网络"或"净"标签小组,然后点击HTTP请求感兴趣的发现都详细说明关于HTTP请求和响应。的 下面截图 是从铬:
我想设置这些标题的文件下载过
首先,这个问题的答案是有针对性的"网页"(HTML网页),而不是"文件的下载"(PDF,zip,Excel,等等)。你最好有他们缓存和使用的一些文件的版本标识的某个地方在URI路径或查询到强迫重新下载一个改变的文件。当应用这些no-cache头上下载文件,无论如何,然后小心IE7/8错误时提供服务的一个文件的下载超过HTTPS而不是HTTP。为详细说明,请参阅 即不能下载foo。jsf.即是不是能够打开这个互联网网站。所请求的网站无法使用或不能找到.
其他提示
(嘿,大家:请不要只是盲目地复制、粘贴,所有的标题,你可以找到)
第一, 回来历史是按钮 不是一个缓存:
新鲜的模式(第4.2节)不一定适用于历史的机制。这是一个历史机制可以显示以前的表示,即使它已经过期。
在老HTTP规范的措辞更加强大,明确地告诉浏览器无视缓的指示返回键的历史。
回应该回去的时间(时的时间用户 是 登录)。它没有向前导航的先前开设网址。
然而,在实践中,高速缓冲存储器可以影响后面的按钮,在非常特殊的情况下:
- 页面 必须 被送过来 HTTPS, 否则这个缓冲破坏的不会是可靠的。另外,如果你不使用HTTPS,然后你页容易登录偷在许多其他方面。
- 你必须送
Cache-Control: no-store, must-revalidate
(一些观察浏览器no-store
和一些观察must-revalidate
)
你的 从来没有 需要任何:
<meta>
与缓头—它不工作的。完全无用的。post-check
/pre-check
—这是即仅指令只适用于 支持高速缓存 资源。- 发送相同的头两倍或十几个部分。一些PHP段出有实际上取代以前的标题,导致只有最后一个被送。
如果你想,你可以添加:
no-cache
或max-age=0
, ,这将会使资源(URL)"陈旧",并要求浏览器检查与服务,如果有一个较新的版本(no-store
已经意味着这个更强大的).Expires
与过去的某个日期为HTTP/1.0客户(虽 真的 HTTP/1.0-只有客户都是完全不存在的这些天)。
奖励: 新HTTP缓存RFC.
作为porneL说,你想要的是不要停用的高速缓存,但要关闭历史上的缓冲区。不同的浏览器有其自己微妙的方式来禁止历史的缓冲区。
在铬(v28.0.1500.95m)我们可以做到这一点只有通过 Cache-Control: no-store
.
在火狐(v23。0.1)任何一个这些会工作:
Cache-Control: no-store
Cache-Control: no-cache
(https仅)Pragma: no-cache
(https仅)Vary: *
(https仅)
在歌剧院(v12。15)我们只能做这个的 Cache-Control: must-revalidate
(https)。
在Safari(5.1.7,7534.57.2)中的任何一个这些会工作:
Cache-Control: no-store
<body onunload="">
在htmlCache-Control: no-store
(https仅)
在IE8(v8。0.6001.18702IC)中的任何一个这些会工作:
Cache-Control: must-revalidate, max-age=0
Cache-Control: no-cache
Cache-Control: no-store
Cache-Control: must-revalidate
Expires: 0
Cache-Control: must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
(https仅)Vary: *
(https仅)
结合上述给了我们这个解决方案适用于镀铬28、FireFox23,IE8、野生动物园5.1.7,并剧12.15: Cache-Control: no-store, must-revalidate
(https仅)
注意https是必要的,因为歌剧院不会停用历史缓冲区用于纯http页。如果你真的不能获得https和你准备好要忽视剧,你能做的最好是这样的:
Cache-Control: no-store
<body onunload="">
以下列出了原料日志的测试:
HTTP:
Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:歌剧12.15
成功:铬28、FireFox23,IE8、野生动物园5.1.7Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:歌剧12.15
成功:铬28、FireFox23,IE8、野生动物园5.1.7Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
失败:Safari5.1.7,歌剧12.15
成功:铬28、FireFox23,IE8Cache-Control: private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:Safari5.1.7,歌剧12.15
成功:铬28、FireFox23,IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: no-store
失败:Safari5.1.7,歌剧12.15
成功:铬28、FireFox23,IE8Cache-Control: no-store
<body onunload="">
失败:歌剧12.15
成功:铬28、FireFox23,IE8、野生动物园5.1.7Cache-Control: no-cache
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Vary: *
失败:铬28、FireFox23,IE8、野生动物园5.1.7,歌剧12.15
成功:没有Pragma: no-cache
失败:铬28、FireFox23,IE8、野生动物园5.1.7,歌剧12.15
成功:没有Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: must-revalidate, max-age=0
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: must-revalidate
Expires: 0
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
失败:铬28、FireFox23、野生动物园5.1.7,歌剧12.15
成功:IE8Cache-Control: private, must-revalidate, proxy-revalidate, s-maxage=0
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、FireFox23,IE8、野生动物园5.1.7,歌剧12.15
成功:没有
HTTPS:
Cache-Control: private, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
<body onunload="">
失败:铬28、FireFox23,IE8、野生动物园5.1.7,歌剧12.15
成功:没有Cache-Control: private, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
<body onunload="">
失败:铬28、FireFox23,IE8、野生动物园5.1.7,歌剧12.15
成功:没有Vary: *
失败:铬28、野生动物园5.1.7,歌剧12.15
成功:FireFox23,IE8Pragma: no-cache
失败:铬28、野生动物园5.1.7,歌剧12.15
成功:FireFox23,IE8Cache-Control: no-cache
失败:铬28、野生动物园5.1.7,歌剧12.15
成功:FireFox23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0
失败:铬28、野生动物园5.1.7,歌剧12.15
成功:FireFox23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
失败:铬28、野生动物园5.1.7,歌剧12.15
成功:FireFox23,IE8Cache-Control: private, no-cache, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:铬28、野生动物园5.1.7,歌剧12.15
成功:FireFox23,IE8Cache-Control: must-revalidate
失败:铬28、FireFox23,IE8、野生动物园5.1.7
成功:歌剧12.15Cache-Control: private, must-revalidate, proxy-revalidate, s-maxage=0
<body onunload="">
失败:铬28、FireFox23,IE8、野生动物园5.1.7
成功:歌剧12.15Cache-Control: must-revalidate, max-age=0
失败:铬28、FireFox23、野生动物园5.1.7
成功:IE8、歌剧12.15Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、野生动物园5.1.7
成功:FireFox23,IE8、歌剧12.15Cache-Control: private, no-cache, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:铬28、野生动物园5.1.7
成功:FireFox23,IE8、歌剧12.15Cache-Control: no-store
失败:歌剧12.15
成功:铬28、FireFox23,IE8、野生动物园5.1.7Cache-Control: private, no-cache, no-store, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
Pragma: no-cache
Vary: *
<body onunload="">
失败:歌剧12.15
成功:铬28、FireFox23,IE8、野生动物园5.1.7Cache-Control: private, no-cache, no-store, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
<body onunload="">
失败:歌剧12.15
成功:铬28、FireFox23,IE8、野生动物园5.1.7Cache-Control: private, no-cache
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:铬28、野生动物园5.1.7,歌剧12.15
成功:FireFox23,IE8Cache-Control: must-revalidate
Expires: 0
失败:铬28、FireFox23、野生动物园5.1.7,
成功:IE8、歌剧12.15Cache-Control: must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
失败:铬28、FireFox23、野生动物园5.1.7,
成功:IE8、歌剧12.15Cache-Control: private, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: 0
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,
成功:IE8、歌剧12.15Cache-Control: private, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0
Expires: Sat, 12 Oct 1991 05:00:00 GMT
<body onunload="">
失败:铬28、FireFox23、野生动物园5.1.7,
成功:IE8、歌剧12.15Cache-Control: private, must-revalidate
Expires: Sat, 12 Oct 1991 05:00:00 GMT
Pragma: no-cache
Vary: *
失败:铬28、野生动物园5.1.7
成功:FireFox23,IE8、歌剧12.15Cache-Control: no-store, must-revalidate
失败:没有
成功:铬28、FireFox23,IE8、野生动物园5.1.7,歌剧12.15
我找到网上。config有用的路线(试图将它添加到答案,但似乎没有被接受,所以张贴在这里)
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Cache-Control" value="no-cache, no-store, must-revalidate" />
<!-- HTTP 1.1. -->
<add name="Pragma" value="no-cache" />
<!-- HTTP 1.0. -->
<add name="Expires" value="0" />
<!-- Proxies. -->
</customHeaders>
</httpProtocol>
</system.webServer>
这里是快node.js 方式做同样的:
app.use(function(req, res, next) {
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
next();
});
我发现,所有答复在这一页面仍然有问题。特别是,我注意到,他们都不会停止IE8从使用缓存的版本的网页时访问它通过击中背部按钮。
经过大量研究和试验中,我发现的仅有的两头我真的需要是:
Cache-Control:没有商店
有所不同:*
解释的不同头,检查了 http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html#sec13.6
在IE6-8,跟ff1类似,.5-3.5、铬2-3,野生动物4,并剧9日至10日,这些标题所引起的页面被请求从服务器当你点击的链接的网页,或是把该网址,直接在的地址栏。这涵盖了约 99% 所有的浏览器中使用的Jan'10.
在IE6,并剧9日至10日,击中后背的按钮仍然引起缓存的版本将载入。在所有其他浏览器,我测试,他们没有获取一个新版本的服务器。迄今为止,我还没有找到任何设置的头,将导致这些浏览器不返回缓存版本的网页,当你打回来按钮。
更新: 写完后这个答案,我意识到,我们的网络服务器是标识本身作为一个HTTP1.0服务器。头我已经列出的是正确的,以便为响应来自HTTP1.0服务器不会缓存通过浏览器。HTTP1.1服务器,看看BalusC的 答案.
后一点研究我们提出了下列标题似乎涵盖大多数浏览器:
- 过期:Mon,26Jul1997 05:00:00GMT
- Cache-Control: no-cache, 私人, 必须重新验证, max-陈旧=0,post-check=0,pre-check=0no-store
- Pragma:no-cache
在ASP.NET 我们加入这些使用以下段:
Response.ClearHeaders();
Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1
Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1
Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0
Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.0
使用说明标题应是一个妻子的故事。RFC2616仅定义为一请求头
免责声明:我强烈建议阅读@BalusC的答复。阅读以下缓存教程: http://www.mnot.net/cache_docs/ (我建议你阅读它,太),我相信它是正确的。然而,由于历史原因(因为我已经测试过它自己的),我将包括我原来回答如下:
我尝试'接受'回答PHP,这并没有为我工作。然后我做了一点小小的研究,发现轻微的变型,测试过它,和它的工作。这里是:
header('Cache-Control: no-store, private, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0, max-stale = 0', false); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Expires: 0', false);
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header ('Pragma: no-cache');
应工作。问题是,当设置相同的一部分,头两次,如果 false
是不是发给作为第二个论点头功能,头功能简单地将复盖前面 header()
呼叫。因此,在设定 Cache-Control
, 例如,如果一个人不要把所有的争论中的一个 header()
功能呼叫,他必须这样做:
header('Cache-Control: this');
header('Cache-Control: and, this', false);
看到更完整的文档 在这里,.
有一个错误在IE6
内容与"Content-Encoding:gzip"总是缓存,甚至如果使用"Cache-Control:no-cache".
http://support.microsoft.com/kb/321722
你可以禁止gzip compression为IE6用户(检查用户的代理为"MSIE6")
对于ASP.NET 核心,创建一个简单的中间类别:
public class NoCacheMiddleware
{
private readonly RequestDelegate m_next;
public NoCacheMiddleware( RequestDelegate next )
{
m_next = next;
}
public async Task Invoke( HttpContext httpContext )
{
httpContext.Response.OnStarting( ( state ) =>
{
// ref: http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers
httpContext.Response.Headers.Append( "Cache-Control", "no-cache, no-store, must-revalidate" );
httpContext.Response.Headers.Append( "Pragma", "no-cache" );
httpContext.Response.Headers.Append( "Expires", "0" );
return Task.FromResult( 0 );
}, null );
await m_next.Invoke( httpContext );
}
}
然后注册 Startup.cs
app.UseMiddleware<NoCacheMiddleware>();
确保你加入这个地方后
app.UseStaticFiles();
RFC为 HTTP1.1 说适当的方法是加入一个HTTP标题为:
Cache-Control:no-cache
旧的浏览器也可能忽略这一点,如果他们不适当的符合HTTP1.1.对于那些你可以尝试的标题:
Pragma:no-cache
这也是应该的工作HTTP1.1浏览器。
这些指令并没有减轻任何安全风险。他们是真正意力UA的刷新挥发性的信息,不让UA的被保留的信息。看看 这种类似的问题.至少,也不能保证任何路由器、代理,等等。不会忽略的缓存的指令。
在更积极的方面来说,政策有关的物理访问计算机、软件安装,等会把你的里面前的大多数公司在安全方面。如果消费者的信息是公众成员中,只有件事你真的可以做的是帮助他们了解,一旦信息命他们的机器,机器 他们 责任不在你。
设置改性http头到一些日期在1995年通常不会把戏。
这里有一个例子:
Expires: Wed, 15 Nov 1995 04:58:08 GMT Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT Cache-Control: no-cache, must-revalidate
的 PHP文件头功能 有一个相当完整的示例(促成了由一个第三方):
header('Pragma: public');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate'); // HTTP/1.1
header('Cache-Control: pre-check=0, post-check=0, max-age=0', false); // HTTP/1.1
header ("Pragma: no-cache");
header("Expires: 0", false);
如果你在面临下载问题与IE6-IE8SSL和缓:no-cache header(和类似的价值观)与MS Office文件可以使用缓存:私人的,没有存储头和返回的文件在后的请求。它的工作。
在我的情况下,我修复的问题,在铬与这个
<form id="form1" runat="server" autocomplete="off">
我需要清除的内容previus形式的数据时的用户击按钮回由于安全原因
我有最好和最一致的结果在所有浏览器由设置 Pragma:no-cache
标题的答复中提供的BalusC不会阻止Safari5(可能还有旧版本)显示的内容浏览器高速缓存在使用浏览器的按钮。一种方式来防止这就是添加一个空onunload事件处理属性的身体标记:
<body onunload="">
这个黑客显然中断后前进的高速缓存在Safari: 是否有一个跨浏览器的事件加载单回来按钮吗?
所接受的答案不会出现工作IIS7+,由大量问题有关的高速缓存标题没有在被送i.i.7:
- 什么是迫使答复有cache-control:私人在IIS7
- IIS7:高速缓存设置工作不...为什么?
- IIS7+ASP.NET 视客户缓存头不工作
- 设置cache-control为aspx页
- Cache-control:没有储存,必须重新验证不发给客户浏览器在IIS7+ASP.NET 视
等等
所接受的答案是正确的,在这标题,必须设置,但不是在他们如何必须设置。这种方式适用IIS7:
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.AppendCacheExtension("no-store, must-revalidate");
Response.AppendHeader("Pragma", "no-cache");
Response.AppendHeader("Expires", "-1");
第一线设定 Cache-control
要 no-cache
, 和第二线中添加的其他属性 no-store, must-revalidate
还有,只是良好的措施,确保重 ExpiresDefault
在你 .htaccess
文件如果使用,以启用缓存。
ExpiresDefault "access plus 0 seconds"
之后,你可以使用 ExpiresByType
设定具体的价值观的文件你想要的超高速缓存:
ExpiresByType image/x-icon "access plus 3 month"
这也可以派上用场,如果你的动态文件,例如php,等等。正在缓存通过浏览器,你不能弄清楚为什么。检查 ExpiresDefault
.
在外头,考虑服务你的网页通过 https.许多浏览器将不会缓https默认。
//In .net MVC
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult FareListInfo(long id)
{
}
// In .net webform
<%@ OutputCache NoStore="true" Duration="0" VaryByParam="*" %>
来完成 BalusC -> 答案 如果您使用的是perl可以使用CGI添加HTTP头。
使用Perl:
Use CGI;
sub set_new_query() {
binmode STDOUT, ":utf8";
die if defined $query;
$query = CGI->new();
print $query->header(
-expires => 'Sat, 26 Jul 1997 05:00:00 GMT',
-Pragma => 'no-cache',
-Cache_Control => join(', ', qw(
private
no-cache
no-store
must-revalidate
max-age=0
pre-check=0
post-check=0
))
);
}
使用apache httpd.conf
<FilesMatch "\.(html|htm|js|css|pl)$">
FileETag None
<ifModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</ifModule>
注: 当我试图使用html元,浏览器忽略了他们,并缓存页。
我只想指出,如果有人想阻止缓存的唯一动态的内容,添加这些额外的标题应以编程方式。
我编辑的配置文件的项目追加no-cache标题,但也禁用缓存静态的内容,这不是通常可取的。修改应在码头保证图像的风格和文件将缓存。
这是很明显的,但仍然值得一提。
和另一个谨慎。小心使用ClearHeaders方法从HttpResponse类。它可以给你一些伤痕,如果你用它。喜欢它给了我。
之后将在ActionFilterAttribute事件的后果的清除所有的标题是失去了所有的会议的数据和数据TempData储存。这是更安全的重新定向,从一项行动或不清晰的头当重新定向正在发生。
在第二个想法我阻止所有使用ClearHeaders方法。这是最好删除标题分开。并且设置Cache-Control头适当我使用这个代码:
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.AppendCacheExtension("no-store, must-revalidate");
我有没有运气 <head><meta>
元素。加HTTP缓存直接相关的参数(外HTML doc)的确为我工作。
样本代码使用Python web.py web.header
呼叫下。我有意修订我个人不相关的实用程序的代码。
import web import sys import PERSONAL-UTILITIES myname = "main.py" urls = ( '/', 'main_class' ) main = web.application(urls, globals()) render = web.template.render("templates/", base="layout", cache=False) class main_class(object): def GET(self): web.header("Cache-control","no-cache, no-store, must-revalidate") web.header("Pragma", "no-cache") web.header("Expires", "0") return render.main_form() def POST(self): msg = "POSTed:" form = web.input(function = None) web.header("Cache-control","no-cache, no-store, must-revalidate") web.header("Pragma", "no-cache") web.header("Expires", "0") return render.index_laid_out(greeting = msg + form.function) if __name__ == "__main__": nargs = len(sys.argv) # Ensure that there are enough arguments after python program name if nargs != 2: LOG-AND-DIE("%s: Command line error, nargs=%s, should be 2", myname, nargs) # Make sure that the TCP port number is numeric try: tcp_port = int(sys.argv[1]) except Exception as e: LOG-AND-DIE ("%s: tcp_port = int(%s) failed (not an integer)", myname, sys.argv[1]) # All is well! JUST-LOG("%s: Running on port %d", myname, tcp_port) web.httpserver.runsimple(main.wsgifunc(), ("localhost", tcp_port)) main.run()
参见此链接到一个案例研究缓存:
http://securityevaluators.com/knowledge/case_studies/caching/
总之,根据该条,只有 Cache-Control: no-store
工作上的浏览器,火狐和IE。即接受其他的控制,但是镀铬和火狐不这样做。该链接是一个很好的阅读完整的历史的高速缓存和记录证明的概念。