File Not Found when running PHP with Nginx

PhpNginx

Php Problem Overview


Recently I installed the latest version of Nginx and looks like I'm having hard time running PHP with it.

Here is the configuration file I'm using for the domain:

server {
listen       80;
server_name  localhost;

location / {
    root   /usr/share/nginx/html;
    index  index.php;
}

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    include        fastcgi_params;
}

}

Here is the error I'm getting on the error log file:

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

Php Solutions


Solution 1 - Php

Try another fastcgi_param something like

fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;

Solution 2 - Php

I had the "file not found" problem, so I moved the "root" definition up into the "server" bracket to provide a default value for all the locations. You can always override this by giving any location it's own root.

server {
    root /usr/share/nginx/www;
    location / {
            #root /usr/share/nginx/www;
    }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

Alternatively, I could have defined root in both my locations.

Solution 3 - Php

I had been having the same issues, And during my tests, I have faced both problems:

1º: "File not found"

and

2º: 404 Error page

And I found out that, in my case:

I had to mount volumes for my public folders both on the Nginx volumes and the PHP volumes.

If it's mounted in Nginx and is not mounted in PHP, it will give: "File not found"

Examples (Will show "File not found error"):


services:
  php-fpm:
    build:
      context: ./docker/php-fpm
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:
        
        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
        # - Copy Public Folder:
      - ./sites/example.com/root/public/:/var/www/example.com/public
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

If it's mounted in PHP and is not mounted in Nginx, it will give a 404 Page Not Found error.

Example (Will throw 404 Page Not Found Error):

version: '3'

services:
  php-fpm:
    build:
      context: ./docker/php-fpm
    volumes:
      - ./sites/example.com/root/public/:/var/www/example.com/public
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:
        
        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

And this would work just fine (mounting on both sides) (Assuming everything else is well configured and you're facing the same problem as me):

version: '3'

services:
  php-fpm:
    build:
      context: ./docker/php-fpm
    volumes:
      # Mount PHP for Public Folder
      - ./sites/example.com/root/public/:/var/www/example.com/public
  nginx:
    build:
      context: ./docker/nginx
    volumes:
        #Nginx Global Configurations
      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./docker/nginx/conf.d/:/etc/nginx/conf.d

        #Nginx Configurations for you Sites:
        
        # - Nginx Server block
      - ./sites/example.com/site.conf:/etc/nginx/sites-available/example.com.conf
        # - Copy Public Folder:
      - ./sites/example.com/root/public/:/var/www/example.com/public
    ports:
      - "80:80"
      - "443:443"
    depends_on:
      - php-fpm
    restart: always

Also here's a Full working example project using Nginx/Php, for serving multiple sites: https://github.com/Pablo-Camara/simple-multi-site-docker-compose-nginx-alpine-php-fpm-alpine-https-ssl-certificates

I hope this helps someone, And if anyone knows more about this please let me know, Thanks!

Solution 4 - Php

Probably it's too late to answer but a couple things since this is a really annoying error. Following solution worked on Mac OS X Yosemite.

  1. It's the best if you have

> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

  1. The include with fast cgi params should go above that line.

  2. All your directories down to the PHP file you're executing (including that file too) should have a+x permissions, e.g.

> sudo chmod a+x /Users/ > sudo chmod a+x /Users/oleg/ > sudo chmod a+x /Users/oleg/www/ > sudo chmod a+x /Users/oleg/www/a.php

Solution 5 - Php

I just spent like 40 minutes trying to debug a non-working /status with:

$ SCRIPT_NAME=/status SCRIPT_FILENAME=/status QUERY_STRING= REQUEST_METHOD=GET cgi-fcgi -bind -connect /var/run/php5-fpm.sock

It just produced "File not found" error, while the actual scripts (that are found on the filesystem) worked just fine.

Turned out, I had a couple of orphaned processes of php5-fpm. After I killed everything and restarted php5-fpm cleanly, it just went back to normal.

Hope this helps.

Solution 6 - Php

In my case the PHP-script itself returned 404 code. Had nothing to do with nginx.

Solution 7 - Php

In my case, it was because the permissions on the root web directory were not set correctly. To do this, you need to be in the parent folder when you run this in terminal:

sudo chmod -R 755 htmlfoldername

This will chmod all files in your html folder, which is not recommended for production for security reasons, but should let you see the files in that folder, to be sure that isn't the issue while troubleshooting.

Solution 8 - Php

The error message “Primary script unknown or in your case is file not found.” is almost always related to a wrongly set in line SCRIPT_FILENAME in the Nginx fastcgi_param directive (Quote from https://serverfault.com/a/517327/560171).

In my case, I use Nginx 1.17.10 and my configuration is:

    location ~ \.php$ {
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      include fastcgi_params;
      fastcgi_read_timeout 600;
    }

You just change $document_root to $realpath_root, so whatever your root location, like /var/www/html/project/, you don't need write fastcgi_param SCRIPT_FILENAME /var/www/html/project$fastcgi_script_name; each time your root is changes. This configuration is more flexible. May this helps.

=================================================================================

For more information, if you got unix:/run/php/php7.2-fpm.sock failed (13: Permission denied) while connecting to upstream, just change in /etc/nginx/nginx.conf, from user nginx; to user www-data;.

Because the default user and group of PHP-FPM process is www-data as can be seen in /etc/php/7.2/fpm/pool.d/www.conf file (Qoute from https://www.linuxbabe.com/ubuntu/install-nginx-latest-version-ubuntu-18-04):

user = www-data
group = www-data

May this information gives a big help

Solution 9 - Php

I had this error as well. In my case it was because there was another virtual host that was pointing to the same root directory.

Solution 10 - Php

After upgrading to PHP72, we had an issue where the php-fpm.d/www.conf lost the settings for user/group which was causing this error. Be sure to double check those if your setup involves php-fpm.

Solution 11 - Php

For me, problem was Typo in location path.

Maybe first thing to check out for this kind of problem

Is path to project.

Solution 12 - Php

When getting "File not found", my problem was that there was no symlink in the folder where was pointing this line in ngix config:

root /var/www/claims/web;

Solution 13 - Php

in case it helps someone, my issue seems to be just because I was using a subfolder under my home directory, even though permissions seem correct and I don't have SELinux or anything like that. changing it to be under /var/www/something/something made it work.

(if I ever found the real cause, and remember this answer, I'll update it)

Solution 14 - Php

try this command

sudo chmod 755 -R htdocs/

Solution 15 - Php

my case: I used relative dir

       location ~* \.(css|js)\.php$ {

            root ../dolibarr/htdocs;

    		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    		fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    		fastcgi_index index.php;

    		 include /etc/nginx/fastcgi_params;
       }

this line did not work too : fastcgi_param SCRIPT_FILENAME /vagrant/www/p1/../p2/htdocs/core/js/lib_head.js.php;

So I discovered fastcgi_param does not support relative path.

This works

fastcgi_param SCRIPT_FILENAME /vagrant/www/p2/htdocs/core/js/lib_head.js.php;

Solution 16 - Php

The website will show "File Not Found" error.

enter image description here

You should check this configure file: /etc/nginx/nginx.conf (error_log) to get the nginx webservice log location. Then check nginx log: /var/log/nginx/error.log to get the root cause:

  • Keyword: FastCGI sent in stderr: "Primary script unknown"
  • Root cause: php-fpm service's account: apache doesn't have access permission to open webapps directory

Step 0. Find your root directory: open /etc/nginx/nginx.conf file and find root keyword

Root directory: /var/lib/nginx/webapps/ enter image description here

Step 1. Make sure apache account can access the ROOT directory.

sudo -u apache ls -l /var/lib/nginx/webapps/

Step 2. chmod a+x permission for all ROOT folder

sudo chmod a+x /var/
sudo chmod a+x /var/lib/
sudo chmod a+x /var/lib/nginx/
sudo chmod a+x /var/lib/nginx/webapps/

Solution 17 - Php

I have solved this issue in nginx version: nginx/1.21.3 PHP 7.4.23 macOS Catalina version 10.15.7

nano /usr/local/etc/nginx/nginx.conf


location ~ \.php$ {
            root           html;
            include        fastcgi.conf; 
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #Comment bellow Line
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #Add This line
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2298995View Question on Stackoverflow
Solution 1 - PhpdeaghView Answer on Stackoverflow
Solution 2 - PhpdogatonicView Answer on Stackoverflow
Solution 3 - PhpPablo CamaraView Answer on Stackoverflow
Solution 4 - PhpOleg BermanView Answer on Stackoverflow
Solution 5 - PhpAlex A.View Answer on Stackoverflow
Solution 6 - PhpPHZ.fi-PharazonView Answer on Stackoverflow
Solution 7 - PhpRyanView Answer on Stackoverflow
Solution 8 - PhpGame TerserahView Answer on Stackoverflow
Solution 9 - PhpSarcastronView Answer on Stackoverflow
Solution 10 - PhpAepodView Answer on Stackoverflow
Solution 11 - PhpStevan TosicView Answer on Stackoverflow
Solution 12 - PhpDarius.VView Answer on Stackoverflow
Solution 13 - PhpMauricioOttaView Answer on Stackoverflow
Solution 14 - PhpGrenobloisView Answer on Stackoverflow
Solution 15 - PhpscilView Answer on Stackoverflow
Solution 16 - PhpW. DanView Answer on Stackoverflow
Solution 17 - PhpNanhe KumarView Answer on Stackoverflow