From a7dcc229768ca92f8f1e6271ba1b81aba35f65ce Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Wed, 25 Nov 2026 14:35:39 +0900 Subject: [PATCH] serve static files with Nginx X-Accel-Redirect `static.php` could (optionally) use Nginx's X-Accel-Redirect feature to offload static file's content delivery to webserver and reduce php execution footprint --- public_html/static.php | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/public_html/static.php b/public_html/static.php index 9853fef9c4..af55d6bb76 100644 --- a/public_html/static.php +++ b/public_html/static.php @@ -62,14 +62,33 @@ ? $_SERVER['PATH_INFO'] : explode('static.php/', $_SERVER['REQUEST_URI'] ?? '')[1] ?? ''; -$path = validateStaticFile($path); +$validated_path = validateStaticFile($path); -if (!$path) { +$real_path = realpath(INSTALL_PATH . $validated_path); + +if (!$validated_path || !$real_path) { http_response_code(404); exit; } -serveStaticFile($path); + +// Detect if web-server is Nginx with X-ACCEL config and let it do content delivery part +if ( + !empty($_SERVER['SERVER_SOFTWARE']) && + stripos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false && + !empty($_SERVER['X-ACCEL_PREFIX']) +) { + $accel_prefix = rtrim($_SERVER['X-ACCEL_PREFIX'], '/'); + + // let nginx take care about mime type + header("Content-Type: "); + header('X-Accel-Redirect: ' . $accel_prefix . '/' . $validated_path); + exit; +} + + +// server file's content via script +serveStaticFile($real_path); /** * Validate that the file exists and can be served @@ -121,11 +140,6 @@ function validateStaticFile(string $path): ?string return null; } - $path = realpath(INSTALL_PATH . $path); - - if ($path === false) { - return null; - } return $path; }