NGINX PHP FastCGI多location(alias)配置异常求助
Hey there! Let's work through this Nginx issue you're dealing with—seeing that generic "File not found." message after fixing the 403/404 errors is definitely frustrating, especially when you're still getting up to speed with the tech. Let's break down the most common causes and fixes first, then we can narrow it down further with a bit more detail from your setup.
Common Culprits to Check
- Mismatched
root/aliasandSCRIPT_FILENAME: This is the #1 cause of this error with PHP-FPM. If your second location usesaliasinstead ofroot, you can't use the standard$document_root$fastcgi_script_nameforSCRIPT_FILENAME—you need to switch to$request_filenameinstead. Nginx handlesaliaspaths differently, so the parameter has to reflect that. - Incorrect file paths: Double-check that the directory specified in
rootoraliasactually exists on your server, and that it contains the PHP files you're trying to access. Typos in paths are easier to miss than you think! - PHP-FPM permissions: Even if Nginx can access the files, the PHP-FPM process user (usually
www-dataorphp-fpm) might not have read permissions for the directory or files in your second location block.
Sample Configs to Compare Against
If you're using root for your second location, here's a working template:
location /your-second-path/ { root /var/www/your-second-project-folder; index index.php index.html; location ~ \.php$ { fastcgi_pass unix:/run/php/php8.2-fpm.sock; # Adjust to your PHP-FPM socket/port fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
If you're using alias, you need to tweak the PHP block like this:
location /your-second-path/ { alias /var/www/actual-project-files/; index index.php index.html; location ~ \.php$ { alias /var/www/actual-project-files/; fastcgi_pass unix:/run/php/php8.2-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; include fastcgi_params; } }
Next Steps for Precise Debugging
To get to the bottom of this quickly, it would help to share:
- The full code of both your location blocks
- The complete line from your Nginx error log (it probably includes the exact path PHP was trying to access, which is a huge clue)
For example, if your error log line looks like this:
2024/05/20 14:22:10 [error] 4567#4567: *12 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.1.5, server: your-domain.com, request: "GET /second-path/test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "your-domain.com"
That directly points to a SCRIPT_FILENAME mismatch between what Nginx is sending to PHP-FPM and where your files actually live.
内容的提问来源于stack exchange,提问作者PBX

