浏览器缓存内容时遇到问题
抱歉,这似乎与许多其他问题非常相似,但我已经查看了我能看到的所有相关帖子,但它们似乎没有与我完全相同的问题;-)
问题是我无法从我的 Apache2 服务器获取 .js、.css 和图像的客户端缓存(我在 Windows 下有 Ubuntu Apache/2.2.8 和 WAMP Apache)。
使用默认设置,我可以获得良好的 ETag 样式服务器主导的缓存(304 响应),但如果我通过将 Header Unset 命令放入虚拟主机配置文件中来关闭此功能,然后补充缓存指令(见下文),则它不会似乎受到浏览器的尊重。
我可以使用 Firebug 看到浏览器知道缓存的文件在几个小时内有效,但它仍然要求它。
任何有关如何调试此问题的提示都将受到欢迎。
主机配置的相关部分如下所示:
<VirtualHost *:80>
DocumentRoot "C:/Projects/A2C/branches/CR7"
ServerName *
ServerAlias localhost ecomsvr0.dmclub.net
ErrorLog "c:\log\a2c.log"
CustomLog "c:\log\a2c-access.log" common
<Directory "c:\projects\A2C\branches\CR7">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
ExpiresDefault A36000
Header Set Cache-Control "max-age=7200"
Header Unset Etag
Header Unset Last-Modified
</Directory>
</VirtualHost>
Sorry that this appears to be very similar to a number of other questions, but I have looked at all related posts I can see, but they don't seem to have quite the same issue as I do ;-)
The problem is that I can't get clientside caching of .js, .css and images to work from my Apache2 servers (I've got both Ubuntu Apache/2.2.8 and a WAMP Apache under Windows).
With default settings, I get good ETag style server-led caching (304 responses) but if I turn this off by putting Header Unset commands into my virtual host config file, and then supplement with Caching directives (see below), it doesn't seem to be respected by the browser.
I can see using Firebug that the browser knows that a cached file is valid for another couple of hours, but it still asks for it anyway.
Any tips on how to debug this would be most welcome.
The relevant part of host config is shown below:
<VirtualHost *:80>
DocumentRoot "C:/Projects/A2C/branches/CR7"
ServerName *
ServerAlias localhost ecomsvr0.dmclub.net
ErrorLog "c:\log\a2c.log"
CustomLog "c:\log\a2c-access.log" common
<Directory "c:\projects\A2C\branches\CR7">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
ExpiresDefault A36000
Header Set Cache-Control "max-age=7200"
Header Unset Etag
Header Unset Last-Modified
</Directory>
</VirtualHost>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
令我印象深刻的是,您没有发送任何验证器(Last-Modified 或 ETag)。虽然它们不是强制性的,但我强烈建议您发送一个,以使浏览器能够执行条件请求。 RFC 2616 说“HTTP/1.1 服务器应该在可行的情况下发送 Last-Modified ”。这不一定是问题的根本原因,但很可能是。
因此,我建议删除“Header Unset Last-Modified”行并再次检查。
请注意,您的 Expires 是访问时间加上 36000 秒,而您的 max-age 时间是 7200 秒,这是没有意义的。了解新的 Cache-Control 指令(所有新浏览器都这样做)的浏览器将采用 max-age。
如果添加 Last-Modified 不起作用,您能否发布服务器发送的完整响应标头?例如,转到 http://redbot.org/ 并针对您的服务器运行此检查器并发布结果。这将进一步帮助确定您的问题。
What strikes me is that you don't send any validator (Last-Modified or ETag). While they are not mandatory, I strongly encourage you to send one, to enable browsers to do conditional requests. RFC 2616 says "HTTP/1.1 servers SHOULD send Last-Modified whenever feasible." This must not be the root cause of your problem, but it could well be.
So I suggest to remove the line "Header Unset Last-Modified" and check again.
Note that your Expires is access time plus 36000s while your max-age time is 7200s, which doesn't make sense. Browsers understanding the new Cache-Control directive (all new browsers do) will go for max-age.
If adding Last-Modified does not work, can you please post the full response headers your server sends? For example, go to http://redbot.org/ and run this checker against your server and post the results. This will further help to identify your problem.