这个问题的大部分答案是, 设置fastcgi_param SCRIPT_FILENAME,它将工作 (斜体格式化是打破?!)。
我已经设置了这个variables(正确),但它仍然显示错误,而不是404页面,因为问题的根源在这里:
location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; }
一个不存在的path被传递给php5-fpm,这将返回打印日志中的错误,如下所示:
FastCGI sent in stderr: "Unable to open primary script: ... (No such file or directory)" while reading response header from upstream
因此,在fastcgi_pass行之前,必须有一个条件来检查文件是否真的存在,或者如果fpm工作返回“找不到文件”,指导nginx返回一个404页面。
我怎样才能做到这一点?
使用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; }
感谢http://nginxlibrary.com/resolving-no-input-file-specified-error/
在一些老的教程中,你经常会发现这样的东西
fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (! -f $document_root$fastcgi_script_name) { return 404; }
但正如https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/指出,您应该更好地使用try_files
只是一个侧面说明:使用没有if / try_files块发布的configuration是危险的,因为它可以允许在某些情况下执行任意代码! 事实上网上有很多教程没有涉及这方面的内容,所以我build议大家检查一下他们的configuration是不是在工作,但也是安全的。