核心檢查項
- 主配置是否包含 vhost 文件:
httpd.conf 中必須有 Include conf/extra/httpd-vhosts.conf 且未被註釋。
- NameVirtualHost 指令:Apache 2.4 不再需要,直接在 `` 中配置。
- ServerName / ServerAlias 匹配:確保請求的 Host 頭與某個 `` 的 `ServerName` 或 `ServerAlias` 精確匹配。
- DocumentRoot 路徑權限:目錄需存在,且 Apache 進程用戶有讀取權限(`` 中 `Require all granted`)。
- 配置生效:修改後必須
httpd -t 語法檢查通過,並 systemctl reload httpd 或重啟服務。
最小可用模板
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/var/www/example"
<Directory "/var/www/example">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "logs/example-error.log"
CustomLog "logs/example-access.log" common
</VirtualHost>
排查技巧
- 啟用
LogLevel debug 觀察錯誤日誌中虛擬主機匹配過程。
- 用
curl -H "Host: www.example.com" https://localhost/ 測試匹配邏輯。
- 檢查是否有多個 `` 定義導致順序搶佔(第一個匹配生效)。