使用Apache作為Web服務器的時候,在當前目錄下沒有index.html|php
等入口就會顯示目錄。讓目錄暴露在外面是非常危險的事。
找到Apache的配置文件 /etc/apache2/apache2.conf或在網站根目錄下建立.htaccess文件
改為:
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
修改為:
<Directory /var/www/> Options FollowSymLinks AllowOverride None Require all granted </Directory>
其實就是將Indexes去掉,Indexes表示若當前目錄沒有index.html就會顯示目錄結構。
禁止訪問某些文件/目錄
通過增加Files選項來控制,比如不允許訪問 .inc 擴展名的文件,保護php類庫:
<Files ~ ".inc$"> Order allow,deny Deny from all </Files>
再比如禁止訪問圖片
<FilesMatch .(?i:gif|jpeg|png)$> Order allow,deny Deny from all </FilesMatch>
禁止訪問/var/www
目錄下的滿足該正則的目錄
<Directory ~ "^/var/www/(.+/)*[0-9]{3}"> Order allow,deny Deny from all </Directory>