当前位置: 代码迷 >> 综合 >> Apache httpd 2.4 访问控制
  详细解决方案

Apache httpd 2.4 访问控制

热度:59   发布时间:2023-12-14 22:59:14.0

Apache访问控制可以由几个不同的模块完成。其中最重要的是mod_authz_core和mod_authz_host。Apache使用Require指令进行授权来确保用户被允许或拒绝访问资源。其中mod_authz_host模块可以使用ip,host,forward-dns和local扩展授权类型。其他授权类型也可以使用,但可能需要加载额外的授权模块。这些授权提供程序会影响哪些主机可以访问服务器的某个区域。访问可以通过主机名,IP地址或IP地址范围进行控制。

一、基于主机的访问控制

由mod_authz_host实现的授权提供者是使用Require指令注册的。 
该指令作用域<Directory><Files><Location>部分.htaccess文件中通常,访问限制指令适用于所有访问方法(GET,PUT,POST等)。 这在大多数情况下是需要的行为。
但是,通过将指令放在<Limit>部分中,可以限制某些方法,而使其他方法不受限制。

二、特定上下文环境常见的访问控制

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 10 172.20 192.168.2
??允许特定IP或IP段,多个IP或IP段间使用空格分隔

三、基于IP地址的访问控制

单个或多个IP
??Require ip 10.1.2.3
??Require ip 192.168.1.104 192.168.1.205

基于部分IP地址的访问控制
??Require ip 10.1
??Require ip 10 172.20 192.168.2

基于网络/子网掩码的访问控制
??Require ip 10.1.0.0/255.255.0.0
??Require ip 10.1.0.0/16

基于IPv6的访问控制
??Require ip 2001:db8::a00:20ff:fea7:ccea
??Require ip 2001:db8:1:1::a #Author : Leshami
??Require ip 2001:db8:2:1::/64 #Blog :http://blog.csdn.net/leshami
??Require ip 2001:db8:3::/48

基于主机名的访问控制
??Require host example.org
??Require host .net example.edu

基于forward-dns方式的访问控制
??forward-dns允许根据简单的主机名来访问服务器。 当指定require forward-dns host-name时,所有与主机名对应的IP地址都被允许访问。该方法不依赖于反向DNS查询:它仅查询DNS的主机名,并允许客户端的IP匹配。 因此,它只适用于主机名,而不是域名。 但是,由于不使用反向DNS,它将与使用动态DNS服务的客户端协同工作。

??Require forward-dns bla.example.org
??能够被bla.example.org解析的IP地址将被允许访问

仅允许本机访问
??Require local

四、混合配置

黑名单方法
<RequireAll>Require all grantedRequire not ip 10.252.46.165
</RequireAll>Require not ip 192.168.205
Require not host phishers.example.com moreidiots.example
Require not host gov白名单方法
Require ip 192.168.1.104 192.168.1.205基于变量和表达式的访问控制方法
<If "%{HTTP_USER_AGENT} == 'BadBot'">Require all denied
</If>Require expr %{HTTP_USER_AGENT} != 'BadBot'

五、配置示例

环境
[root@centos7-router ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built:  Nov 19 2015 21:43:13配置允许所有客户端访问
注释原有的DocumentRoot,并添加下列内容到httpd.conf
[root@centos7-router ~]# tail -8 /etc/httpd/conf/httpd.conf 
DocumentRoot "/u01/web"
<Directory "/u01/web">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted
</Directory># echo "This is a access privileges test page.">>/u01/web/index.html
[root@centos7-router ~]# systemctl reload httpd
[root@centos7-router ~]# curl http://localhost:90
This is a access privileges test page.配置拒绝所有客户端访问
Require all granted 成 Require all denied并reload httpd
# curl http://localhost:90
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /
on this server.</p>
</body></html>配置允许192.168.1.0网段访问,注意,此处使用RequireAll容器
DocumentRoot "/u01/web"<Directory "/u01/web">Options Indexes FollowSymLinksAllowOverride None<RequireAll>Require ip 192.168.1</RequireAll>
</Directory>从1.253服务器上进行访问
[robin@ydq4 ~]$ ip addr |grep inet|grep globalinet 192.168.1.253/24 brd 192.168.1.255 scope global eth0
[robin@ydq4 ~]$ curl http://192.168.1.175:90
This is a access privileges test page.

五、更多参考

https://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require
http://httpd.apache.org/docs/2.4/mod/mod_authz_core.html

  相关解决方案