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

在Windows7上安装了Nginx+PHP,参考教程为 https://www.cnblogs.com/anlia/p/5916758.html

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

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

<?php
phpinfo();
?>

在浏览器中输入:

localhost:/phpinfo.php

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

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

        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’。


反思总结

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


补充:

刚才试验了一下,发现

$document_root/$fastcgi_script_name (有'/')

$document_root$fastcgi_script_name (无'/')

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

posted @ 2018-09-02 11:11  HorseShoe2016  阅读(6595)  评论(0编辑  收藏  举报