Nginx – Another PHP NGINX error: “No input file specified”

centosfastcginginxPHP

I looked through about 5 questions and blog posts already and tried everything they told me without luck. I can browse to www.example.com/phpinfo.php, but not www.example.com/index.php. I even went so far as to make phpinfo.php a symlink of index.php and phpinfo.php still works while index.php does not.

Nginx Config

[admin@web01 www]$ cat /etc/nginx/conf.d/example.com.conf 
server {
    listen       80;
    server_name  www.example.com;

    # Document root
    index  index.php;
    root   /var/www/sites/example.com/www;

    # Pass PHP scripts to FastCGI server
    location ~* \.php$ {
        include       fastcgi.conf;
        try_files     $uri =404;
        fastcgi_pass  127.0.0.1:9000;
    }
}

[admin@web01 www]$ cat /etc/nginx/fastcgi.conf

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Permissions

[admin@web01 www]$ ls -al /var/www/sites/example.com/www/*.php
-rw-rw-r--. 1 admin admin 17 Jan 24 10:37 /var/www/sites/example.com/www/index.php
lrwxrwxrwx. 1 admin admin  9 Jan 24 10:37 /var/www/sites/example.com/www/phpinfo.php -> index.php

[admin@web01 www]$ ls -alZ /var/www/sites/example.com/www/*.php
-rw-rw-r--. admin admin unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/sites/example.com/www/index.php
lrwxrwxrwx. admin admin unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/sites/example.com/www/phpinfo.php -> index.php

[admin@web01 ~]# ls -al /var
drwxr-xr-x. 19 root root  4096 Dec 15 18:02 .
dr-xr-xr-x. 23 root root  4096 Dec 15 17:48 ..
drwxr-xr-x.  7 root root  4096 Dec 15 18:13 www

[admin@web01 ~]# ls -al /var/www
drwxr-xr-x.  7 root  root  4096 Dec 15 18:13 .
drwxr-xr-x. 19 root  root  4096 Dec 15 18:02 ..
drwxr-xr-x.  7 admin admin 4096 Jan 23 11:35 sites

[admin@web01 ~]# ls -al /var/www/sites
drwxr-xr-x. 7 admin admin 4096 Jan 23 11:35 .
drwxr-xr-x. 7 root  root  4096 Dec 15 18:13 ..
drwxrwxr-x. 4 admin admin 4096 Jan 23 11:21 example.com

[admin@web01 ~]# ls -al /var/www/sites/example.com
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 .
drwxr-xr-x.  7 admin admin 4096 Jan 23 11:35 ..
drwxrwxr-x. 10 admin admin 4096 Jan 23 11:21 html
drwxrwxr-x.  2 admin admin 4096 Jan 27 09:00 logs
lrwxrwxrwx.  1 admin admin   11 Jan 23 11:21 www -> html/public

[admin@web01 ~]# ls -al /var/www/sites/example.com/html
drwxrwxr-x. 10 admin admin 4096 Jan 23 11:21 .
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 ..
drwxrwxr-x. 11 admin admin 4096 Jan 23 11:21 application
drwxrwxr-x.  8 admin admin 4096 Jan 23 11:21 data
drwxrwxr-x.  3 admin admin 4096 Jan 23 11:21 docs
drwxrwxr-x.  3 admin admin 4096 Jan 23 11:22 library
drwxrwxr-x.  6 admin admin 4096 Jan 27 15:57 public
drwxrwxr-x.  3 admin admin 4096 Jan 24 09:33 scripts
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 tests

[admin@web01 ~]# ls -al /var/www/sites/example.com/html/public
drwxrwxr-x.  6 admin admin 4096 Jan 27 15:57 .
drwxrwxr-x. 10 admin admin 4096 Jan 23 11:21 ..
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 assets
drwxrwxr-x.  4 admin admin 4096 Jan 23 11:21 css
-rw-rw-r--.  1 admin admin   17 Jan 24 10:37 index.php
drwxrwxr-x.  4 admin admin 4096 Jan 24 09:33 js
lrwxrwxrwx.  1 admin admin    9 Jan 24 10:37 phpinfo.php -> index.php

Error log

2014/01/24 10:37:52 [error] 8286#0: *15 FastCGI sent in stderr:
"Unable to open primary script: /var/www/sites/example.com/www/index.php
(No such file or directory)" while reading response header from upstream,
client: XXX, server: www.example.com, request: "GET /index.php HTTP/1.1",
upstream: "fastcgi://127.0.0.1:9000", host: "www.example.com"

Update

  1. Updated my config to include everything
  2. Added permissions for /, /var, /var/www, etc
  3. Turned off SELinux completely with no effect

Update #2

Removed the try_files line from the location ... .php$ line and edited php.ini's cgi.fix_pathinfo=0. Realized I hadn't restarted php-fpm this whole time. Magically working now! If you can explain why phpinfo.php was working the whole time and index.php was not (even though they resided in the same directory, and phpinfo.php was a symlink to index.php!!) you get the bounty.

Best Answer

Try adding this to your nginx.conf:

fastcgi_index index.php;

e.g. the relevant section of your nginx.conf file would look like this:

# Pass PHP scripts to FastCGI server
location ~ \.php$ {
    fastcgi_index index.php;
    include       fastcgi.conf;
    try_files     $uri =404;
    fastcgi_pass  127.0.0.1:9000;
}

What this does is tell fastcgi to use index.php if a file is not specified. For the record, I am using a patched nginx 1.2.9 (I changed ~* to ~ because that's how it looks in my nginx.conf)