本地測試 https://localhost/test.php 正常,但使用區域網 IP https://10.10.50.195/test.php 訪問時返回 403 Forbidden:
You don't have permission to access / on this server.
原因:Apache 預設配置僅允許本地回環地址(127.0.0.1)訪問,拒絕外部 IP。
Apache 使用 <Directory> 區塊控制路徑存取權限。預設配置通常包含:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Satisfy all
</Directory>
以及網站根目錄區塊:
<Directory "D:/Wamp5/www">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
</Directory>
Deny from all 拒絕所有來源,Allow from 127.0.0.1 僅放行本地回環,導致區域網 IP 被擋。
修改兩處 <Directory> 區塊,將 Deny from all 註釋或刪除,改為 Allow from all:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
# Deny from all
Allow from all
Satisfy all
</Directory>
<Directory "D:/Wamp5/www">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
# Deny from all
# Allow from 127.0.0.1
Allow from all
</Directory>
<Directory "D:/Wamp5/www">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Allow from 10.10.50.0/24 # 僅允許 10.10.50.x 網段
Allow from 192.168.1.0/24 # 也可加入其他內網段
</Directory>
Apache 2.4 棄用 Order/Deny/Allow,改用 Require:
<Directory "D:/Wamp5/www">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted # 允許所有
# Require ip 127.0.0.1 10.10.50.0/24 # 或限定 IP
</Directory>
注意:若配置檔混用舊新語法,需載入 mod_access_compat 模組,否則會衝突。
httpd.conf(通常在 Apache安裝目錄/conf/httpd.conf)。<Directory 找到上述兩個區塊。httpd -k restart 或服務管理器重啟)。AllowOverride None 會忽略 .htaccess,必須改 httpd.conf。<VirtualHost>,確保其內部 <Directory> 也放行。setsebool -P httpd_can_network_connect 1 或調整標籤。Allow from all,應結合 IP 白名單、VPN、基本認證或應用層權限控制。# Windows
httpd -t
# Linux
apachectl configtest
# 或
httpd -t
輸出 Syntax OK 表示配置無誤,可安全重啟。