测试 X-Sendfile 头是否正常工作

8

我正在寻找一种确认X-Sendfile是否正确处理由脚本(PHP)返回给Web服务器的请求的方法。 图像被正确地提供,但我认为在curl请求中应该看到头文件。

$ curl -I http://blog2.stageserver.net/wp-includes/ms-files.php?file=/2011/05/amos-lee-feature.jpg
HTTP/1.1 200 OK
Date: Wed, 04 Jan 2012 17:19:45 GMT
Server: Cherokee/1.2.100 (Arch Linux)
ETag: "4dd2e306=9da0"
Last-Modified: Tue, 17 May 2011 21:05:10 GMT
Content-Type: image/jpeg
Content-Length: 40352
X-Powered-By: PHP/5.3.8
Content-Disposition: inline; filename="amos-lee-feature.jpg"

配置

Cherokee 1.2.100与PHP-FPM 5.3.8在FastCGI中的配置:
cherokee.conf: vserver!20!rule!500!handler!xsendfile = 1
(由vServer > Behavior > Extensions php > Handler设置:允许X-Sendfile [选中启用])

WordPress Network / WPMU 3.3.1:
wp-config.php中的define('WPMU_SENDFILE',true);是在包含wp-settings.php之前设置的。这将触发以下代码在WP的wp-includes/ms-files.php:50中执行,为特定博客提供文件:

header( 'X-Sendfile: ' . $file );
exit;

我已确认以上片段正在执行,通过在调用exit();之前添加一个附加的内容协商标头进行。在上面的 curl 结果中存在 Content-Disposition,而在 ms-files.php 代码中原本不存在。添加的代码是:
header('Content-Disposition: inline; filename="'.basename($file).'"');

研究

我已经:

  • 在更改配置后重新启动了php-fpm / cherokee守护程序。
  • 尝试了一些在php.net/readfile评论中的技巧,并用示例中更完整的代码替换了ms-files.php中的简单标题。
    • php.net/manual/en/function.readfile.php
    • www.jasny.net/articles/how-i-php-x-sendfile/
    • *codeutopia.net/blog/2009/03/06/sending-files-better-apache-mod_xsendfile-and-php/*
  • 即使我认为我的图片正在正确提供,也经过[Cherokee支持][5]和测试[有/无][6]压缩,我也发现了来自lighttpd帖子的可疑类似问题。
    • cherokee-project.com/doc/other_goodies.html*
    • code.google.com/p/cherokee/issues/detail?id=1228
    • webdevrefinery.com/forums/topic/4761-x-sendfile/
  • 在SO上找到了一段简短的话,可能表明标题已被剥离
    • stackoverflow.com/questions/7296642/django-understanding-x-sendfile
  • 测试了来自curl、wget、Firefox、Chrome和web-sniffer.net的标头是一致的。
  • 发现由于声望不足,我无法发布超过2个链接。

问题

  • 当X-Sendfile正常工作时,标题中是否存在X-Sendfile,还是被剥离了?
  • 可以使用访问日志确定X-Sendfile是否正在工作吗?

我在这里寻找一般的故障排除提示或信息,不一定特定于PHP/Cherokee。

更新

我已经找到了一种适合在测试或沙盒环境中确认 X-Sendfile 或 X-Accel-Redirect 的方法:禁用 X-Sendfile 并检查标题。

禁用 Cherokee 中的 Allow X-Sendfile:

$ curl -I http://blog2.stageserver.net/wp-includes/ms-files.php?file=/2011/05/amos-lee-feature.jpg
HTTP/1.1 200 OK
Date: Fri, 06 Jan 2012 15:34:49 GMT
Server: Cherokee/1.2.101 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.3
Content-Type: image/jpeg
X-Sendfile: /srv/http/wordpress/wp-content/blogs.dir/2/files/2011/05/amos-lee-feature.jpg
Content-Length: 40352

在浏览器中无法加载图像,但您可以看到标题存在。重新启用“允许X-Sendfile”后,图像加载并且您可以确信X-Sendfile正在工作。

3个回答

0

它们被删除,仅因为它们的存在会防止使用它的一个原因,即在不让接收者知道所提供文件的位置的情况下提供文件。


0
根据github上的源代码,X-Sendfile头信息将被剥离。
如果我正确地浏览了该文件,只有在调试模式下编译成功时才会记录成功信息。

有趣的是,此外我已经检查了Cherokeenginx的源代码。看起来它们都会剥离头部或从空白集合重新构建。 - dalethedeveloper

0

你可以使用xsendfile来检查发送大文件时的内存使用情况。


好的建议,但不适合在生产环境的服务器上玩耍。也许需要进行一些沙盒测试。 - dalethedeveloper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接