Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Apache 禁止列出網站目錄:去掉 Indexes 與目錄訪問控制實例

Apache 默認行為是:當前目錄沒有 index.html 就把根目錄結構直接列出來。這其實很危險——數據庫配置文件洩露、後台隱藏頁面暴露,都可能由此開始。

比如訪問某站根目錄 https://mimvp.com 就會看到文件列表:

Apache 禁止列出網站目錄:去掉 Indexes 與目錄訪問控制實例

下面說明如何禁止這種行為。

去掉 Indexes

編輯配置:

vim /etc/httpd/conf/httpd.conf

找到:

Options Indexes FollowSymLinks

改為:

Options FollowSymLinks

修改後大致如下:

<Directory "/var/www/html">
# Options None
# Options Indexes FollowSymLinks
Options FollowSymLinks

AllowOverride All
Order allow,deny
Allow from all
</Directory>

關鍵就是把 Indexes 去掉——正是它讓 Apache 在缺少 index.html 時展示目錄結構。改完重啟服務:

/etc/init.d/httpd restart

再訪問站點就不會列出目錄了。

1. 禁止訪問特定文件或目錄

用 Files 控制,例如保護 .inc 類庫文件:

<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>

禁止訪問指定目錄:

<Directory "/var/www/private">
Order allow,deny
Deny from all
</Directory>

目錄也可以用 DirectoryMatch 做正則匹配。

按文件類型匹配,例如禁止訪問所有圖片:

<FilesMatch "\.(gif|jpe?g|png)$">
Order allow,deny
Deny from all
</FilesMatch>

按 URL 相對路徑禁止:

<Location "/secret">
Order allow,deny
Deny from all
</Location>

配置示例

<Directory "/usr/local/apache2/htdocs">
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
# 就是這一行,只去掉 indexes 也可以
#Options Indexes FollowSymLinks
Options FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
AllowOverride None
# Controls who can get stuff from this server.
Order allow,deny
Allow from all
</Directory>

建議默認就關掉目錄瀏覽,需要展示列表時再針對單個目錄開 Indexes。

Copyright © Linux教程網 All Rights Reserved