3案例3:配置網頁内容通路
3.1 問題
本例要求在web網站http://server0.example.com的DocumentRoot目錄下建立一個名為private的子目錄,要求如下:
- 從http://classroom/pub/materials/private.html下載下傳一個檔案副本到這個目錄,重命名為index.html
- 不要對檔案index.html的内容作任何修改
- 從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.