File Not Found when running PHP with Nginx
When you see a "Primary script unknown" error in your Nginx logs, it usually points to a mismatch between the filesystem path Nginx is looking for and the actual location of your application code. In the specific configuration provided, this issue stems from how the SCRIPT_FILENAME parameter is constructed relative to the root directive.
The error occurs within the location ~ \.php$ block. Here, the directive fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; explicitly tells Nginx that every PHP request should be resolved in a directory named /scripts. However, your server's root is defined as /usr/share/nginx/html.
When you access a file like http://localhost/index.php, Nginx combines the configured root (/usr/share/nginx/html) with the script name to determine the full path. But because SCRIPT_FILENAME overrides this with an absolute prefix of /scripts, Nginx attempts to serve the script from /scripts/index.php. Since that directory does not exist, PHP returns a 500 error, and Nginx logs that the primary script is unknown.
To resolve this, you need to align the SCRIPT_FILENAME path with your actual application root. The most robust way to do this in modern Nginx configurations is to remove the hardcoded /scripts prefix and allow Nginx to use the standard fastcgi_script_name. You should update the configuration block as follows:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
By changing SCRIPT_FILENAME to $document_root$fastcgi_script_name, you instruct Nginx to dynamically resolve the path based on the current location context. If your site is served from /usr/share/nginx/html, PHP will correctly look for the script in that directory rather than searching in a non-existent /scripts folder.
Another common pitfall involves the root directive itself. In the original example, the root is set to /usr/share/nginx/html. If your actual application code resides elsewhereâsay, in /var/www/myappâyou should adjust the root directive or use a more specific path for PHP files. For instance, using fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; is often a relic from older tutorials where the web root and script root were distinct. In modern setups, keeping them consistent avoids confusion.
If you are deploying a Laravel application specifically, this misconfiguration can cause critical failures immediately upon startup. While Nginx serves static assets correctly, any route requiring controller logic will fail because PHP cannot locate the entry point files. If you are managing multiple projects or using symbolic links, ensure that $document_root accurately reflects the directory containing your app, bootstrap, and public folders.
Sometimes, developers try to fix this by adding a generic root /scripts; directive inside the PHP location block, but that is redundant and often incorrect unless /scripts is explicitly mounted as a separate volume or directory in your filesystem structure. The cleanest approach is always to rely on $document_root or an explicit absolute path that matches your project layout.
Finally, after making these changes, remember to test the configuration syntax before reloading Nginx. Run nginx -t to verify there are no syntax errors, then apply the changes with nginx -s reload. This ensures your PHP-FPM service communicates correctly with the web server without triggering further upstream errors.
