【问题标题】:Test if X-Sendfile header is working测试 X-Sendfile 标头是否正常工作
【发布时间】:2012-02-02 18:01:06
【问题描述】:

我正在寻找一种方法来确认 X-Sendfile 是否正确处理由脚本 (PHP) 传回给网络服务器的请求。图片提供正确,但我想我会在 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 在 FastCGI 中使用 PHP-FPM 5.3.8:
cherokee.conf: vserver!20!rule!500!handler!xsendfile = 1
(由 vServer > Behavior > Extensions php > Handler 设置:允许 X-Sendfile [检查已启用])

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

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

我已经通过在exit(); 调用之前添加一个额外的头文件来确认上面的sn-p 正在执行。 Content-Disposition 存在于上面的 curl 结果中,而不是最初在 ms-files.php 代码中。添加的代码是:
header('Content-Disposition: inline; filename="'.basename($file).'"');


研究

我有:

  • 进行配置更改后重新启动 php-fpm / cherokee 守护进程。
  • php.net/readfile 的 cmets 中尝试了几个技巧,并用示例中更完整的代码替换了 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 support][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 测试,上面的 headers 是一致的。
  • 发现由于缺乏声誉,我不能发布超过 2 个链接。

问题

  • 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

图像不会在浏览器中加载,但您可以看到标题存在。重新启用 Allow X-Sendfile 后,图像会加载,您可以确信 X-Sendfile 正在工作。

【问题讨论】:

    标签: php wordpress fastcgi cherokee x-sendfile


    【解决方案1】:

    根据source on github X-Sendfile 标头将被剥离。

    如果我正确浏览了文件,那么只有在调试模式下编译时才会记录成功。

    【讨论】:

    • 有趣,另外我检查了Cherokeenginx 的来源。看来他们都确实剥离了标题或从空白集中重建。
    【解决方案2】:

    您可以检查使用和不使用 xsendfile 发送大文件的内存使用情况。

    【讨论】:

    • 很好的建议,但不能在实时环境中的服务器上使用。也许需要进行一些沙盒测试。
    【解决方案3】:

    它们被剥离,仅仅是因为它们存在会阻止使用它的原因之一,即在接收者不知道所提供文件的位置的情况下提供文件。

    【讨论】:

      猜你喜欢
      • 2014-06-18
      • 2014-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多