Nginx throws 404 only on php scripts using php-fpm

nginxphp-fpm

I've installed a testing server using nginx + php-fpm. I've tried all of the following:

Nginx + Php5-fpm not rendering php files

nginx + php fpm -> 404 php pages – file not found

When accessing PHP files, nginx throws an 404 error

Summarizing what I've tried:

  • Reinstalling.
  • Changing the script privileges (changed them to 0777).
  • fastcgi_intercept_errors on.
  • Checked the root directive at the levels: server, location and location ~ \.php.
  • Checked the fastcgi_param SCRIPT_FILENAME directive.

The server returns 404 on (and only on) .php scripts. I can rename them to .html and they'd be fine. How can I go about this?

This is my nginx.conf:

user nginx;
worker_processes 1;

error_log  /var/log/nginx/error.log;

pid        /run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    keepalive_timeout  2;

    include /etc/nginx/conf.d/*.conf;

    index   index.html index.htm;

    server {
        listen       80;
        server_name  _;
        root         /var/www/html;

        location / {
            root /var/www/html;
            index index.php index.html index.htm;
        }

        error_page  404              /404.html;
        location = /40x.html {
            #root /var/www/html;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            #root /var/www/html;
        }

        location ~ \.php$ {
            root           /var/www/html;
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass   unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }


    }

}

Best Answer

Solved it. It turns out that the problem was the permissions set on the socket where php was listening. I just had to change a directive called listen.mode on /etc/php-fpm.d/www.conf

listen.mode = 0750

And set the user to nginx:

listen.owner = nginx
listen.group = nginx

So the file looks like this:

srwxr-x---. 1 nginx nginx 0 jul  8 08:59 /var/run/php5-fpm.sock

Because I was using a unix socket instead of a tcp port:

listen = /var/run/php5-fpm.sock;

Also, I was getting 404 instead of 500 or 503 because my www.conf was configured to redirect errors to custom pages, and since they weren't there, I was getting 404's.

Edit:

It appears that in most recent versions of the nginx distribution in Fedora (Fedora 22, 23), nginx uses the apache user by default, and the socket is set to the user apache too, so no further configuration is needed.