Windows7安装 nginx+php 后访问.php文件出现 “No input file specified.” 的解决办法

在Windows7上安装了Nginx+PHP,参考教程为 http://www.javashuo.com/article/p-swnsykib-d.htmlphp

启动 nginx 后,在浏览器中输入localhost能够看到成功访问nginx的页面:
Nginx 欢迎页面html

在html目录中添加了一个phpinfo.php文件,内容为:nginx

<?php
phpinfo();
?>

在浏览器中输入:浏览器

localhost:/phpinfo.phpcode

页面却显示:
没法访问php文件htm

后来意识到是 nginx 安装目录下的 conf/nginx.conf 文件配置不正确:blog

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi_params;
        }

如上配置中的 SCRIPT_FILENAME , /scripts 是不存在的路径,应当更改成存放 .php 文件的目录路径:教程

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  D:/nginx-1.15.3/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

或者使用 $document_root 来代替,该变量即为 location 配置块中的 root 指定的目录 ‘html’。ip


反思总结

回头再看那篇参考教程,里面的配置就是把 SCRIPT_FILENAME 写成 '$document_root/$fastcgi_script_name' (原文中 $document_root 后面少了一个 '/'),当时由于在配置文件中没有找到 $document_root 的定义,因此就没放在心上,觉得是各自的环境不同的缘由,如今看来,$document_root 是 nginx 的一个内置环境变量,专门用来指代当前 location 配置块中的 root 变量。get


补充:

刚才试验了一下,发现

$document_root/$fastcgi_script_name (有'/')

$document_root$fastcgi_script_name (无'/')

均可以使 nginx 正常找到 .php 文件

相关文章
相关标签/搜索