浏览器会缓存具有不同 ETag 的页面吗?
我不太了解缓存的工作原理,也不了解如何控制我的托管 nginx 服务器。但是,我确实有一个需要更改的实时网站,因此非常感谢您的建议。
目前我有以下三个文件:
index.html
my.js
my.css
所有文件都通过以下标头提供(在我的浏览器上):
Date:Wed, 22 Feb 2012 23:17:49 GMT
ETag:"b2c84d1-7551-4b995b0c89c0"
Last-Modified:Wed, 22 Feb 2012 23:14:34 GMT
Server:nginx
没有任何其他与缓存相关的标头。鉴于上述情况,如果我只是用新版本替换文件,我是否可以假设用户的浏览器知道不使用缓存版本? (我假设如果替换该文件,ETag 将会更改。)
我想避免的情况是用户的浏览器下载了新版本的 index.html
但继续引用my.js
的缓存版本,因为这可能会破坏一些东西。我还想知道是否可以让用户看到该网站的较新版本,或者某些用户是否会继续看到缓存版本。
感谢您的帮助。
I don't know much about how caching works, or much control over my hosting's nginx server. However, I do have a live site that I need to make a change to, so your advice will be very much appreciated.
Currently I have the following three files:
index.html
my.js
my.css
All the files are being served (on my browser) with the following headers:
Date:Wed, 22 Feb 2012 23:17:49 GMT
ETag:"b2c84d1-7551-4b995b0c89c0"
Last-Modified:Wed, 22 Feb 2012 23:14:34 GMT
Server:nginx
There aren't any other cache-related headers. Given the above, if I simply replace the files with new versions, can I assume that users' browsers will know not to use cached versions? (I assume if I replace the file, the ETag will change.)
What I'd like to avoid is a situation where a user's browser downloads the new version of index.html
but continues to refer to a cached version of my.js
, because that's likely to break things. I'd also like to know if I can rely on the users seeing the newer version of the site, or if some users will continue to see a cached version.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览器将发送
If-Modified-Since:
和(如果您使用的是最新浏览器)If-None-Match:
标头,第一个标头引用Last-Modified:
标头,后者包含ETag
。如果修改日期比 Last-Modified 标头更新,服务器将发送新文件。 ETag 如何更改取决于您的服务器,但在大多数情况下,这将是文件内容的哈希值。因此,只需确保文件内容发生更改且修改日期是最新的(例如通过
touch /var/www/your-file.js
)。The browser will send the
If-Modified-Since:
and (if you have a recent browser) theIf-None-Match:
headers, the first refering to theLast-Modified:
header, the latter containing theETag
. The server will send the new file, if the modification date is newer than the Last-Modified header. How the ETag changes will depend on your server, but in most cases this will be a hash over the file content.So just make sure that both the file content changes and the modification date is up-to-date (e.g. by
touch /var/www/your-file.js
).