自定义etag生成

Sto*_*ica 8 apache2 nginx

如何配置apache或nginx服务器以使用我选择的算法发送Etag标头(即不涉及inode,mtime或size)?有没有其他方法来编译新的C模块?

pie*_*n72 5

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

  • INode - 您的文件在其服务的特定服务器上的索引节点号
  • 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
Run Code Online (Sandbox Code Playgroud)

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
?>
Run Code Online (Sandbox Code Playgroud)