title: Test if X-Sendfile header is working
body: <p>I am looking for a way to confirm if X-Sendfile is properly handling requests handed back to the webserver by a script (PHP).  Images are being served correctly but I thought I would see the header in curl requests.</p>
  
  <pre><code>$ 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"
  </code></pre>
  
  <h2>Configuration</h2>
  
  <p><strong>Cherokee 1.2.100</strong> with PHP-FPM 5.3.8 in FastCGI:<br>
  <code>cherokee.conf: vserver!20!rule!500!handler!xsendfile = 1</code><br>
  (Set by vServer > Behavior > Extensions php > Handler: Allow X-Sendfile [check Enabled])</p>
  
  <p><strong>Wordpress Network / WPMU 3.3.1</strong>:<br>
  <code>define('WPMU_SENDFILE',true);</code> is set in the <code>wp-config.php</code> the following just before <code>wp-settings.php</code> is included.  This will trigger the following code to be executed in <a href="http://core.trac.wordpress.org/browser/trunk/wp-includes/ms-files.php" rel="noreferrer">WP's wp-includes/ms-files.php:50</a> serves up files for a particular blog:</p>
  
  <pre><code>header( 'X-Sendfile: ' . $file );
  exit;
  </code></pre>
  
  <p>I have confirmed that the above snippet is executing by adding an additional header for disposition right before the <code>exit();</code> call.  That Content-Disposition is present with curl results above and not originally in the ms-files.php code.  The code that was added is:<br>
  <code>header('Content-Disposition: inline; filename="'.basename($file).'"');</code></p>
  
  <hr>
  
  <h2>Research</h2>
  
  <p>I have:</p>
  
  <ul>
  <li>Rebooted php-fpm / cherokee daemons after making configuration changes.</li>
  <li>Tried several tricks in the comments over at <em>php.net/readfile</em> and replaced the simple header in <code>ms-files.php</code> with more complete code from examples.
  <ul>
  <li><em>php.net/manual/en/function.readfile.php</em></li>
  <li><em>www.jasny.net/articles/how-i-php-x-sendfile/</em></li>
  <li>*codeutopia.net/blog/2009/03/06/sending-files-better-apache-mod_xsendfile-and-php/*</li>
  </ul></li>
  <li>Confirmed [cherokee support][5] and tested [with and without][6] compression even though I don't think it would apply since my images are serving correctly.  I also found a suspiciously similar problem from a lighttpd post.
  <ul>
  <li>*cherokee-project.com/doc/other_goodies.html*</li>
  <li><em>code.google.com/p/cherokee/issues/detail?id=1228</em></li>
  <li><em>webdevrefinery.com/forums/topic/4761-x-sendfile/</em></li>
  </ul></li>
  <li>Found a blurb here on SO that may indicate the header gets stripped
  <ul>
  <li><em>stackoverflow.com/questions/7296642/django-understanding-x-sendfile</em></li>
  </ul></li>
  <li>Tested that the headers above are consistent from curl, wget, Firefox, Chrome, and web-sniffer.net.</li>
  <li>Found out that I can't post more than 2 links yet due to lack of reputation.</li>
  </ul>
  
  <h2>Questions</h2>
  
  <ul>
  <li>Will <code>X-Sendfile</code> be present in the headers when it is working correctly or is it stripped out?</li>
  <li>Can the access logs be used to determine if <code>X-Sendfile</code> is working?</li>
  </ul>
  
  <p>I am looking for general troubleshooting tips or information here, not necessarily specific to PHP / Cherokee.</p>
  
  <h2>Update</h2>
  
  <p>I have found a suitable way to confirm X-Sendfile or X-Accel-Redirect in a test or sandbox environment:  Disable X-Sendfile and check the headers.</p>
  
  <p>With Allow X-Sendfile disabled in Cherokee:</p>
  
  <pre><code>$ 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
  </code></pre>
  
  <p>The image will not load in browsers but you can see that the header is present.  After re-enabling Allow X-Sendfile the image loads and you can be confident that X-Sendfile is working.</p>