Nginx + php5-fpm = "File not found"

Solution 1:

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.

Solution 2:

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.


Solution 3:

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.