從 Apache 2.2 升級到 2.4 後,存取控制語法全面改用 mod_authz_host 的 Require 指令,舊版的 Order allow,deny / Allow from all 寫法將不再生效,導致虛擬主機或目錄出現 403 Forbidden。
預設配置 httpd.conf 或 httpd-vhosts.conf 中的 <Directory> 區塊若仍為:
<Directory "D:/www">
Require all denied
</Directory>
或是缺少授權指令,Apache 會拒絕所有請求。
在對應的 <Directory> 區塊中設定:
<Directory "D:/www">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
| 需求 | 指令範例 |
|---|---|
| 允許所有 IP | Require all granted |
| 僅允許特定 IP/網段 | Require ip 192.168.1.0/24 10.0.0.1 |
| 僅允許特定主機名 | Require host example.com |
| 組合條件 (全部滿足) | <RequireAll> Require ip 192.168.1.0/24 Require user admin </RequireAll> |
| 組合條件 (任一滿足) | <RequireAny> Require ip 192.168.1.0/24 Require user admin </RequireAny> |
httpd.conf,搜尋 <Directory "${INSTALL_DIR}/www/"> 區塊修改。apache/conf/httpd.conf 或 apache/conf/extra/httpd-vhosts.conf 中的虛擬主機 <Directory> 區塊。logs/error.log 確認是否為其他模組(如 mod_security、mod_evasive)攔截。只需將舊版 Order/Deny/Allow 替換為 Require all granted,即可解決絕大多數 Apache 2.4 遷移後的權限問題。