Nginx + php5-fpm = "File not found"

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.


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.