title: Serving protected files with Symfony2 and Nginx X-accel-redirect
body: <p>My config is php-fpm 5.4 with nginx. I need to serve some protected files. So I check with Symfony if the user has the correct rights and then I would like nginx to serve the file (a swf) using the X-accel-redirect header.</p>
  
  <p>Controller in symfony:</p>
  
  <pre><code>/**
   * @Route("/protected/swf")
   */
  public function sendFileAction()
  {
      // [...]    
  
      // user rights are ok, serve the file
  
      $fileName = 'myfile.swf';
      $filePath = $this-&gt;get('kernel')-&gt;getRootDir() . '/../files/' . $fileName;
  
      $response = new BinaryFileResponse($filePath);
      $response-&gt;trustXSendfileTypeHeader();
      return $response;
  }
  </code></pre>
  
  <p>And my nginx config: (<a href="http://wiki.nginx.org/XSendfile" rel="nofollow">http://wiki.nginx.org/XSendfile</a>). The files are in a directory outside of the web directory (files).</p>
  
  <pre><code>location /files/ {
       internal;
       root   /path/to/folder;
  }
  </code></pre>
  
  <p>It works (I mean the file is served) but I don't think it makes use of XSendfile. If I understand correctly, nginx will serve a file if the header</p>
  
  <pre><code>X-Accel-Redirect: /files/myfile.swf;
  </code></pre>
  
  <p>is present. But the BinaryFileResponse use a path on the disk to find my file and does not know that this header should be set as above.</p>
  
  <p>Can someone show me an example of how to that?
  Thanks!</p>