Decoding the "Primary script unknown" Error in Nginx
When you encounter the error FastCGI sent in stderr: "Primary script unknown" in your Nginx access logs, it is a clear indicator that the web server cannot find the specific PHP entry point defined in your configuration for the requested URI. While this often feels like a permission issue, especially given the common frustration of setting directories to 777, the root cause here is almost exclusively a mismatch between the file system path Nginx expects and the actual location of your application's index file.
The error log entry you are seeing reveals the specific request: GET /index.php HTTP/1.1. The upstream connection to PHP-FPM on 127.0.0.1:9000 succeeded in establishing a connection, but the PHP process immediately rejected the request because it could not find the script at $document_root$fastcgi_script_name.
In your sites-available configuration, you have defined your root as /home/willem/git/console/frontend/www. However, Nginx constructs the path to the script by concatenating this root with the SCRIPT_FILENAME variable. The critical line in your config is:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
The variable $fastcgi_script_name typically resolves to the URI requested (e.g., /index.php). Consequently, Nginx and PHP are looking for a file at /home/willem/git/console/frontend/www/index.php.
If that file does not exist in that specific directory, you get the "Primary script unknown" error. It is crucial to understand that this is not a permissions issue. Setting your home directory or project folders to 777 is an administrative band-aid that compromises security but does not help Nginx locate files. The web server process (www-data) needs read and execute permissions on the directory structure, but it requires the file itself to physically exist at the constructed path.
To resolve this, you must verify the actual filesystem layout. Run the following command to check if the expected file exists:
ls -la /home/willem/git/console/frontend/www/index.php
If the file is missing, it may be that your project structure expects the index.php to reside directly in /home/willem/git/console, rather than inside the frontend/www subdirectory. If you intended for Nginx to serve files from the parent directory, you need to adjust the root directive:
root /home/willem/git/console;
Alternatively, if your application framework (such as Yii or Laravel) requires specific routing logic where the index.php is located elsewhere, you must ensure that the URI rewrite rules correctly point to that file. Your current configuration uses try_files $uri $uri/ /index.php?$args;. This rule attempts to serve static files first and then falls back to /index.php relative to the root. If your application expects the entry point to be named differently or located in a different subfolder, this fallback will fail.
Additionally, ensure that your PHP-FPM pool is configured to listen on 127.0.0.1:9000 as specified in your fastcgi_pass directive. A mismatch here would typically result in a connection refused error rather than "script unknown," but verifying the upstream configuration is always a good practice when debugging FastCGI issues.
If you are migrating from Apache to Nginx, remember that Apache's mod_rewrite and .htaccess files do not translate directly. The path-based routing logic must be explicitly defined in your nginx.conf or site-specific config files. For modern PHP applications like those built on the Laravel ecosystem, similar configuration pitfalls exist when deploying on shared hosting or Docker environments where the document root and upstream paths must align perfectly.
In summary, the error is a structural mismatch, not a permission denial. Locate your actual entry point script, verify it exists at the path constructed by Nginx, and adjust your root directive or application structure accordingly.