在 Windows Server 2008 上部署 WampServer 3.0.4 後,預設僅允許本機訪問(localhost/127.0.0.1),遠程 IP 請求直接返回 403 Forbidden。根因在於 Apache 2.4 將訪問控制指令從 2.2 的 Order Allow,Deny 完全重寫為 Require 語法。本文給出最小改動修復方案並解釋核心邏輯。
https://localhost/ 正常https:/// 返回 403 ForbiddenAH01630: client denied by server configuration<Directory "D:/wamp64/www/">
Options -Indexes +FollowSymLinks
AllowOverride All
# 核心修改:允許所有來源訪問
Require all granted
# 預設僅允許本機(需註釋或移除)
# Require local
</Directory>
| 指令 | 含義 | 建議設置 |
|---|---|---|
Options -Indexes | 禁用目錄瀏覽(防止敏感文件列表洩露) | 生產環境必須開啟 |
Options +FollowSymLinks | 允許跟隨符號鏈接 | 框架路由/靜態資源常需開啟 |
AllowOverride All | 允許 .htaccess 覆蓋所有指令 | Laravel/ThinkPHP/Symfony 必需 |
Require all granted | Apache 2.4 新語法:授權所有 IP 訪問 | 解決遠程訪問 403 的關鍵 |
Require local | 僅允許 localhost/127.0.0.1/::1 訪問 | 預設安全策略,遠程部署需移除 |
# Apache 2.2 (舊語法,2.4 兼容但不推薦)
Order Allow,Deny
Allow from all
# Apache 2.4 (標準語法)
Require all granted
# 2.2 限制特定 IP
Order Deny,Allow
Deny from all
Allow from 192.168.1.0/24
# 2.4 等價寫法
Require ip 192.168.1.0/24
# 2.4 組合邏輯
<RequireAll>
Require ip 10.0.0.0/8
Require not ip 10.1.1.1
</RequireAll>
Require all granted 設置在根目錄,建議針對具體項目目錄配置<Directory "D:/wamp64/www/myapp/">
Require ip 192.168.1.0/24 10.0.0.0/8
</Directory>
ServerTokens Prod ServerSignature Off
httpd.conf 保存curl -I https:/// 應返回 HTTP/1.1 200 OKhttpd-vhosts.conf 虛擬主機配置而未修改主 httpd.conf 的 Directory 默認策略Require local 和 Require all granted → RequireAll 邏輯導致仍拒絕遠程AllowOverride All 導致框架路由/偽靜態失效Apache 2.4 的 Require 模組化授權體系雖學習成本稍高,但表達能力遠超 2.2。掌握核心指令組合,既能快速解決遠程訪問問題,又能構建精細的訪問控制策略,是 Windows 下 PHP 部署必備技能。