ETag 与标头过期
我环顾四周,但无法弄清楚是否应该同时使用 ETag 和过期标头或其中之一。
我想做的是确保我的 Flash 文件(以及其他图像以及这些文件发生更改时不仅会更新。
我不想做任何特殊的事情,例如更改文件名或添加一些奇怪的内容) url 末尾的字符以使其不被缓存
另外,我需要在我的 PHP 脚本中以编程方式执行任何操作来支持此功能还是全部是 Apache?
I've looked around but haven't been able to figure out if I should use both an ETag and an Expires Header or one or the other.
What I'm trying to do is make sure that my flash files (and other images and what not only get updated when there is a change to those files.
I don't want to do anything special like changing the filename or putting some weird chars on the end of the url to make it not get cached.
Also, is there anything I need to do programatically on my end in my PHP scripts to support this or is it all Apache?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它们略有不同 - ETag 没有任何信息可供客户端用来确定将来是否再次请求该文件。 如果 ETag 就是它的全部,那么它总是需要发出请求。 但是,当服务器从客户端请求中读取 ETag 时,服务器可以确定是发送文件 (HTTP 200) 还是告诉客户端仅使用其本地副本 (HTTP 304)。 ETag 基本上只是文件的校验和,当文件内容发生变化时,它的语义也会发生变化。
客户端(和代理/缓存)使用 Expires 标头来确定是否需要向服务器发出请求。 距离过期日期越近,客户端(或代理)就越有可能从服务器发出对该文件的 HTTP 请求。
因此,您真正想要做的是使用两个标头 - 根据内容更改的频率将 Expires 标头设置为合理的值。 然后配置要发送的 ETag,以便当客户端向服务器发送请求时,可以更轻松地确定是否将文件发回。
关于 ETag 的最后一点 - 如果您使用的是负载平衡服务器设置,其中多台机器运行 Apache,您可能需要关闭 ETag 生成。 这是因为 inode 用作 ETag 哈希算法的一部分,该算法在服务器之间会有所不同。 您可以将 Apache 配置为不使用索引节点作为计算的一部分,但随后您需要确保文件上的时间戳完全相同,以确保为所有服务器生成相同的 ETag。
They are slightly different - the ETag does not have any information that the client can use to determine whether or not to make a request for that file again in the future. If ETag is all it has, it will always have to make a request. However, when the server reads the ETag from the client request, the server can then determine whether to send the file (HTTP 200) or tell the client to just use their local copy (HTTP 304). An ETag is basically just a checksum for a file that semantically changes when the content of the file changes.
The Expires header is used by the client (and proxies/caches) to determine whether or not it even needs to make a request to the server at all. The closer you are to the Expires date, the more likely it is the client (or proxy) will make an HTTP request for that file from the server.
So really what you want to do is use BOTH headers - set the Expires header to a reasonable value based on how often the content changes. Then configure ETags to be sent so that when clients DO send a request to the server, it can more easily determine whether or not to send the file back.
One last note about ETag - if you are using a load-balanced server setup with multiple machines running Apache you will probably want to turn off ETag generation. This is because inodes are used as part of the ETag hash algorithm which will be different between the servers. You can configure Apache to not use inodes as part of the calculation but then you'd want to make sure the timestamps on the files are exactly the same, to ensure the same ETag gets generated for all servers.
Etag 和 Last-modified 标头是验证器。
它们帮助浏览器和/或缓存(反向代理)了解文件/页面是否已更改,即使它保留相同的名称。
过期和缓存控制提供刷新信息。
这意味着它们通知浏览器和反向中间代理,它们可以将页面/文件保留在缓存中的时间或时长。
因此,问题通常是使用哪一个验证器,etag 或最后修改的验证器,以及使用哪个刷新信息标头,过期或缓存控制。
Etag and Last-modified headers are validators.
They help the browser and/or the cache (reverse proxy) to understand if a file/page, has changed, even if it preserves the same name.
Expires and Cache-control are giving refresh information.
This means that they inform, the browser and the reverse in-between proxies, up to what time or for how long, they may keep the page/file at their cache.
So the question usually is which one validator to use, etag or last-modified, and which refresh infomation header to use, expires or cache-control.
Expires
和Cache-Control
是“强缓存标头”Last-Modified
和ETag
是“弱缓存标头”首先浏览器检查
Expires/Cache-Control
来决定是否向服务器发出请求,如果必须发出请求,则会发送
Last-Modified/ETag
在 HTTP 请求中。 如果文档的Etag
值与此匹配,服务器将发送 304 代码而不是 200,并且不发送任何内容。 浏览器将从其缓存中加载内容。Expires
andCache-Control
are "strong caching headers"Last-Modified
andETag
are "weak caching headers"First the browser check
Expires/Cache-Control
to determine whether or not to make a request to the serverIf have to make a request, it will send
Last-Modified/ETag
in the HTTP request. If theEtag
value of the document matches that, the server will send a 304 code instead of 200, and no content. The browser will load the contents from its cache.另一个总结:
您需要同时使用两者。 ETag 是“服务器端”信息。 过期是“客户端”缓存。
使用ETag,除非您有负载平衡服务器。 它们是安全的,并且会让客户知道每次您更改自己的某些内容时,他们应该获取服务器文件的新版本。
Expires 必须谨慎使用,就好像你设置了一个很远的过期日期,但想立即更改其中一个文件(例如 JS 文件),有些用户可能无法得到修改后的版本直到很长一段时间!
Another summary:
You need to use both. ETags are a "server side" information. Expires are a "Client side" caching.
Use ETags except if you have a load-balanced server. They are safe and will let clients know they should get new versions of your server files every time you change something on your side.
Expires must be used with caution, as if you set a expiration date far in the future but want to change one of the files immediatelly (a JS file for instance), some users may not get the modified version until a long time!
我想提一下的另一件事是,一些答案可能被遗漏了,那就是在标头中同时包含 ETags 和 Expires/Cache-control 的缺点。
根据您的需要,它可能只是在标头中添加额外的字节,这可能会增加数据包,这意味着更多的 TCP 开销。 同样,您应该看看在标头中同时包含这两个内容的开销是否是必要的,或者它是否会在您的请求中增加额外的重量,从而降低性能。
您可以在 Kyle Simpson 撰写的这篇优秀博客文章中阅读更多相关信息:http:// /calendar.perfplanet.com/2010/bloated-request-response-headers/
One additional thing I would like to mention that some of the answers may have missed is the downside to having both
ETags
andExpires/Cache-control
in your headers.Depending on your needs it may just add extra bytes in your headers which may increase packets which means more TCP overhead. Again, you should see if the overhead of having both things in your headers is necessary or will it just add extra weight in your requests which reduces performance.
You can read more about it on this excellent blog post by Kyle Simpson: http://calendar.perfplanet.com/2010/bloated-request-response-headers/
在我看来,使用 Expire Header,服务器可以告诉客户端我的数据何时过时,而使用 Etag,服务器将检查客户端每个请求的 etag 值。
In my view, With Expire Header, server can tell the client when my data would be stale, while with Etag, server would check the etag value for client' each request.
Etag
是用于指示资源版本的哈希值。 当服务器返回数据时,它会对数据进行哈希处理,并将此哈希值设置在ETAG
下。 当您向服务器发送“PUT”请求以更新记录时,可能同时另一个用户发出相同的“PUT”请求并且其请求已被处理。 服务器将检查您的“PUT”数据,并发现它是相同的更新,因此不会进行另一次更新,它将向您发送更新的数据(由另一个用户),并且您将更新您的缓存。当缓存时间到期时,浏览器会自动发出新的请求以获取最新数据。 这就是使用“Expires”标头的原因。 从这里开始
Etag
is a hash for indicating the version of the resource. When the server returns data, it hashes the data and set this hash value underETAG
. When you send a "PUT" request to the server to update a record, maybe simultaneously another user made the same "PUT" request and its request has been processed. The server will check your "PUT" data and will see that it is the same update so it wont make another update, it will send you the updated data (by another user) and you will update your cache.when the time for caching expires, the browser automatically makes a new request to get the fresh data. That is why "Expires" header is used. From here
ETag 用于确定资源是否应该使用副本资源。 和Cache-Control一样的Expires Header告诉客户端,在缓存几十年之前,客户端应该获取本地资源。
在现代网站中,经常会提供一个名为 hash 的文件,例如
app.98a3cf23.js
,因此使用 Expires Header 是一个很好的做法。 除此之外,它还降低了网络成本。希望能帮助到你 ;)
ETag is used to determine whether a resource should use the copy one. and Expires Header like Cache-Control is told the client that before the cache decades, client should fetch the local resource.
In modern sites, There are often offer a file named hash, like
app.98a3cf23.js
, so that it's a good practice to use Expires Header. Besides this, it also reduce the cost of network.Hope it helps ;)