“Primary script unknown”: Nginx and Apache with PHP-FPM
Updated 25 November 2026 · By Brent Alexis
Short answer: PHP-FPM was handed a file path, and there was no PHP file at that path. The visitor sees File not found. with a 404. Either the file really doesn't exist (often a scanner probing for .php files), or the path Nginx or Apache sends (SCRIPT_FILENAME) doesn't match where PHP-FPM sees your site: a path copied from a tutorial, or two Docker containers with the site in different folders. Make the path match, and stop missing files from reaching PHP at all.
Tested on Debian 12 (Nginx 1.22.1 and Apache 2.4.71, PHP 8.2.36) and on the official nginx (1.30.8) and php:8.3-fpm (8.3.37) Docker images, with ProxyForge's own Config Generator output: 33 checks. How it's tested
- The log lines
- A .php file that doesn't exist
- SCRIPT_FILENAME points somewhere else
- Apache: AH01071
- Docker: two containers, two paths
- 403 “Access denied.”: PHP can't read the file
The log lines
Nginx writes PHP-FPM's message into its own error log (/var/log/nginx/error.log):
[error] FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: example.com, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock:", host: "example.com"
Apache logs it under its own code (/var/log/apache2/error.log):
[proxy_fcgi:error] [pid 88:tid 91] [client 127.0.0.1:39118] AH01071: Got error 'Primary script unknown'
In both cases the page says File not found. with status 404. That text comes from PHP-FPM, not from Nginx or Apache, which is how you can tell the request did reach PHP. One exception: in the official php Docker image the log line doesn't appear at all (below).
A .php file that doesn't exist
If someone asks for /nope.php and the web server passes it to PHP-FPM without checking, PHP-FPM is the one that finds nothing, and you get the log line above. Scanners often do this, asking for wp-login.php and similar files on sites that don't have them, so many of these lines are noise. The Config Generator's PHP-FPM output for Nginx stops it with one line (try_files $uri =404;):
location ~ \.php$ {
# 404 for .php files that don’t exist, instead of passing them to PHP
try_files $uri =404;
# fastcgi_params ships with every Nginx package (Debian, RHEL, nginx.org)
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Socket: Debian/Ubuntu unix:/run/php/php8.X-fpm.sock, RHEL/Alma/Rocky unix:/run/php-fpm/www.sock
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Checked: with that block, /nope.php was a 404 from Nginx and nothing was logged. With the try_files line removed, the same request reached PHP-FPM and logged “Primary script unknown”. Change the socket to your PHP version: the lab's Debian 12 has PHP 8.2, so it used php8.2-fpm.sock. A socket that doesn't exist is a 502, not this error (see the 502 guide).
SCRIPT_FILENAME points somewhere else
When the file does exist and you still get “Primary script unknown”, the path PHP-FPM receives is wrong. SCRIPT_FILENAME is that path. The generator's line builds it from your site's root:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Two mistakes gave “Primary script unknown” for an index.php that was there, in /var/www/example.com:
- A path copied from a tutorial:
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;sends PHP to/var/www/html/index.php. Use$document_rootinstead of a fixed folder. - The line left out: Debian's
fastcgi_paramsfile doesn't setSCRIPT_FILENAME, soinclude fastcgi_params;on its own isn't enough. Many answers say this gives a blank white page. With PHP 8.2 it didn't: it was the same 404 “File not found.” and the same log line.
sudo nginx -t && sudo systemctl reload nginx
Apache: AH01071
Until 25 November 2026, the generator's Apache virtual host handed every .php request to PHP-FPM without checking that the file exists:
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost/"
</FilesMatch>
So a missing /nope.php logged AH01071: Got error 'Primary script unknown'. Wrapping the handler in an <If> fixed it: the config passed apachectl configtest, existing PHP files still ran, and missing ones got Apache's own 404 with nothing logged. The generator's output now does this itself:
<FilesMatch \.php$>
# 404 for .php files that don’t exist, instead of passing them to PHP
<If "-f %{REQUEST_FILENAME}">
SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost/"
</If>
</FilesMatch>
Checked again with that exact output (socket changed to Debian 12's php8.2-fpm.sock): PHP files ran, /nope.php got Apache's 404 with no AH01071, and in the WordPress output, permalinks such as /about/ still reached index.php. The .htaccess output has the same check; it worked with AllowOverride All, and PHP still ran with only AllowOverride FileInfo.
The path problem exists on Apache too. A ProxyPassMatch line copied from a tutorial, ProxyPassMatch "^/(.*\.php)$" "unix:/run/php/php8.2-fpm.sock|fcgi://localhost/var/www/html/$1", sent PHP to /var/www/html while the site was in /var/www/example.com: AH01071 for a file that existed. SetHandler inside FilesMatch, as above, uses the real path of the file Apache found, so there's no folder to get wrong.
sudo apachectl configtest && sudo systemctl reload apache2
Docker: two containers, two paths
A common setup runs Nginx in one container and PHP-FPM in another. Nginx sends a path, and PHP-FPM looks for it inside its own container. In the lab, the site was in /var/www/example.com in the nginx container but in /var/www/html (the php image's default) in the php container: 404 “File not found.” for a file that existed.
The surprise: nothing was logged. No “Primary script unknown” in Nginx's log, and only a plain 404 line in PHP-FPM's. The official php image turns PHP-FPM's message off: its /usr/local/etc/php/conf.d/docker-fpm.ini contains fastcgi.logging = Off. So on this setup, a File not found. page with no error is your clue.
Fix: mount the site at the same path in both containers. With the file at /var/www/example.com in both, it worked. Keep the file in the nginx container too: with the generator's try_files line, a file that was only in the php container was a 404 from Nginx, because Nginx checks for it locally first.
403 “Access denied.”: PHP can't read the file
A related message has a different cause. With a PHP file set to 600 and owned by root, the page was Access denied. with status 403, and Nginx's log said (Apache logged the same message under AH01071):
Unable to open primary script: /var/www/example.com/locked.php (Permission denied)
PHP-FPM's pool runs as www-data on Debian and Ubuntu, and that user needs to read the file. chmod 644 fixed it. For folders, see the Nginx 403 guide: the same rules apply.
One trap while testing: PHP keeps compiled scripts in memory (OPcache). A file that had already run once kept working after it was set to 600. So a permission change can look like it did nothing. Test with a new file. Restarting PHP-FPM clears that memory (sudo systemctl restart php8.2-fpm; not lab-tested).
Generate a PHP-FPM config for Nginx or Apache →
How to use ProxyForge · FAQ: common errors · Guides · What's new · About