如题所示,在使用Linux上的Nginx+PHP的架构的时候,访问服务器端的PHP页面,前端返回了错误【file not found】;
具体情况如下:

当前:PHP与Nginx服务是运行的:

[root@HJLnginx nginx]# ps -ef | grep nginx
root       831     1  0 Nov10 ?        00:00:00 /sbin/dhclient -1 -q -lf /var/lib/dhclient/dhclient--eth0.lease -pf /var/run/dhclient-eth0.pid -H HJLnginx eth0
root     10308     1  0 10:18 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   10309 10308  0 10:18 ?        00:00:00 nginx: worker process
root     10326 15673  0 10:23 pts/0    00:00:00 grep --color=auto nginx
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# ps -ef | grep php
root     10328 15673  0 10:23 pts/0    00:00:00 grep --color=auto php
root     27343     1  0 09:53 ?        00:00:00 php-fpm: master process (/usr/local/php-7.0.9/etcphp-fpm.conf)
nobody   27344 27343  0 09:53 ?        00:00:00 php-fpm: pool www
nobody   27345 27343  0 09:53 ?        00:00:00 php-fpm: pool www
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# netstat -tupln | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10308/nginx: master 
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# netstat -tupln | grep php
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      27343/php-fpm: mast 
[root@HJLnginx nginx]# 

服务器上的页面文件也是存在的:

[root@HJLnginx nginx]# pwd
/usr/local/nginx
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# ls -ltr html
total 12
-rw-r--r-- 1 root root 612 Nov 13 10:13 index.html
-rw-r--r-- 1 root root 494 Nov 13 10:13 50x.html
-rw-r--r-- 1 root root  19 Nov 13 10:19 me.php
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# cat html/me.php 
<?php phpinfo();?>
[root@HJLnginx nginx]# 

但是,前端访问页面却有问题:

其实,出现这个问题的原因是因为Nginx配置文件中涉及到PHP的那部分的配置不当导致的;

默认的Nginx的配置文件直接解锁注释后,是这样的:

[root@HJLnginx nginx]# pwd
/usr/local/nginx
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# cat conf/nginx.conf | grep -A 10 "pass the PHP scripts"
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        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;
        }

        # deny access to .htaccess files, if Apache's document root
[root@HJLnginx nginx]# 

我们需要对它做出一些更改,并重新加载配置:

[root@HJLnginx nginx]# vi conf/nginx.conf
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# cat conf/nginx.conf | grep -A 10 "pass the PHP scripts"
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ .*\.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
[root@HJLnginx nginx]# 
[root@HJLnginx nginx]# sbin/nginx -s reload
[root@HJLnginx nginx]# 

然后,再次查看页面:


至此,文首的问题已经得到了回答与解决。

说点什么

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒
隐藏
变装