You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NGINX PHP FastCGI多location(alias)配置异常求助

Troubleshooting the "File not found." Error in Your Second Nginx Location Block

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/alias and SCRIPT_FILENAME: This is the #1 cause of this error with PHP-FPM. If your second location uses alias instead of root, you can't use the standard $document_root$fastcgi_script_name for SCRIPT_FILENAME—you need to switch to $request_filename instead. Nginx handles alias paths differently, so the parameter has to reflect that.
  • Incorrect file paths: Double-check that the directory specified in root or alias actually 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-data or php-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:

  1. The full code of both your location blocks
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:17:27