Permalink
Browse files

feature(http): allow use of X-Sendfile/X-Accel web server feature

Fixes #4898
  • Loading branch information...
mrclay committed Apr 12, 2016
1 parent 2512058 commit a88db207aa39351eb26855fe9125cdaf287a1e03
@@ -169,7 +169,7 @@ Installation requirements:
Configuration:
-Uncomment and populate the following sections in `settings.php`
+Uncomment and populate the following sections in ``settings.php``
.. code:: php
@@ -203,6 +203,13 @@ These speed up your site by caching the compiled byte code from your
script meaning that your server doesn't have to compile the PHP code
each time it is executed.
+Direct file serving
+-------------------
+
+If your server can be configured to support the X-Sendfile or X-Accel headers,
+you can configure it to be used in ``settings.php``. This allows your web server to
+directly stream files to the client instead of using PHP's ``readfile()``.
+
Hosting
=======
@@ -152,6 +152,25 @@
*/
//$CONFIG->cacheroot = "";
+/**
+ * Enable SendFile file serving
+ *
+ * After enabling X-Sendfile/X-Accel on your server, you can enable its support in Elgg. Set the
+ * X-Sendfile-Type value to "X-Sendfile" (Apache) or "X-Accel-Redirect" (Nginx).
+ *
+ * @global string $CONFIG->{'X-Sendfile-Type'}
+ */
+//$CONFIG->{'X-Sendfile-Type'} = '';
+
+/**
+ * Configure X-Accel on nginx (see SendFile above)
+ *
+ * For Nginx, you'll likely also need to set this to a mapping like: "/path/to/dataroot/=/download/".
+ *
+ * @global string $CONFIG->{'X-Accel-Mapping'}
+ */
+//$CONFIG->{'X-Accel-Mapping'} = '';
+
/**
* Cookie configuration
*
@@ -110,6 +110,17 @@ public function getResponse(Request $request) {
'Content-Type' => (new MimeTypeDetector())->getType($filenameonfilestore),
];
$response = new BinaryFileResponse($filenameonfilestore, 200, $headers, $public, $content_disposition);
+
+ $sendfile_type = $this->config->getVolatile('X-Sendfile-Type');
+ if ($sendfile_type) {
+ $request->headers->set('X-Sendfile-Type', $sendfile_type);
+
+ $mapping = (string)$this->config->getVolatile('X-Accel-Mapping');
+ $request->headers->set('X-Accel-Mapping', $mapping);
+
+ $response->trustXSendfileTypeHeader();
+ }
+
$response->prepare($request);
if (empty($expires)) {

0 comments on commit a88db20

Please sign in to comment.