Nginx + PHP - 入力ファイルが指定されていません

Nginx + PHP –入力ファイルが指定されていません

一般的なNginx + PHPエラーメッセージ「入力ファイルが指定されていません。」

image

nginx.conf

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

    server {
        listen       80;
        server_name  localhost;

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

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9999;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

Nginx 1.12.1 + PHP 7.1でテスト済み

溶液

location / {}のルートファイルパスがlocation ~ \.php$に適用されないため、PHPは実行する.phpファイルを見つけることができません。

これを解決するには、次のようにルートファイルパスをサーバーブロックに移動します。

nginx.conf

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

    server {
        listen       80;
        server_name  localhost;

        # Move the root file path to server block.
        root www;

        location / {
            #root www;
            index  index.html index.htm;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9999;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}