基于客户端地址的访问控制

通过对客户端IP地址的限制可以增强服务器的安全性,客户端IP地址限制只能针对服务器上的某个目录进行设置,大致格式如下:

317 <Directory "/var/www/html">
338     AllowOverride None
343     Order allow,deny
344     Allow from all
346 </Directory>

<Directory "/var/www/html">...</Directory>表示对站点的主目录设置,“Order allow,deny”表示默认拒绝所有客户端访问,然后再配合使用“Allow from”语句指定允许访问的客户端。如果是要设置拒绝访问的客户端,那么就得反过来,先用“Order deny,allow”允许所有客户端访问,然后再配合使用“deny from”拒绝指定的客户端。设置允许访问的地址可以是多个,地址之间用空格间隔,如deny from 10.15.72.73 10.16.72.83

如不允许Client访问Apache Server:

Apache server ip:10.15.72.38   Client:10.15.72.73

当前访问Apache Server:

Web服务基础四之Apache访问控制_httpd

修改Apache不允许访问:

[root@justin html]# vim /etc/httpd/conf/httpd.conf
 317 <Directory "/var/www/html">
 331     Options Indexes FollowSymLinks
 338     AllowOverride None
 343     Order deny,allow
 344     Deny from 10.15.72.73
 346 </Directory>
[root@justin html]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]
[root@justin html]#

Web服务基础四之Apache访问控制_httpd.conf_02


Client无法访问Apache Server

对虚拟目录设置访问控制

如对bbs的虚拟目录/virdir设置,设置前:

Web服务基础四之Apache访问控制_httpd_03

[root@justin html]# vim /etc/httpd/conf/httpd.conf
1010 NameVirtualHost 10.15.72.38:80
1011 <VirtualHost 10.15.72.38:80>
1012     DocumentRoot /var/www/html/bbs
1013     ServerName bbs.justin.com
1014     Alias /virdir "/home/www/virdir"
1015     <Directory "/home/www/virdir">
1016 #   order allow,deny
1017     order deny,allow
1018     deny from 10.15.72.73
1019     </Directory>
1020 </VirtualHost>

设置后:

Web服务基础四之Apache访问控制_apache_04


基于用户的访问控制

在实际应用中,我们大都是希望通过对用户进行身份验证从而来进行访问控制,Apache支持的身份验证方法有基本认证(Basic)和摘要认证(Digest)两种,应用较多的同样是基本认证,以下介绍基本认证。

基于用户的访问控制也是只能针对服务器上的某个目录进行设置,设置内容必须包含在<Directory 目录> …… </Directory>的区域中。

如设置只允许justin1、justin2可以访问apache主目录,其他用户无法访问:

1、修改主配置文件中MAIN SERVER参数

317 <Directory "/var/www/html">
331     Options Indexes FollowSymLinks
338     AllowOverride None
343     Order allow,deny
344     allow from all
345     AuthName "html"
346     AuthType Basic
347     AuthUserFile /etc/httpd/conf/.htpasswd
348     require valid-user
349 </Directory>

   AuthName:定义受保护的领域名称,客户端访问时在弹出的认证登陆对话框中将显示该名称。

   AuthType:设置认证的类型,Basic为基本认证

   AuthUserFile:设置用于保存用户帐号、密码的认证文件路径。(文件可以自由定义,但通常都是保存于/etc/httpd/conf目录中,并采           用.htpasswd的文件名,文件名前面加.表示隐藏文件。)

    require valid-user:授权给认证文件中的所有有效用户。这一项也可以写成“require user [用户名]”,指定一个具体的用户,这样无论认证文件中如何定义,也只有该用户才可以访问。

2、添加认证用户

这个认证用户与系统用户没有任何关系,不需要先创建相应的系统用户,可以直接来添加认证用户。

[root@justin html]# htpasswd -cm /etc/httpd/conf/.htpasswd justin1
New password:
Re-type new password:
Adding password for user justin1
[root@justin html]# htpasswd -m /etc/httpd/conf/.htpasswd justin2
New password:
Re-type new password:
Adding password for user justin2
[root@justin html]# cat /etc/httpd/conf/.htpasswd
justin1:$apr1$Zii8HQ7.$0EBdyJmQixiLifXuRDk7O/
justin2:$apr1$uUzIxR4R$e8vtC61eUdURYOY3F6rlj.
[root@justin html]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]
[root@justin html]#

-c:创建用户认证文件   -m:MD5加密 ;因为创建justin1时候已经生成了认证文件/.htpasswd,后面就不需要在加参数,只需要把用户写入认证文件即可。

3、访问Apache服务

Web服务基础四之Apache访问控制_httpd.conf_05Web服务基础四之Apache访问控制_httpd_06

上面定义的AuthName "html"就显示在上图中了

对虚拟目录设置访问控制

[root@justin html]# vim /etc/httpd/conf/httpd.conf
1013 NameVirtualHost 10.15.72.38:80
1014 <VirtualHost 10.15.72.38:80>
1015     DocumentRoot /var/www/html/bbs
1016     ServerName bbs.justin.com
1017     Alias /virdir "/home/www/virdir"
1018     <Directory "/home/www/virdir">
1019     AuthName "virdir"
1020     AuthType Basic
1021     AuthUserFile /etc/httpd/conf/.htpasswd
1022     require user justin2
1023     </Directory>
1024 </VirtualHost>
[root@justin html]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]
[root@justin html]#

访问Apache服务,这时候输入http://10.15.72.38/virdir会弹出身份认证对话框,此时输入据说justin1是无法访问,输入justin2就可以正常访问

###########################################################

                         关注微信平台,了解最新动态                                                                        

Web服务基础四之Apache访问控制_httpd_07由于学习阶段公开的文章随时进行完善,
修正的文章会通过微信平台给出提示,
欢迎关注,同时希望得到大家的指点.

Linux交流QQ群:333212786

Web服务基础四之Apache访问控制_apache_08################################################################################