Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx and PHP-FPM show File not found or Primary script unknown

ErrorFastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

Short 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.

  • Applies to nginx 1.24, PHP 8.3 FPM, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1File not found. with the site root correct for static files but wrong for .phproot is missing or different in the PHP locationFix
    2. 2Primary script unknown for every PHP file, even ones that existSCRIPT_FILENAME is not sent at allFix
    3. 3The path in nginx's config exists on the nginx side, but not where FPM runsPHP-FPM sees a different file system than nginxFix
    4. 4Access denied. or No input file specified. instead of File not found.The message is Access denied or No input file specifiedFix

What does the error look like?

  • FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
  • File 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.php

Confirmed by nginx.org

Did this fix it?

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 nginx

Confirmed by raw.githubusercontent.com

Did this fix it?

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/*.conf

2Fix

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 nginx

Confirmed by php.net

Did this fix it?

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 readable

2Fix

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.php

3Verify

Request the page; the test command prints readable.

curl -I http://example.com/index.php

Confirmed by php.net

Did this fix it?

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:

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 fix

Servers

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.