Skip to content

Commit 5e1ad5d

Browse files
authored
docs: efficiently serving large static files (X-Sendfile/X-Accel-Redirect) (#896)
* docs: X-Sendfile/X-Accel-Redirect * lint * fix
1 parent 96dd739 commit 5e1ad5d

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ frankenphp php-server
8888
* [Worker mode](https://frankenphp.dev/docs/worker/)
8989
* [Early Hints support (103 HTTP status code)](https://frankenphp.dev/docs/early-hints/)
9090
* [Real-time](https://frankenphp.dev/docs/mercure/)
91+
* [Efficiently Serving Large Static Files](https://frankenphp.dev/docs/x-sendfile/)
9192
* [Configuration](https://frankenphp.dev/docs/config/)
9293
* [Docker images](https://frankenphp.dev/docs/docker/)
9394
* [Deploy in production](https://frankenphp.dev/docs/production/)

caddy/frankenphp/Caddyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# }
2020
#}
2121

22-
root * public/
22+
root public/
2323
encode zstd br gzip
2424

2525
# Uncomment the following lines to enable Mercure and Vulcain modules

docs/fr/x-sendfile.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Servir efficacement les gros fichiers statiques (`X-Sendfile`/`X-Accel-Redirect`)
2+
3+
Habituellement, les fichiers statiques peuvent être servis directement par le serveur web,
4+
mais parfois, il est nécessaire d'exécuter du code PHP avant de les envoyer :
5+
contrôle d'accès, statistiques, en-têtes HTTP personnalisés...
6+
7+
Malheureusement, utiliser PHP pour servir de gros fichiers statiques est inefficace comparé à
8+
à l'utilisation directe du serveur web (surcharge mémoire, diminution des performances...).
9+
10+
FrankenPHP permet de déléguer l'envoi des fichiers statiques au serveur web
11+
**après** avoir exécuté du code PHP personnalisé.
12+
13+
Pour ce faire, votre application PHP n'a qu'à définir un en-tête HTTP personnalisé
14+
contenant le chemin du fichier à servir. FrankenPHP se chargera du reste.
15+
16+
Cette fonctionnalité est connue sous le nom de **`X-Sendfile`** pour Apache, et **`X-Accel-Redirect`** pour NGINX.
17+
18+
Dans les exemples suivants, nous supposons que le "document root" du projet est le répertoire `public/`
19+
et que nous voulons utiliser PHP pour servir des fichiers stockés en dehors du dossier `public/`,
20+
depuis un répertoire nommé `private-files/`.
21+
22+
## Configuration
23+
24+
Tout d'abord, ajoutez la configuration suivante à votre `Caddyfile` pour activer cette fonctionnalité :
25+
26+
```patch
27+
root public/
28+
# ...
29+
30+
+ # Needed for Symfony, Laravel and other projects using the Symfony HttpFoundation component
31+
+ request_header X-Sendfile-Type x-accel-redirect
32+
+ request_header X-Accel-Mapping ../private-files=/private-files
33+
+
34+
+ intercept {
35+
+ @accel header X-Accel-Redirect *
36+
+ handle_response @accel {
37+
+ root private-files/
38+
+ rewrite * {resp.header.X-Accel-Redirect}
39+
+ method * GET
40+
+
41+
+ # Remove the X-Accel-Redirect header set by PHP for increased security
42+
+ header -X-Accel-Redirect
43+
+
44+
+ file_server
45+
+ }
46+
+ }
47+
48+
php_server
49+
```
50+
51+
## PHP simple
52+
53+
Définissez le chemin relatif du fichier (à partir de `private-files/`) comme valeur de l'en-tête `X-Accel-Redirect` :
54+
55+
```php
56+
header('X-Accel-Redirect: file.txt') ;
57+
```
58+
59+
## Projets utilisant le composant Symfony HttpFoundation (Symfony, Laravel, Drupal...)
60+
61+
Symfony HttpFoundation [supporte nativement cette fonctionnalité](https://symfony.com/doc/current/components/http_foundation.html#serving-files).
62+
Il va automatiquement déterminer la bonne valeur pour l'en-tête `X-Accel-Redirect` et l'ajoutera à la réponse.
63+
64+
```php
65+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
66+
67+
BinaryFileResponse::trustXSendfileTypeHeader();
68+
$response = new BinaryFileResponse(__DIR__.'/../private-files/file.txt');
69+
70+
// ...
71+
```

docs/x-sendfile.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Efficiently Serving Large Static Files (`X-Sendfile`/`X-Accel-Redirect`)
2+
3+
Usually, static files can be served directly by the web server,
4+
but sometimes it's necessary to execute some PHP code before sending them:
5+
access control, statistics, custom HTTP headers...
6+
7+
Unfortunately, using PHP to serve large static files is inefficient compared to
8+
direct use of the web server (memory overload, reduced performance...).
9+
10+
FrankenPHP lets you delegate the sending of static files to the web server
11+
**after** executing customized PHP code.
12+
13+
To do this, your PHP application simply needs to define a custom HTTP header
14+
containing the path of the file to be served. FrankenPHP takes care of the rest.
15+
16+
This feature is known as **`X-Sendfile`** for Apache, and **`X-Accel-Redirect`** for NGINX.
17+
18+
In the following examples, we assume that the document root of the project is the `public/` directory.
19+
and that we want to use PHP to serve files stored outside the `public/` directory,
20+
from a directory named `private-files/`.
21+
22+
## Configuration
23+
24+
First, add the following configuration to your `Caddyfile` to enable this feature:
25+
26+
```patch
27+
root public/
28+
# ...
29+
30+
+ # Needed for Symfony, Laravel and other projects using the Symfony HttpFoundation component
31+
+ request_header X-Sendfile-Type x-accel-redirect
32+
+ request_header X-Accel-Mapping ../private-files=/private-files
33+
+
34+
+ intercept {
35+
+ @accel header X-Accel-Redirect *
36+
+ handle_response @accel {
37+
+ root private-files/
38+
+ rewrite * {resp.header.X-Accel-Redirect}
39+
+ method * GET
40+
+
41+
+ # Remove the X-Accel-Redirect header set by PHP for increased security
42+
+ header -X-Accel-Redirect
43+
+
44+
+ file_server
45+
+ }
46+
+ }
47+
48+
php_server
49+
```
50+
51+
## Plain PHP
52+
53+
Set the relative file path (from `private-files/`) as the value of the `X-Accel-Redirect` header:
54+
55+
```php
56+
header('X-Accel-Redirect: file.txt');
57+
```
58+
59+
## Projects using the Symfony HttpFoundation component (Symfony, Laravel, Drupal...)
60+
61+
Symfony HttpFoundation [natively supports this feature](https://symfony.com/doc/current/components/http_foundation.html#serving-files).
62+
It will automatically determine the correct value for the `X-Accel-Redirect` header and add it to the response.
63+
64+
```php
65+
use Symfony\Component\HttpFoundation\BinaryFileResponse;
66+
67+
BinaryFileResponse::trustXSendfileTypeHeader();
68+
$response = new BinaryFileResponse(__DIR__.'/../private-files/file.txt');
69+
70+
// ...
71+
```

0 commit comments

Comments
 (0)