基于客户端地址的访问控制html
经过对客户端IP地址的限制能够加强服务器的安全性,客户端IP地址限制只能针对服务器上的某个目录进行设置,大体格式以下:apache
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.83vim
如不容许Client访问Apache Server:安全
Apache server ip:10.15.72.38 Client:10.15.72.73bash
当前访问Apache Server:服务器
修改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]#
Client没法访问Apache Server运维
对虚拟目录设置访问控制
ide
如对bbs的虚拟目录/virdir设置,设置前:学习
[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>
设置后:
基于用户的访问控制
在实际应用中,咱们大都是但愿经过对用户进行身份验证从而来进行访问控制,Apache支持的身份验证方法有基本认证(Basic)和摘要认证(Digest)两种,应用较多的一样是基本认证,如下介绍基本认证。
基于用户的访问控制也是只能针对服务器上的某个目录进行设置,设置内容必须包含在<Directory 目录> …… </Directory>的区域中。
如设置只容许justin一、justin2能够访问apache主目录,其余用户没法访问:
一、修改主配置文件中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 [用户名]”,指定一个具体的用户,这样不管认证文件中如何定义,也只有该用户才能够访问。
二、添加认证用户
这个认证用户与系统用户没有任何关系,不须要先建立相应的系统用户,能够直接来添加认证用户。
[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,后面就不须要在加参数,只须要把用户写入认证文件便可。
三、访问Apache服务
上面定义的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就能够正常访问
###########################################################
关注微信平台,了解最新动态