天天看点

Nginx + PHP-fpm File not found.问题解决记录403 forbidden错误

版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/80051117

现象确认:

打开浏览器的开发者工具,查看请求信息

HTTP/1.1 404 Not Found

Server: nginx/1.11.10

Date: Fri, 20 Apr 2018 08:10:13 GMT

Content-Type: text/html; charset=UTF-8

Transfer-Encoding: chunked

Connection: keep-alive

X-Powered-By: PHP/5.6.30

说明结果是由FastCGI返回的。

问题原因:

估计是跟权限有关系

相同的配置,使用以下两个: 

/opt/local/share/nginx/html  
/opt/local/share/nginx/html/php           

目录都可以,但使用: 

/Users/xxx/Downloads/workForder           

目录却不可以。

通过 ls -l  查看权限,发现区别在于 

/Users/xxx/Downloads/workForder           

目录中的文件没有扩展属性,也就是没有@符号。

后来修改Nginx的静态文件root目录到:

/Users/xxx/Downloads/workForder           

访问出现:

403 forbidden错误

再看 nginx 403 ,找到问题原因了,需要修改配置文件中的user 和 group。

nginx的格式是:

user 你的用户名/root owner;
比如:
user root owner;           

PHP-fpm的分别修改:

user = root
group = owner           

然后重新启动。

PHP-fpm 自身没有重启的命令,比较坑!只能杀进程了。

配置文件:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

        location ~ \.php$ {
            root           /Users/xxx/Downloads/workForder;
           # root           /opt/local/share/nginx/html/php;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;

         }           

这里只展示与PHP有关的部分。当我在配置中加入 

try_files $uri =404;           
 后,就会看到由Nginx返回的:      

访问PHP文件时,你可能会看到下面这样的错误:

那是因为FastCGI没有启动,需要启动:

sudo php-fpm  -D           

而如果Nginx和PHP的都指向同一个目录,那么还是会看到:

File not found