nginx and PHP-FPM show File not found or Primary script unknown
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstreamShort answer
PHP-FPM received a script path it could not turn into a file. The path is built from SCRIPT_FILENAME, so check what nginx sends: the root in the PHP location, the fastcgi_param line, and whether FPM can see the same folder. A different message, Access denied or No input file specified, points at permissions or extensions.
Fix it fast
1Find out which cause you have
Dump the active nginx config and look at the root and SCRIPT_FILENAME lines that apply to the PHP location. Then check that the file exists at exactly that path.
sudo nginx -T 2>/dev/null | grep -n -E "server_name|root |SCRIPT_FILENAME|fastcgi_pass" ls -l /var/www/example.com/public/index.php2Match what you see, then jump to its fix
- 1
File not found. with the site root correct for static files but wrong for .phproot is missing or different in the PHP locationFix - 2
Primary script unknown for every PHP file, even ones that existSCRIPT_FILENAME is not sent at allFix - 3
The path in nginx's config exists on the nginx side, but not where FPM runsPHP-FPM sees a different file system than nginxFix - 4
Access denied. or No input file specified. instead of File not found.The message is Access denied or No input file specifiedFix
- 1
What does the error look like?
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstreamFile not found.No input file specified.Access denied.
Cause 1 of 4
root is missing or different in the PHP location
1Check
FPM gets SCRIPT_FILENAME from nginx, often built as $document_root$fastcgi_script_name. If root is set only inside another location, the PHP location has no root of its own and the path is wrong.
sudo nginx -T 2>/dev/null | grep -n -B2 -A6 "location ~ \\.php"2Fix
Set root once at server level so every location inherits it, and build the script path from it.
server {
root /var/www/example.com/public;
index index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}3Verify
Test the config, reload, and request a PHP file.
sudo nginx -t && sudo systemctl reload nginx
curl -I http://example.com/index.phpConfirmed by nginx.org
Cause 2 of 4
SCRIPT_FILENAME is not sent at all
1Check
The PHP-FPM source returns 404 File not found when it has no script path: SCRIPT_FILENAME (and the PATH_TRANSLATED fallback) is absent. nginx lists SCRIPT_FILENAME among the minimum parameters for PHP.
sudo nginx -T 2>/dev/null | grep -n "SCRIPT_FILENAME"2Fix
Add the fastcgi_param line inside the PHP location, and keep a single definition of it.
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}3Verify
Reload nginx and request the page again.
sudo nginx -t && sudo systemctl reload nginxConfirmed by raw.githubusercontent.com
Cause 3 of 4
PHP-FPM sees a different file system than nginx
1Check
FPM opens the path nginx sends. With chroot set in the pool, the path is resolved inside that directory. The same happens when FPM runs in another container or host that does not have the site folder at the same path.
grep -n -E "^\s*chroot" /etc/php/8.3/fpm/pool.d/*.conf2Fix
Make the folder reachable at the same absolute path for both, or change SCRIPT_FILENAME to the path as FPM sees it. If chroot is set, remove it or build the path relative to the chroot directory.
# pool chrooted to /var/www/example.com, files at /public/index.php inside it
fastcgi_param SCRIPT_FILENAME /public$fastcgi_script_name;3Verify
Restart FPM, reload nginx, request the page.
sudo systemctl restart php8.3-fpm
sudo systemctl reload nginxConfirmed by php.net
Cause 4 of 4
The message is Access denied or No input file specified
1Check
FPM answers 403 Access denied when the extension is not allowed by security.limit_extensions (default .php .phar) or when opening the script fails with a permission error. Any other open failure gives 404 No input file specified.
grep -n "limit_extensions" /etc/php/8.3/fpm/pool.d/www.conf
sudo -u www-data test -r /var/www/example.com/public/index.php && echo readable2Fix
If the script has an extension you did not mean to run as PHP, do not widen limit_extensions; rename or route it. For a permission error, make the script and every folder above it readable by the FPM user.
sudo chmod o+x /var/www /var/www/example.com
sudo chmod o+r /var/www/example.com/public/index.php3Verify
Request the page; the test command prints readable.
curl -I http://example.com/index.phpConfirmed by php.net
How do I stop it coming back?
- Define root once at server level and use $document_root in SCRIPT_FILENAME, so there is one place to change.
- Run nginx -t after every config edit.
- If FPM runs in a container, mount the site folder at the same path in both containers.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Servers
nginx gives up waiting for the app and returns 504
upstream timed out (110: Connection timed out) while reading response header from upstream
nginx returns 504 when the app behind it takes the request but sends nothing back within proxy_read_timeout (60 seconds by default), or when the connection cannot be opened in time.
5 causes, each with a fixServers
nginx still returns 504 although the PHP script has a long time limit
upstream timed out (110: Connection timed out) while reading response header from upstream
set_time_limit() only controls PHP.
4 causes, each with a fixServers
nginx cannot open the PHP-FPM socket: Permission denied
502 Bad Gateway
nginx runs as one user and PHP-FPM creates its socket for another.
3 causes, each with a fixServers
Cookies from a PHP backend vanish behind an nginx proxy
Login works when you call the backend directly but fails through nginx
nginx forwards Set-Cookie headers from the backend unchanged, so lost cookies usually come from the cookie itself: a domain or path that does not match the public URL, or Secure and SameSite flags the browser rejects.
4 causes, each with a fixServers
display_errors has no effect on PHP behind nginx and PHP-FPM
500 Internal Server Error with no PHP message in the browser
With nginx there is no web-server PHP setting.
4 causes, each with a fixServers
nginx cannot find the PHP-FPM socket file
502 Bad Gateway
nginx and the PHP-FPM pool must name the same address.
2 causes, each with a fix