ETag 与标题过期 [英] ETag vs Header Expires

查看:25
本文介绍了ETag 与标题过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,但一直无法确定是否应该同时使用 ETag Expires Header 其中之一.>

我要做的是确保我的 Flash 文件(和其他图像以及不仅在这些文件发生更改时更新).

我不想做任何特殊的事情,例如更改文件名或在 url 末尾放置一些奇怪的字符以使其不被缓存.

另外,是否有什么我需要在我的 PHP 脚本中以编程方式做的事情来支持这一点,还是全部是 Apache?

解决方案

它们略有不同 - ETag 没有任何信息可供客户端用来确定将来是否再次请求该文件.如果 ETag 是它所拥有的全部,它将始终必须发出请求.但是,当服务器从客户端请求中读取 ETag 时,服务器可以确定是发送文件 (HTTP 200) 还是告诉客户端只使用其本地副本 (HTTP 304).ETag 基本上只是一个文件的校验和,当文件的内容发生变化时,它会在语义上发生变化.

客户端(和代理/缓存)使用 Expires 标头来确定它是否甚至需要向服务器发出请求.越接近到期日期,客户端(或代理)就越有可能从服务器发出对该文件的 HTTP 请求.

因此,您真正想要做的是使用两个标头 - 根据内容更改的频率将 Expires 标头设置为合理的值.然后配置要发送的 ETags,这样当客户端 DO 向服务器发送请求时,它可以更容易地确定是否将文件发回.

关于 ETag 的最后一个注意事项 - 如果您在多台运行 Apache 的机器上使用负载平衡服务器设置,您可能需要关闭 ETag 生成.这是因为 inode 用作 ETag 哈希算法的一部分,这在服务器之间会有所不同.您可以将 Apache 配置为不使用 inode 作为计算的一部分,但随后您需要确保文件上的时间戳完全相同,以确保为所有服务器生成相同的 ETag.

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?

解决方案

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 与标题过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