自定义etag生成

我怎样才能configuration一个Apache或nginx服务器使用我select的algorithm(即不涉及inode,mtime或大小)发送Etag头? 有没有其他的编译新的C模块?

在Apache中, ETags作为核心功能来处理 。 ETag被计算为几个值的哈希值。 您可以在httpd.conf.htaccess文件中使用FileETag指令来定义包含在哈希中的值的条件行为,但正如您所指出的,您的选项仅限于:

  • INode – 您的文件在其服务的特定服务器上的i-node编号
  • MTime – 您的文件服务器的时间戳(以毫秒为单位)
  • Size – 以字节为单位的文件大小
  • All – 以上所有
  • None – 以上都不是

如果你想真正定制ETag的生成,你肯定会写最好的一个Apache模块。 但是,如果您需要快速修复,则可以通过将请求路由到PHP脚本并在脚本中附加Etag头来生成自己的标记。 在httpd.conf.htaccess文件中,路由可能如下所示:

 RewriteCond %{REQUEST_FILENAME} \.png$ # This example looks for .png requests RewriteRule ^(.*)$ /gentag.php?path=$1 [B] # ...and routes them to a PHP script 

PHP脚本可能如下所示:

 <? $path = $_GET['path']; // Grab the filepath from GET params $cont = file_get_contents($path); // Get file contents to hash $hash = crc32($cont); // Create your own ETag hash however you like header("Etag: $hash"); // Send the custom Etag header echo $cont; // Dump the file contents to output ?>