feat(nginx): internal /_accel/ location for X-Accel-Redirect from storage/app

An app that has authorised a file can answer with an empty body and
"X-Accel-Redirect: /_accel/<path under storage/app>"; nginx then sends the file
itself (Range requests included). The location re-adds the CORS headers from the
upstream response, which nginx otherwise drops on this redirect. The php location
announces it to PHP as $_SERVER['X_ACCEL_STORAGE'] = '/_accel/'.

Harmless for an app that does not use it: the location is `internal`, reached
only through an X-Accel-Redirect the app itself returns, so no response of such
an app changes. The one visible difference is that an external request to
/_accel/... gets nginx's 404 instead of reaching the app's front controller; no
app here routes under /_accel/. The fastcgi param only adds a server variable
nothing reads unless the app asks for it (it is not a request header, so it
changes no headers, and Symfony's own X-Sendfile support ignores it). nginx -t
passes on 1.26.3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-04 11:58:52 +01:00
parent 3a8cbfed6c
commit 2d6d6383de
2 changed files with 21 additions and 0 deletions

View File

@ -131,6 +131,7 @@ services:
- **PECL**: redis, ev, igbinary, imagick (graceful fallback if unavailable for a PHP version) - **PECL**: redis, ev, igbinary, imagick (graceful fallback if unavailable for a PHP version)
- **OPcache** with JIT auto-enabled on PHP 8.0+ - **OPcache** with JIT auto-enabled on PHP 8.0+
- **Nginx** with headers-more module, optimized for Laravel - **Nginx** with headers-more module, optimized for Laravel
- **X-Accel-Redirect** for files under `storage/app`: after its own access check the app answers with an empty body and `X-Accel-Redirect: /_accel/<path under storage/app>`, and nginx sends the file (Range requests, CORS headers passed on). The internal location is announced to PHP as `$_SERVER['X_ACCEL_STORAGE'] = '/_accel/'`, so an app can switch it on only when the image has it
- **Composer** (latest) - **Composer** (latest)
- **Node.js** + npm - **Node.js** + npm
- **Image optimizers**: optipng, pngquant, gifsicle, webp, avif, svgo - **Image optimizers**: optipng, pngquant, gifsicle, webp, avif, svgo

View File

@ -21,6 +21,22 @@ server {
try_files $uri $uri/ /index.php?$query_string; try_files $uri $uri/ /index.php?$query_string;
} }
# Files Laravel has authorised (X-Accel-Redirect): PHP checks access, then
# answers with an empty body and "X-Accel-Redirect: /_accel/<path under
# storage/app>", and nginx sends the file itself, Range requests included.
# internal: no client can request it directly. On this redirect nginx keeps
# only a few upstream headers (Content-Type, Cache-Control, Expires,
# Content-Disposition, Set-Cookie, Accept-Ranges), so the CORS ones are
# passed on here; an empty value adds no header.
location ^~ /_accel/ {
internal;
alias /var/www/html/storage/app/;
add_header Access-Control-Allow-Origin $upstream_http_access_control_allow_origin always;
add_header Access-Control-Allow-Credentials $upstream_http_access_control_allow_credentials always;
add_header Access-Control-Expose-Headers $upstream_http_access_control_expose_headers always;
add_header Vary $upstream_http_vary always;
}
location ~ [^/]\.php(/|$) { location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_param HTTP_PROXY ""; fastcgi_param HTTP_PROXY "";
@ -29,6 +45,10 @@ server {
fastcgi_param SERVER_NAME $host; fastcgi_param SERVER_NAME $host;
fastcgi_pass 127.0.0.1:9000; fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
# Tells the app that /_accel/ exists (above). A server variable, not a
# request header, so a client cannot fake it; without it the app streams
# files through PHP as before.
fastcgi_param X_ACCEL_STORAGE /_accel/;
include fastcgi_params; include fastcgi_params;
} }
} }