nginx + PHP-FPM “File not found” / Primary script unknown
Short answer
A bare “File not found.” page with “Primary script unknown” in the log means PHP-FPM could not open the file named by SCRIPT_FILENAME. Nine times out of ten SCRIPT_FILENAME is missing or $document_root is wrong (root set inside a location); otherwise PHP-FPM cannot read the file or open_basedir blocks it.
Most common cause: SCRIPT_FILENAME is not set: include fastcgi_params alone is not enough (Debian's fastcgi.conf / snippets/fastcgi-php.conf sets it).
Try this first: root at server level, explicit SCRIPT_FILENAME
server {
root /var/www/site; # at server level so every location inherits it
index index.php index.html;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}Last verified: · against the official docs listed below · Sources
Typical log lines
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream FastCGI sent in stderr: "PHP message: PHP Warning: Unknown: open_basedir restriction in effect."
Common causes
- SCRIPT_FILENAME is not set: include fastcgi_params alone is not enough (Debian's fastcgi.conf / snippets/fastcgi-php.conf sets it).
- root is set only inside location /, so the PHP location does not inherit it and $document_root falls back to the default html directory.
- The PHP-FPM pool user (e.g. www-data) cannot read the site directory.
- PHP-FPM runs in a container where the path differs from what nginx sees.
- open_basedir in php.ini or the pool config does not include the site directory.
Diagnosis commands
grep -E 'Primary script unknown|open_basedir' /var/log/nginx/error.log | tail -3nginx -T 2>/dev/null | grep -nE 'root|SCRIPT_FILENAME|fastcgi_pass|include'sudo -u www-data test -r /var/www/site/index.php && echo readableFix configs
root at server level, explicit SCRIPT_FILENAME
server {
root /var/www/site; # at server level so every location inherits it
index index.php index.html;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}PHP-FPM in a container: use its own path
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; # the site directory inside the container fastcgi_pass php:9000;
Run nginx -t first, then systemctl reload nginx.
Key facts and sources
- $fastcgi_script_name is the request URI (plus fastcgi_index when the URI ends with a slash). [nginx.org]
- fastcgi_param directives are inherited from the previous level only if none are defined at the current level. [nginx.org]
Checked against the linked pages on 2026-11-14; the official docs win if a newer nginx changes them.
FAQ
fastcgi_params vs fastcgi.conf?
fastcgi.conf is fastcgi_params plus a SCRIPT_FILENAME line. With fastcgi_params you must set SCRIPT_FILENAME yourself.
Why do only PHP files fail?
nginx serves static files via root; PHP-FPM opens PHP files via SCRIPT_FILENAME, so a path or permission mismatch breaks only PHP.
Fix it with a tool
Related errors
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.