繁体   English   中英

使用 Nginx + PHP-FPM 访问 PHP 文件被拒绝 (403)

[英]Access denied (403) for PHP files with Nginx + PHP-FPM

我一直在这个问题上花了几个小时,尽管与它相关的帖子数量很多,但我无法解决它。 我有一个带有 Nginx + PHP-FPM 的 Fedora 20 盒子,直到今天都运行良好(我猜是在我重新加载了 php-fpm.service 之后)。 Nginx 提供静态文件没有问题,但任何 PHP 文件都会触发错误 403。

权限没问题,nginx和php-fpm都在用户“nginx”下运行:

root     13763  0.0  0.6 490428 24924 ?        Ss   15:47   0:00 php-fpm: master process (/etc/php-fpm.conf)
nginx    13764  0.0  0.1 490428  7296 ?        S    15:47   0:00 php-fpm: pool www
nginx    13765  0.0  0.1 490428  7296 ?        S    15:47   0:00 php-fpm: pool www
nginx    13766  0.0  0.1 490428  7296 ?        S    15:47   0:00 php-fpm: pool www
nginx    13767  0.0  0.1 490428  7296 ?        S    15:47   0:00 php-fpm: pool www
nginx    13768  0.0  0.1 490428  6848 ?        S    15:47   0:00 php-fpm: pool www

提供的文件也已设置为 nginx 用户,我什至结束了 chmoding 777 这些文件的尝试,但对于任何 PHP 文件仍然“拒绝访问”。

下面是我的 Nginx 配置的服务器:

server {
        listen          80;
        server_name     localhost;

        root            /var/www/html;

         location ~ \.php$ {
            fastcgi_intercept_errors on;
            try_files $uri =404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

PHP-FPM 池:

[www]
...
listen = 127.0.0.1:9000
user = nginx
group = nginx
...

对于版本:

php-5.5.11 (当然还有php-fpm-5.5.11

nginx-1.4.7

我正在添加 Nginx 错误日志:

 FastCGI sent in stderr: "Access to the script '/var/www/html' has been denied (see security.limit_extensions)" while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xxx.xxx.xxx.xxx"

准确地说security.limit_extensions是正确的,设置为: security.limit_extensions = .php

关于路径权限,可以遍历/var/www/html 我错过了什么?

以下是一些可能的解决方案:

  1. 在您的 php-fpm www.conf 中,将security.limit_extensions设置为.php.php5或任何适合您环境的文件。 对于某些用户,完全删除所有值或将其设置为FALSE是使其正常工作的唯一方法。

  2. 在您的 nginx 配置文件中,将fastcgi_pass设置为您的套接字地址(例如unix:/var/run/php-fpm/php-fpm.sock; )而不是您的服务器地址和端口。

  3. 检查您的SCRIPT_FILENAME fastcgi 参数并根据文件的位置进行设置。

  4. 在你的 nginx 配置文件中包含fastcgi_split_path_info ^(.+\\.php)(/.+)$; 在定义所有其他 fastcgi 参数的位置块中。

  5. 在你的 php.ini 中将cgi.fix_pathinfo设置为1

请注意,上述解决方案(将cgi.fix_pathinfo设置为1 )是一个糟糕的主意。 请参阅https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/以获得一个很好的概述。

问题可能归结于您的应用程序依赖 PATH_INFO。 为 php 启用访问日志以获取有关如何调用应用程序的更多信息,以帮助您调试此问题。

再一次,为了确定 - 接受的解决方案是一个糟糕的主意,并且可能会让您的网站遭到黑客攻击。

更改php.ini后不要忘记重启php5-fpm服务!!

服务 php5-fpm 重启或服务 php5-fpm 重新加载

fpm 预启动 php5,因此仅重新启动 nginx 以应用更改是不够的。

可能与selinux有关。 如果使用Virtual Box共享文件夹,则在 Linux 中无法更改此文件夹中的访问权限。 因此,您可以在关闭selinux后解决它。

供以后参考:在您站点的 conf 中尝试添加: fastcgi_param PATH_INFO $fastcgi_path_info; 还要看看 SELinux 在做什么。 关闭它: setenforce 0 但是然后找出问题所在的脚本并放回 setenforce 1

如果您的 vhost 文档根目录中没有index.php ,也可能发生这种情况。

仔细检查 nginx 配置中的www_root参数。 然后仔细检查您尝试访问的 php 文件是否确实在那里。

就我而言,我错误地输入了 vhost doc 根路径,因此将其指向一个空目录,产生了 403。

我遇到了同样的问题,在尝试了上面的所有建议后,它仍然没有用。 然后我检查了我的文件。 显然我不是服务器专家。

原来我有一个文件夹 /sites-available 和 /sites-enabled 。 我已经更新了sites-available 文件夹中的默认文件,但没有更新/sites-enabled 文件夹中的默认文件。 这样做后(并重新启动 nginx),一切都开始工作了。

我花了一些时间来解决这个问题,我希望这可以帮助其他人尽快解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM