I've hit a wall whilst setting up a site using nginx / fpm. The page displays "File not found", and this appears in the nginx error.log:

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

I'm new to both nginx and fpm, and that error message means nothing to me (even the google machine hasn't helped!). Can anyone shed any light onto what could be happening?

up vote 18 down vote accepted

You should have a location section to handle PHP requests configured similarly to this:

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index   index.php;
            fastcgi_param   SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include         fastcgi_params;
    }

(The extra try_files resolves a security vulnerability which could allow arbitrary files to be executed as PHP.)

In addition, your root should be defined in the server section of the configuration file, not the location section. This is one of the most common nginx misconfigurations.

  • +1 for that misconfigurations article - really worth a read, particularly if you're learning Nginx. Well written, entry-level, with several great tips! – Ben Jan 20 '14 at 1:22

This is a note for passenger installs.

I just installed nginx from source via passenger which caused a problem with php5-fpm. The default nginx.conf makes use of the problem described by Michael Hampton. The solution is to remove the blok around the root and index directives, so:

location / {
    root html
    index index.html index.htm
}

becomes:

root html
index index.html index.htm

Furthermore the php block is incorrectly set up. See Michael Hamptons answer for the correct way to it.

An additional note could be that if php5-fpm is set up to use sockets point the fastcgi_pass parameter in the php block in nginx.conf to the socket setup in /etc/php5/fpm/pool.d/www.conf.

I Just had this problem in a new version of nginx. (config taken from an older version)

What I had to do was place the include fastcgi_params; above my custom SCRIPT_FILENAME like this:

location @web {
        try_files $uri =404;
        include         fastcgi_params;
        fastcgi_pass    127.0.0.1:9000;
        fastcgi_param   SCRIPT_FILENAME  $document_root/index.php;
}

As the SCRIPT_FILENAME was being overwritten.

If you're using aliases in your location blocks, an unhandled 404 error can also exhibit this behavior. You can see this if the page displayed in the browser is the simple text "File not found" as opposed to the prettier formatted (centered) nginx 404 page. Essentially, it's really saying the 404 page can't be found.

To solve, add an additional try_files $uri =404 line in your location block and reload the nginx config. In addition to what Michael Hampton said about solving a specific security vulnerability, this also allows the fastcgi handler to override the alias definition and find the 404 script in the default location.

sudo vim /etc/php-fpm.conf

about line 149, change php user && user group

I test it successful now.

  • It Works for me. Thank you very much! – Love Jun 1 '17 at 6:59
  • This is way is right! Hey man, you can try it! – Love Jun 1 '17 at 7:00
  • When you change the user and group, You don't forget to restart php-fpm.If you use centos6 you can use this command: sudo service php-fpm restart – Love Jun 1 '17 at 7:01

I have seen :

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

in a server I placed under high load when stress testing. My suspicion, still to be confirmed, is that the available file handles from the OS were exhausted. In which case php-fpm cannot get a reference to the file.

I realise this is speculative but it certainly fits my scenario and might also help someone else.

Thanks @homeway, Your answer inspire me. Thank you very much!

I meet the same question, But Other method didn't help me resolve the question!

I resolve it, I find the key is that: Linux User Right lead to the question: FastCGI sent in stderr: "Primary script unknown"

Because The PHP-FPM default user:group is apache:apache, But your code dir is someBody:someBody. So you should change the User right!

I write a blog to resolve this question, You can see this blog:

[Nginx FastCGI sent in stderr: "Primary script unknown"][1] ` [1]: http://geekhades.blogspot.com/2017/06/nginx-fastcgi-sent-in-stderr-primary.html

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.