ETag vs Header Expires [英] ETag vs Header Expires

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

问题描述

我环顾四周但未能弄清楚我是否应该同时使用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.

我要做的是确保我的flash文件(以及其他图像以及当这些文件发生更改时不仅会更新。

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.

另外,在我的PHP脚本中,我需要以编程方式执行任何操作以支持此操作,还是所有Apache?

Also, is there anything I need to do programatically on my end in my PHP scripts to support this or is it all Apache?

推荐答案

它们略有不同 - ETag没有任何客户可以用来确定是否在将来再次请求该文件的信息。如果ETag是它的全部,它将始终必须发出请求。但是,当服务器从客户端请求中读取ETag时,服务器就可以确定是发送文件(HTTP 200)还是告诉客户端只使用其本地副本(HTTP 304)。 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.

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

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.

那真的是什么你想要做的是使用BOTH标头 - 根据内容更改的频率将Expires标头设置为合理的值。然后配置要发送的ETag,以便当客户端向服务器发送请求时,它可以更容易地确定是否将文件发回。

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.

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

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

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