在nginx webserver上的php open_basedir中排除一个目录

时间:2011-07-17 14:43:39

标签: php apache nginx

我正在尝试为我的网站设置一个php open_basedir,我只有两个位置来执行我的php脚本,我需要在文档根目录下的子文件夹上禁用php。我不知道这是否可能。

是doc根需要php执行

/home/bastian/html
/home/bastian/meatalheavy

我需要在以下目录上禁用php exection

/home/bastian/html/ass_ests
/home/bastian/html/images/

的php.ini

open_basedir = "/home/bastian/html" ; "/home/bastian/meatalheavy"

2 个答案:

答案 0 :(得分:0)

open_basedir PHP Manual的设置设置对目录的访问权限,而不是特定目录。因此,如果您想限制子目录,则不得允许任何父目录。

注意:根据你的目录布局(和PHP版本),你想要做的是不可能,或者至少没有大量的配置(单独允许所有文件和子文件夹)。

答案 1 :(得分:0)

open_basedir设置应该写成:

open_basedir = "/home/bastian/html:/home/bastian/meatalheavy"

这实际上不会阻止在子目录上执行php脚本,它还允许从open_basedir的子目录中包含php脚本(include[_once]require[_once]。但是你可能需要在你的其他路径中使用som open basedir,临时目录上使用的任何外部PHP库(尤其是上传目录)或session.save_path。

open_basedir = "/home/bastian/html:/home/bastian/meatalheavy:/var/lib/php5/:/path/to/my/php/defined/temporary/dir:/path/to/my/session/dir"

所以这可以包含(无法从包含中删除子树),现在为了防止在某些子目录上执行php,你只需要阻止直接访问那里的php脚本(与后端不同的是 - php访问)。对于Nginx,这应该没问题:

location ~* ^/(ass_ests|images)/.*\.(php|php5)$
{
    deny all;
}

但你也可以使用this blog post中描述的mim类型。其中图像目录的内容被强制为图像,因此即使具有图像扩展名的脚本也不会被执行。在FastCgi模式下,可以使用图像文件中的一些PHP执行。 所以对于你的图像目录(不要知道你在另一个目录中有什么),这将是

    location /images/
    {
            types
            {
                    image/gif       gif;
                    image/jpeg      jpeg jpg;
                    image/png       png;
                    text/plain      txt;
            }
            default_type    application/octet-stream;
            location ~ \.(php|php5)$
            {
                    break;
            }
    }