apache 2.2 和 2.4 访问控制区别 (require 替代 deny)

apache 2.4权限配置

Order命令已从Apache 2.4中删除

注意:使用require指令时,需要在指令外添加<RequireAll></RequireAll>标签对,否则重启Apache2.4加载规则时将出现错误:" negative Require directive has no effect in <RequireAny> directive "。

下面直接给出一些实例对Require指令的使用进行说明:

例1:允许所有访问请求

Apache2.4下的配置:

<Directory xxx/www/yoursite>

    <RequireAll>
Require all granted
</RequireAll> </Directory>

  

例2:拒绝所有访问请求

Apache2.4下的配置:

<Directory xxx/www/yoursite>

    <RequireAll>
Require all denied
</RequireAll> </Directory>

例3:只允许来自特定域名主机的访问请求,其他请求将被拒绝

<Directory xxx/www/yoursite>

    <RequireAll>
Require host google.com
</RequireAll> </Directory>

  

例4:只允许来自特定IP或IP段的访问请求,其他请求将被拒绝

Apache2.4下的配置:

<Directory xxx/www/yoursite>

    <RequireAll>
Require ip 192.120 192.168.100 192.168.1.1
</RequireAll> </Directory>

例5:允许所有访问请求,但拒绝来自特定IP或IP段的访问请求(阻止恶意IP或恶意爬虫网段的访问)

Apache2.4下的配置:

<Directory xxx/www/yoursite>

    <RequireAll>
Require all granted
Require not ip 192.168.1.1
Require not ip 192.120 192.168.100
</RequireAll> </Directory>

  

例6:允许所有访问请求,但拒绝某些User-Agent的访问请求(通过User-Agent屏蔽垃圾网络爬虫)

使用mod_setenvif通过正则表达式匹配来访请求的User-Agent,并设置内部环境变量BADBOT,最后拒绝BADBOT的访问请求。

Apache2.4下的配置:

<Directory xxx/www/yoursite>

    SetEnvIfNoCase User-Agent ".*(FeedDemon|JikeSpider|AskTbFXTV|CrawlDaddy|Feedly|Swiftbot|ZmEu|oBot).*" BADBOT
SetEnvIfNoCase User-Agent "brandwatch" BADBOT
SetEnvIfNoCase User-Agent "rogerbot" BADBOT
<RequireAll>
Require all granted
Require not env BADBOT
Require not ip 192.168.100.1
</RequireAll> </Directory>

  

其它require访问控制指令用法如下:

Require all granted #允许所有
Require all denied #拒绝所有
Require env env-var [env-var] ... #允许匹配环境变量中任意一个
Require method http-method [http-method] ... #允许特定的HTTP方法(GET/POST/HEAD/OPTIONS)
Require expr expression #允许,表达式为true
Require user userid [ userid ] ... #允许特定用户
Require group group-name [group-name] ... #允许特定用户组
Require valid-user # #允许,有效用户
Require ip 192.100 192.168.100 192.168.100.5 #允许特定IP或IP段,多个IP或IP段间使用空格分隔

  

了解更多require指令用法:《Apache Module mod_authz_core》

上一篇:HTML5 & how to download SVG in js


下一篇:Java8函数式接口以及lambda表达式实践