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; } }