我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
我们的调查表明,并非所有浏览器都以统一的方式尊重HTTP缓存指令。
出于安全原因,我们不希望应用程序中的某些页面被web浏览器缓存。这必须至少适用于以下浏览器:
Internet Explorer 6+Firefox 1.5+Safari 3+歌剧9+铬
我们的要求来自安全测试。从我们的网站注销后,您可以按后退按钮并查看缓存的页面。
当前回答
不确定我的答案听起来是否简单和愚蠢,也许你早就知道了,但由于阻止某人使用浏览器后退按钮查看历史页面是你的目标之一,你可以使用:
window.location.replace(“https://www.example.com/page-not-to-be-viewed-in-browser-history-back-button.html");
当然,这可能不可能在整个网站上实现,但至少对于一些关键页面,您可以做到这一点。希望这有帮助。
其他回答
对于ASP.NET Core,创建一个简单的中间件类:
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();
通过设置Pragma:无缓存
我发现web.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>
这里是express/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();
});
免责声明:我强烈建议阅读@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()调用。因此,在设置缓存控件时,例如,如果不想将所有参数放在一个header()函数调用中,则必须执行以下操作:
header('Cache-Control: this');
header('Cache-Control: and, this', false);
请在此处查看更完整的文档。
我在<head><meta>元素方面运气不佳。直接(在HTML文档之外)添加与HTTP缓存相关的参数确实对我有用。
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()