linux系统nginx服务器不能访问php文件问题

发布时间:2020-08-26作者:小灵龙点击:179

 

今天在部署网站时html能访问,php文件不能访问,点击浏览器提示下载。

配置Nginx服务器之后,打开域名,是直接下载而不是直接打开网页,是因为配置的Nginx无法解析PHP的原因。
根据我出现的问题,我的解决办法如下:
1、打开nginx.conf配置文件,

server
那个127.0.0.1的端口号9000是nginx与fastcgi交互的id和端口号,也就是fastcgi监听的端口。
location ~ .php$ 表示匹配到php文件就进行fastcgi操作。

location / {}表示请求根路径时, 都会走这里。第一个

uri表示真实路径,/index.php 表示前面都没有的话访问index.php,$args:所有参数。如果要处理url美化时,必须要配置这里。

2.检查127.0.0.1:9000端口是否处于监听状态,执行 netstat -antp | grep 9000 ,如果没有被监听,说明需要启动。

3.执行 php-cgi -b 127.0.0.1:9000 &   启动之后,在执行netstat -antp结果:
在这里插入图片描述
4。重启nginx,systemctl restart nginx 问题解决。
5,如果出现 No input file specified.报错,则把/script修改为$document_root
 

如果thinkphp5这样支持”/index.php/Home/Index/index”这种网址是这么配置的。

# 典型配置
  location ~ \.php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
    include fastcgi_params;
  }

# 修改第1,6行,支持pathinfo

  location ~ \.php(.*)$ { # 正则匹配.php后的pathinfo部分
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT$fastcgi_script_name;
    fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量    
    include fastcgi_params;
    }

 

标签: