From 2d6d6383deb6289726abf480bc541a9bdb6e4f30 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Wed, 4 Nov 2026 11:58:52 +0100 Subject: [PATCH] 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/"; 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 --- README.md | 1 + config/default.conf | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index 648017f..c73d341 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ services: - **PECL**: redis, ev, igbinary, imagick (graceful fallback if unavailable for a PHP version) - **OPcache** with JIT auto-enabled on PHP 8.0+ - **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/`, 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) - **Node.js** + npm - **Image optimizers**: optipng, pngquant, gifsicle, webp, avif, svgo diff --git a/config/default.conf b/config/default.conf index 6d6eb3c..4f1b83b 100644 --- a/config/default.conf +++ b/config/default.conf @@ -21,6 +21,22 @@ server { 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/", 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(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_param HTTP_PROXY ""; @@ -29,6 +45,10 @@ server { fastcgi_param SERVER_NAME $host; fastcgi_pass 127.0.0.1:9000; 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; } }