預設下 Apache 與 PHP 會在 HTTP 回應標頭與錯誤頁面洩露版本資訊,攻擊者可據此鎖定已知漏洞進行攻擊。本文說明如何徹底隱藏版本資訊,提升伺服器安全性。
# httpd.conf 或 apache2.conf
ServerTokens Prod
ServerSignature Off
| 設定值 | 回應標頭範例 | 資訊洩露程度 |
|---|---|---|
Full (預設) | Server: Apache/2.4.57 (Ubuntu) PHP/8.2.1 OpenSSL/3.0.2 | ❌ 完整版本+模組 |
OS | Server: Apache/2.4.57 (Ubuntu) | ⚠️ 版本+作業系統 |
Minimal | Server: Apache/2.4.57 | ⚠️ 僅版本號 |
Minor | Server: Apache/2.4 | ⚠️ 主次版本 |
Major | Server: Apache/2 | ⚠️ 主版本 |
Prod / ProductOnly | Server: Apache | ✅ **最少資訊** |
Off:錯誤頁面不顯示伺服器資訊(推薦)On:顯示伺服器版本與虛擬主機名稱EMail:顯示版本+管理員 Email(`ServerAdmin` 指令); php.ini
expose_php = Off
| 設定 | X-Powered-By 標頭 | 錯誤頁面 PHP 版本 |
|---|---|---|
| expose_php = On (預設) | X-Powered-By: PHP/8.2.1 | 顯示 |
| expose_php = Off | (無此標頭) | 隱藏 |
需載入 `mod_headers` 模組:
# httpd.conf
LoadModule headers_module modules/mod_headers.so
# 隱藏或偽裝 Server 標頭
Header always unset Server
Header always set Server "MyWebServer/1.0"
# 也可移除 X-Powered-By(雙重保險)
Header always unset X-Powered-By
$ curl -I https://example.com/
HTTP/1.1 200 OK
Server: Apache
Content-Type: text/html; charset=UTF-8
# 無 X-Powered-By
# 無詳細版本號
# httpd.conf 或 .htaccess (需 AllowOverride Headers)
# 隱藏版本
ServerTokens Prod
ServerSignature Off
Header always unset X-Powered-By
# 安全標頭
Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';"
Header always set Permissions-Policy "geolocation=(), microphone=()"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
# nginx.conf
server {
# 隱藏 Nginx 版本
server_tokens off;
# 隱藏 PHP 版本 (fastcgi 傳遞)
fastcgi_hide_header X-Powered-By;
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# php.ini (同樣需要)
expose_php = Off
避免預設錯誤頁洩露路徑、資料庫錯誤等敏感資訊:
# httpd.conf
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
ErrorDocument 502 /errors/502.html
ErrorDocument 503 /errors/503.html
# 自訂錯誤頁面範例 (errors/500.html)
服務暫時不可用
500 - 內部伺服器錯誤
我們已記錄此錯誤,技術團隊正在處理中。
隱藏版本資訊屬於「深度防禦」的一環,非萬靈丹。核心防護仍是:及時修補漏洞、最小權限原則、WAF、入侵偵測、定期滲透測試。