I would want to use Nginx X-Accel with Symfony, for the moment I've this code.
$request->headers->set('X-Sendfile-Type', 'X-Accel-Redirect');
$request->headers->set('X-Accel-Mapping', '/var/www/html/files/=/protected-files/');
$request->headers->set('X-Accel-Limit-Rate', '1k');
BinaryFileResponse::trustXSendfileTypeHeader();
$response = new BinaryFileResponse($file->getAbsolutePath());
$response->headers->set('Content-Disposition', 'attachment;filename="'.$filename.'"');
$response->headers->set('Cache-Control', 'no-cache');
return $response;
And the Nginx Conf:
location /protected-files {
internal;
alias /var/www/html/files;
}
To test the code (know if the file is really served by Nginx), I've add a X-Accel-Limit-Rate on 1ko/s, but a 2Mo file is downloaded instantly, then I'm sure, it doesn't work fine.
I've find this part of code on internet, because the Symfony doc, doesn't really explain how to use it... (http://symfony.com/doc/current/components/http_foundation.html#serving-files)
Why I need to return a BinaryResponse with the file, like without Nginx X-Sendfile, and add the X-Sendfile, X-Accel properties in the resuqest ? I just return the response, no the request, how it can work ?