天天看点

配置网页内容访问

3案例3:配置网页内容访问

3.1 问题

本例要求在web网站http://server0.example.com的DocumentRoot目录下创建一个名为private的子目录,要求如下:

  1. 从http://classroom/pub/materials/private.html下载一个文件副本到这个目录,重命名为index.html
  2. 不要对文件index.html的内容作任何修改
  3. 从server0上,任何人都可以浏览private的内容,但是从其他系统不能访问这个目录的内容
3.2 方案

配置web内容的访问控制需要添加Directory区段,主要形式可参考

<Directory  "父目录路径">
     Require  all  denied                                  //上层目录拒绝任何访问
</Directory>
<Directory  "子目录1路径">
     Require  all  granted                             //子目录1允许任何访问
</Directory>
<Directory  "子目录2路径">
     Require  ip  IP或网段地址 .. ..                     //子目录2允许少数客户机
</Directory>
           
3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:部署网页子目录及文档

1)建立子目录

[[email protected] ~]# mkdir  /var/www/html/private
           

2)部署网页

[[email protected] ~]# cd /var/www/html/private
[[email protected] private]# wget  http://classroom/pub/materials/private.html  -O  index.html
.. ..
2016-11-26 20:30:28 (1.90 MB/s) - ‘index.html’ saved [14/14]
[[email protected] private]# cat  index.html                  //检查网页文件
Private Site.
           

步骤二:为指定的网页子目录限制访问

在httpd服务的标准配置中,根目录/默认拒绝任何访问,但网页目录/var/www/默认允许任何访问。因此,只需要为个别子目录增加访问控制即可。

1)调整虚拟站点server0.example.com的配置文件

[[email protected] ~]# vim  /etc/httpd/conf.d/00-default.conf 
.. ..
<Directory  "/var/www/html/private">
        Require  ip  127.0.0.1  ::1  172.25.0.11
</Directory>
           

2)重启系统服务httpd

[[email protected] ~]# systemctl  restart  httpd
           

步骤三:测试目录访问限制

1)从desktop0上访问http://server0.example.com/private/被拒绝

[[email protected] ~]# elinks  -dump  http://server0.example.com/private/
                                   Forbidden
   You don't have permission to access /private/ on this server.
           

2)从desktop0上访问http://server0.exmaple.com/仍然是正常的

[[email protected] ~]# elinks  -dump  http://server0.example.com/
   Default Site.
           

3)从server0本机上访问http://server0.example.com/private/也不受限制

[[email protected] ~]# elinks  -dump  http://server0.example.com/private/
   Private Site.
           

继续阅读