11.26 访问控制Directory

时间:2022-04-27
本文章向大家介绍11.26 访问控制Directory,主要内容包括访问控制 – Directory目录概要、访问控制、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

访问控制 – Directory目录概要

  • 核心配置文件内容
  <Directory /data/wwwroot/111.com/admin/>
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </Directory>
  • curl测试状态码为403则被限制访问了

访问控制

  1. 打开虚拟主机配置文件
  • Order,用来定义顺序,是先deny,还是allow
    • 若是先deny,就先执行deny的语句
    • 若是先allow,就先执行allow的语句
    • 特殊性:
      • 不管IP是否匹配到,它都会从头到尾执行完
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

将代码放在防盗链代码上面,防止冲突
  <Directory /data/wwwroot/111.com/admin/>
        Order deny,allow
        Deny from all
        Allow from 127.0.0.1
    </Directory>
  1. 创建一个admin目录,并新建一个index.php文件,内容为121212
[root@hf-01 ~]# cd /data/wwwroot/111.com
[root@hf-01 111.com]# ls
11.png  123.php  
[root@hf-01 111.com]# mkdir admin/
[root@hf-01 111.com]# cd admin/
[root@hf-01 admin]# ls
[root@hf-01 admin]# touch index.php
[root@hf-01 admin]# echo "121212" > index.php
[root@hf-01 admin]# ls
index.php
[root@hf-01 admin]# cat index.php
121212
[root@hf-01 admin]# cd ..
[root@hf-01 111.com]# 
  1. 检查语法错误,并重新加载配置文件
[root@hf-01 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 111.com]# 
  1. 限制的原 IP
  • curl -x127.0.0.1:80 111.com/admin/index.php -I
    • 127.0.0.1是目标 IP ,而要访问的IP,也要使用127.0.0.1去访问,最终就是目标IP和原IP是同一个IP,自己和自己通信,限制IP是 原 IP
    • -x指定的是目标IP
[root@hf-01 111.com]# curl -x127.0.0.1:80 111.com/admin/index.php -I
HTTP/1.1 200 OK
Date: Mon, 25 Dec 2017 23:42:01 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
X-Powered-By: PHP/5.6.30
Content-Type: text/html; charset=UTF-8

[root@hf-01 111.com]# 
  1. 在更换目标IP,那么原IP也会跟着变化
[root@hf-01 111.com]# curl -x192.168.74.150:80 111.com/admin/index.php -I
HTTP/1.1 403 Forbidden
Date: Mon, 25 Dec 2017 23:47:06 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Content-Type: text/html; charset=iso-8859-1

[root@hf-01 111.com]# 
  1. 浏览器访问111.com/admin会显示forbidden
  • 访问控制是用目录的形式来做的,首先规定一个目录访问到哪里去的(目录必须使用绝对路径),然后是Oerder,控制的对象就是来源IP