nginx + php-fpm = File not found
When info.php returns a "File not found" error in an nginx environment, the immediate assumption is often a missing file or a broken web root. While checking filesystem permissions is a valid first step, the specific configuration provided reveals a more subtle issue involving how nginx constructs the path to your PHP scripts. The error stems from a mismatch between the directory where the files actually live and the variable logic used by fastcgi_param SCRIPT_FILENAME.
The core of the problem lies in this line within your location block:fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
This directive attempts to hardcode the root path (/usr/share/nginx/html) and append the script name. This approach is brittle for two reasons. First, standard nginx installations on Ubuntu or Debian typically serve documents from /var/www/html, not /usr/share/nginx/html. If your actual files reside in /var/www (as hinted by your root /var/www directive), this hardcoded path points to a non-existent directory, causing the "File not found" response before the PHP process even starts.
Second, and more critically, using a static prefix for every request ignores the root directive defined in your generic location block. When you set root /var/www, nginx expects any subsequent paths to be relative to that root unless explicitly overridden. By hardcoding /usr/share/nginx/html, you effectively tell PHP to look for info.php at /usr/share/nginx/html/info.php, which likely doesn't exist, while the web server is actually serving requests from /var/www/info.php.
To fix this, you must allow nginx to dynamically construct the full path based on the request's URI. The standard and robust way to handle this is by passing the SCRIPT_FILENAME variable with the $document_root and $fastcgi_script_name variables. This ensures that regardless of which subdirectory your application lives in (e.g., /var/www/myapp/), the correct absolute path is passed to PHP-FPM.
Here is the corrected configuration:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:7777;
fastcgi_index index.php;
# Allow nginx to resolve the root dynamically based on the request
include fastcgi_params;
# Override SCRIPT_FILENAME to ensure it matches the actual file location
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Keep timeouts and buffer settings if needed for high-load scenarios
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
}
There is a specific nuance regarding include fastcgi_params. This include file typically defines the default value for $document_root as $server_root, which is often /usr/share/nginx/html. If you rely on the included defaults without overriding SCRIPT_FILENAME, you might inadvertently revert to the hardcoded path behavior depending on your nginx version or distribution defaults. Explicitly setting fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; overrides this default, ensuring consistency.
It is also worth noting that if you are running a specific framework like Laravel, using include fastcgi_params alongside manual parameter definitions can sometimes lead to conflicts regarding session IDs or environment variables. In such cases, it is safer to include the params file only for settings you don't need to override, or ensure your custom overrides come after the included parameters in the configuration order.
Finally, verify that your PHP-FPM pool on port 7777 is listening on 127.0.0.1 and that the user running the nginx worker process has permission to read the files in /var/www. A "File not found" error from nginx usually means the file doesn't exist at the path calculated by SCRIPT_FILENAME, but a 502 Bad Gateway would indicate the PHP-FPM service is down or rejecting the connection entirely.
For those looking to manage this configuration without touching server files directly, services like LaravelCompany offer managed hosting solutions that abstract these underlying nginx and PHP-FPM complexities, ensuring that your deployment scripts handle the path resolution automatically.