环境:nginx + php

问题:

配置的网站,访问出现报错:Access Denied (403)

常见解决方法:

1、文件权限问题

可能是文件权限问题,没有读权限。

或者selinux没有关闭。

2、security.limit_extensions

查看nginx的错误日志error.log,发现有如下错误:

// :: [error] #: * FastCGI sent in stderr: "Access to the script '/home/www/game/10313156.html' has been denied (see security.limi
t_extensions)" while reading response header from......

从5.3.9开始,php官方加入了一个配置"security.limit_extensions",默认只允许执行扩展名为".php"的文件,造成了其他类型的文件不支持的问题。

官方说明 :
; Limits the extensions of the main script FPM will allow to parse. This can
; prevent configuration mistakes on the web server side. You should only limit
; FPM to .php extensions to prevent malicious users to use other extensions to
; exectute php code.
; Note: set an empty value to allow all extensions.
; Default Value: .php
;security.limit_extensions = .php .php3 .php4 .php5

修改php-fpm.conf:(加入需要的文件扩展名)
security.limit_extensions = .php .html .js .css .jpg .jpeg .gif .png .htm

3、cgi.fix_pathinfo

通过这种url访问,显示Acess denied 错误。

nginx错误日志:

// :: [error] #: * FastCGI sent in stderr: "Access to the script '/home/www/home.php/game/qr' has been denied (see security.limit_extensions)" while reading response header......

修改php.ini :(cgi.fix_pathinfo 默认为1 )

cgi.fix_pathinfo = 1

官方说明 :
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is . You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo

其实cgi.fix_pathinfo = 1 会引发文件类型错误解析漏洞,建议是设置 cgi.fix_pathinfo = 0 。

关于漏洞危害的详情,可参考:

http://www.cnblogs.com/batsing/p/nginx_bug1_attack.html

http://www.phpvim.net/web/php/security-risks-caused-by-fix-pathinfo.html

( 关于漏洞的理解:

  当cgi.fix_pathinfo=1时,访问路径:/foo.jpg/file.php ,如果file.php文件不存在,则php解析器会试图猜测你要执行哪个文件,沿着路径往回找。如果foo.jpg存在, 并且包含php代码,php解析器就会去执行foo.jpg 。

  当cgi.fix_pathinfo=0时,PHP 解释器仅尝试给出的路径,如果文件没有找到就停止处理。

)

但将 cgi.fix_pathinfo = 0 ,可能会导致很多MVC框架(如ThinkPHP)无法正常运行。

4、

在php.ini上设置了:open_basedir=/home:/tmp/:/proc/
重启php-fpm

访问网页,按ctrl + F5频繁刷新的时候,会报 Access Denied错误。Access Denied是偶尔才会出现,不是一直403 。

nginx错误日志记录:

// :: [error] #: * FastCGI sent in stderr: "PHP message: PHP Warning:  Unknown: open_basedir restriction in effect. File(/home/www/touch/web/index.php) is not within the allowed path(s): (/home/wwwroot:/tmp/:/proc/) in Unknown on line 0
PHP message: PHP Warning: Unknown: failed to open stream: Operation not permitted in Unknown on line
Unable to open primary script: /home/www/touch/web/index.php (Permission denied)" while reading response header from upstream, client: 117.136.1.22, server: test.hjq.com, request: "GET /index.php?c=Zs&a=getcontent HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "test.hjq.com"
、In your nginx config file set fastcgi_pass to your socket address (e.g. unix:/var/run/php-fpm/php-fpm.sock;) instead of your server address and port.

、Check your SCRIPT_FILENAME fastcgi param and set it according to the location of your files.

、In your nginx config file include fastcgi_split_path_info ^(.+\.php)(/.+)$; in the location block where all the other fastcgi params are defined.

原文链接:http://stackoverflow.com/questions/23390531/access-denied-403-for-php-files-with-nginx-php-fpm

http://www.laruence.com/2010/05/20/1495.html

