Nginx + php5-fpm = "File not found"

Mathew asked:

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?

My answer:

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

    location ~ \.php$ {
            try_files $uri =404;
            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.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.