`No input file specified.`的真正解决方案(nginx,fpm)

Dan*_* W. 8 nginx fastcgi php-fpm http-status-code-404

这个问题的大多数答案是,设置 fastcgi_param SCRIPT_FILENAME 并且它会起作用(斜体格式被破坏了?!)。

我已经设置了这个变量(正确)但它仍然显示错误而不是 404 页面,因为问题的根源在这里:

location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
Run Code Online (Sandbox Code Playgroud)

一个不存在的路径被传递给 php5-fpm,这反过来打印错误,在日志中看起来像:

FastCGI sent in stderr:
"Unable to open primary script: ... (No such file or directory)"
while reading response header from upstream
Run Code Online (Sandbox Code Playgroud)

所以在该行之前fastcgi_pass 必须有一个条件来检查文件是否真的存在,或者,如果fpm worker返回“file not found”,引导nginx返回404页面。

我怎样才能做到这一点?

Dan*_* W. 12

使用try_files $uri =404;第一!

location ~ \.php$ {
        try_files  $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
}
Run Code Online (Sandbox Code Playgroud)

感谢http://nginxlibrary.com/resolving-no-input-file-specified-error/