nginx + php 403 原因分析的更多相关文章

  1. nginx 偶发 403原因

    观察errorlog 日志 是否存在类似错误 [error] 12788#0: *322 connection is denied by policyframe[return code:8], 观察是 ...

  2. 网站假死 重启NGINX无效 必须重启PHP 原因分析

    一.错误提示说明: Nginx 502 Bad Gateway:请求的PHP-CGI已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致PHP-CGI进程终止. Nginx 504 G ...

  3. Nginx 504 Gateway Time-out分析及解决方法

    一.场景还原php程序在执行抓取远程图片库并保存至本地服务器的时候,出现了“504 Gateway Time-out”错误提示. 问题定位:由于图片巨多,所以下载时间很长(10分钟以上),引起网关超时 ...

  4. Centos75 解决Nginx出现403 forbidden(13: Permission denied)

    Centos75 新安装的vm,nginx出现403 forbidden 一般为SELinux设置为开启状态(enabled)的原因 切为root ,执行: sed -i 's/SELINUX=enf ...

  5. SELECT TOP 1 比不加TOP 1 慢的原因分析以及SELECT TOP 1语句执行计划预估原理

    本文出处:http://www.cnblogs.com/wy123/p/6082338.html 现实中遇到过到这么一种情况: 在某些特殊场景下:进行查询的时候,加了TOP 1比不加TOP 1要慢(而 ...

  6. [Linux] Nginx networking 403 Forbidden 静态文件不允许查看

    nginx 的 403 Forbidden errors 表示你在请求一个资源文件但是nginx不允许你查看. 403 Forbidden 只是一个HTTP状态码,像404,200一样不是技术上的错误 ...

  7. mysql索引无效且sending data耗时巨大原因分析

    一朋友最近新上线一个项目,本地测试环境跑得好好的,部署到线上却慢得像蜗牛一样.后来查询了一下发现一个sql执行了16秒,有些长的甚至80秒.本地运行都是毫秒级别的查询.下面记录一下困扰了两天的,其中一 ...

  8. 网站用域名能访问,用域名IP不能访问的原因分析

    原因分析:一般虚拟主机是不能直接输入IP进行访问的 因为一个IP下有很多网站 ,只能用域名进行访问.如果想IP也能访问,必须网站有独立的IP地址,不是共享IP.如果是IIS的话,要把主机头对应的域名去 ...

  9. JavaScript中的ParseInt("08")和“09”返回0的原因分析及解决办法

    今天在程序中出现一个bugger ,调试了好久,最后才发现,原来是这个问题. 做了一个实验: alert(parseInt("01")),当这个里面的值为01====>07时 ...

随机推荐

  1. Sencha Touch 2中如何动态添加button

    原理很简单无非就是在一个容器上面选中id,用容器的add方法将button循环加入. 现在我们来试一下 1.先定义一个Container组件用,以后在里面添加button ? 1 2 xtype:'c ...

  2. G - Power Strings

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  3. uva A Spy in the Metro(洛谷 P2583 地铁间谍)

    A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangero ...

  4. 权限问题导致无法删除ftp文件

    首先吐槽一下,使用新版编辑器,发了两遍愣是time out,果断放弃 这个文章也是一件小事,大致说一下: 有一个java操作ftp文件的程序,运行删除时,总是返回false,也没有报错.開始考虑是没有 ...

  5. edittext禁止android软键盘弹出

    1. EditText ed=(EditText) findViewById(R.id.test); ed.clearFocus(); 2. 在AndroidMainfest.xml中选择哪个acti ...

  6. git 的安装和使用

    安装Git 下载并安装 mysysgit 下载并安装 git windows版本号 配置Git 设置你的名字和邮箱 git config --global user.name "xxxx&q ...

  7. TTS-零基础入门之停止列表中单条语音播报

    做了一个语音 循环播报列表信息.当我新删除了一天列表之后,发现它仅仅有在下一轮播报中才会取消.这明显是不合理的. 一開始的代码是这样写的. <span style="font-fami ...

  8. mysql 日期计算,今天,明天,本周,下周,本月,下月

    --今天 DATE_FORMAT(BIRTH_DATE,'%Y-%m-%d') = CURDATE() --明天 DATE_FORMAT(BIRTH_DATE,'%Y-%m-%d') = TIMEST ...

  9. Java 8 实战 P1 Fundamentals

    目录 Chapter 1. Java 8: why should you care? Chapter 2. Passing code with behavior parameterization Ch ...

  10. Java项目打包发布

    Java项目打包发布 如果只想发布为一个可执行的jar包,使用eclipse的Export功能就可以了 使用eclipse的Export功能,将项目中的所有package打包为一个pet.jar文件, ...