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

feu*_*eub 23 php permissions fedora nginx http-status-code-403

我一直在这个问题上花费几个小时,尽管有相关的帖子数量很多,但我无法解决.我有一个配有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
Run Code Online (Sandbox Code Playgroud)

服务文件也被设置为nginx用户,我甚至结束了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;
        }
}
Run Code Online (Sandbox Code Playgroud)

PHP-FPM池:

[www]
...
listen = 127.0.0.1:9000
user = nginx
group = nginx
...
Run Code Online (Sandbox Code Playgroud)

对于版本:

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"
Run Code Online (Sandbox Code Playgroud)

和精确的说security.limit_extensions是正确的,设置为:security.limit_extensions = .php.

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

VF_*_*VF_ 38

这是一些可能的解决方案:

  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_FILENAMEfastcgi参数并根据文件的位置进行设置.

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

  5. 在你的php.ini中设置cgi.fix_pathinfo1

  • 你确定将fix_pathinfo设置为1是否安全?查看http://serverfault.com/questions/627903/is-the-php-option-cgi-fix-pathinfo-really-dangerous-with-nginx-php-fpm (2认同)

mde*_*ers 8

请注意,上述解决方案(设置cgi.fix_pathinfo1)是一个糟糕的主意.有关概述,请参阅https://nealpoole.com/blog/2011/04/setting-up-php-fastcgi-and-nginx-dont-trust-the-tutorials-check-your-configuration/.

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

再一次,只是为了确定 - 接受的解决方案是一个糟糕的想法,并可能会使您的网站被黑客入侵.