Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Apache 禁止目錄瀏覽:移除 Indexes 選項完整操作指南

編輯:關於伺服器

風險場景

目錄無索引文件(index.html、index.php 等)時,Apache 默認返回文件列表,洩露目錄結構、源碼文件、配置文件、備份文件等敏感信息。

核心配置:Options 指令控制

方法一:移除 Indexes 關鍵字(推薦)


# 原配置

    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all


# 修改為:刪除 Indexes

    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all

方法二:显式禁用 -Indexes(等效)



    Options -Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all

+ 表示啟用,- 表示禁用。-Indexes 明確語義更清晰。

方法三:.htaccess 局部控制(需 AllowOverride Options)


# 站點根目錄 .htaccess
Options -Indexes

適用於虛擬主機、無主配置權限場景。

驗證生效

  1. 創建測試目錄 mkdir /var/www/html/testdir
  2. 不放置索引文件
  3. 瀏覽器訪問 https://server/testdir/
  4. 應返回 403 Forbidden 或自定義錯誤頁,而非文件列表

完整安全配置模板



    # 核心:禁用目錄瀏覽、啟用符號鏈接跟蹤
    Options -Indexes FollowSymLinks
    
    # 禁止 .htaccess 覆蓋(性能、安全),特定目錄再開放
    AllowOverride None
    
    # 訪問控制:Apache 2.4 語法
    Require all granted
    # Apache 2.2 語法
    # Order allow,deny
    # Allow from all
    
    # 禁用服務器簽名
    ServerSignature Off
    
    # 禁用 TRACE 方法
    TraceEnable Off


# 特定應用目錄允許 .htaccess

    AllowOverride FileInfo AuthConfig Limit

擴展:自定義錯誤頁面


# 隱藏 Apache 版本信息
ServerTokens Prod
ServerSignature Off

# 自定義 403 頁面
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

常見誤區

誤區後果正確理解
以為 IndexIgnore * 可禁用列表僅隱藏文件,目錄仍可列表Options -Indexes 才是禁用目錄索引
修改後未重啟 Apache配置不生效apachectl graceful 或 systemctl reload httpd
虛擬主機配置未繼承主配置主配置禁用,虛擬主機仍列表虛擬主機 <Directory> 需同步設置

總結

Apache 目錄瀏覽洩露風險極高。在 <Directory> 中設置 Options -Indexes 或移除 Indexes,重啟 Apache 即可徹底禁用。配合 ServerTokens Prod、ServerSignature Off、自定義錯誤頁,構建最小信息暴露原則防線。

Copyright © Windows教程網 All Rights Reserved