apache 你如何制作与Apache匹配的etag?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you make an etag that matches Apache?
提问by Chris Bartow
I want to make an etag that matches what Apache produces. How does apache create it's etags?
我想制作一个与 Apache 生成的内容相匹配的 etag。apache 如何创建它的 etags?
采纳答案by Chris Bartow
Apache uses the standard format of inode-filesize-mtime. The only caveat to this is that the mtime must be epoch time and padded with zeros so it is 16 digits. Here is how to do it in PHP:
Apache 使用 inode-filesize-mtime 的标准格式。唯一需要注意的是,mtime 必须是纪元时间并用零填充,因此它是 16 位数字。以下是如何在 PHP 中执行此操作:
$fs = stat($file);
header("Etag: ".sprintf('"%x-%x-%s"', $fs['ino'], $fs['size'],base_convert(str_pad($fs['mtime'],16,"0"),10,16)));
回答by Hank Gay
One thing to remember about Apache's Etags is that they don't play well in clusters because they include inode information that can—and probably will—vary between machines in the same cluster.
关于 Apache 的 Etags 需要记住的一件事是,它们在集群中不能很好地运行,因为它们包含的 inode 信息可以——而且可能会——在同一集群中的机器之间变化。
回答by PWolanin
the answer above (from Chris) works well, but can be simplified using an implicit cast in the sprintf:
上面的答案(来自 Chris)效果很好,但可以使用 sprintf 中的隐式强制转换来简化:
sprintf('"%x-%x-%x"', $s['ino'], $s['size'], str_pad($s['mtime'], 16, "0"));
The suggested %016xdoesn't work because the padding is applied after the conversion to hex, rather than before.
建议%016x不起作用,因为填充是在转换为十六进制之后应用的,而不是之前。
回答by Neall
If you're dynamically generating your page though, this probably won't make sense. If you're in PHP, you can pick the inode and file size of the main script, but the modify time won't tell you if your data has changed. Unless you have a good caching process or just generate static pages, etags aren't helpful. If you do have a good caching process, the inode and file size are probably irrelevant.
但是,如果您动态生成页面,这可能没有意义。如果您使用 PHP,您可以选择主脚本的 inode 和文件大小,但修改时间不会告诉您数据是否已更改。除非你有一个很好的缓存过程或者只是生成静态页面,否则 etags 没有帮助。如果您确实有一个良好的缓存过程,那么 inode 和文件大小可能无关紧要。
Edit: For people who don't know what etags are - they're just supposed to be a value that changes when the content has changed, for caching purposes. The browser gets the etag from the web server, compares it to the etag for its cached copy and then fetches the whole page if the etag has changed.
编辑:对于不知道etag是什么的人 - 它们应该是一个在内容更改时更改的值,用于缓存目的。浏览器从 Web 服务器获取 etag,将其与缓存副本的 etag 进行比较,如果 etag 发生更改,则获取整个页面。

