apache: 配置 - 目录文件限制(2.2旧版本)



0. apache2.2版本的目录文件控制

模块:mod_access_compat
简介:
在新版本中被mod_authz_host替代
可用在<Directory><Files><Location>.htaccess文件中
可控制hostname,IP或者request的变量
用Allow和Deny来控制客户端的访问权限
用Order来设置默认访问状态,和设定Allow、Deny的生效顺序

用法简介:

举例说明

*******************************************************
Order Deny,Allow
Allow from example.org
Deny from foo.example.org
*******************************************************
a. foo.example.org匹配Deny,但同时满足Allow,通过
b. example.org匹配Allow,通过
c. 其他所有不匹配*.example.org的,通过
综述,所有条件的域名均可通过以上规则

## 实例
## 只允许本机访问特定目录下的php文件
*******************************************************
<Directory /path/to/dir>
    php_admin_flag engine off
    <Filesmatch "(.*)php$">
            Order deny,allow
            Deny from all
            Allow from 127.0.0.1
    </Filesmatch>
</Directory>
*******************************************************

1. apache2.4版本的目录文件控制

1) Require Directive

作用:检查是否用户被authorization provider授权
模块:mod_authz_core
语法:Require [not] entity-name [entity-name] ...
container: <RequireAll>, <RequireAny> and <RequireNone>

authorization provider汇总:

  1. Require all granted
    Access is allowed unconditionally
  2. Require all denied
    Access is denied unconditionally
  3. Require env env-var [env-var] ...
    Access is allowed only if one of the given environment variables is set
  4. Require method http-method [http-method] ...
    Access is allowed only for the given HTTP methods
  5. Require expr expression
    Access is allowed if expression evaluates to true 示例:
    Require expr "%{TIME_HOUR} -ge 9 && %{TIME_HOUR} -le 17"

mod_authz_user,mod_authz_host,mod_authz_groupfile提供的用法

  1. Require user userid [userid] ...
    Only the named users can access the resource.
  2. Require group group-name [group-name] ...
    Only users in the named groups can access the resource.
  3. Require valid-user
    All valid users can access the resource.
  4. Require ip 10 172.20 192.168.2
    Clients in the specified IP address ranges can access the resource.

用法举例:

## env:允许KnockKnoick/2.0开头的User-Agent访问
SetEnvIf User-Agent ^KnockKnock/2\.0 let_me_in
<Directory "/docroot">
    Require env let_me_in
</Directory>

## expr:
Require expr "%{TIME_HOUR} -ge 9 && %{TIME_HOUR} -le 17"

# 以下两种同义
<RequireAll>
    Require expr "!(%{QUERY_STRING} =~ /secret/)"
    Require expr "%{REQUEST_URI} in { '/example.cgi', '/other.cgi' }"
</RequireAll>

Require expr "!(%{QUERY_STRING} =~ /secret/) && %{REQUEST_URI} in { '/example.cgi', '/other.cgi' }"

2) container