HTTP 響應頭中的 Server 與 X-Powered-By 會洩露軟體版本,攻擊者可據此針對已知漏洞發起攻擊。隱藏版本號是基礎加固措施之一。
編輯 nginx.conf 的 http 區塊,加入或修改:
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; # 關鍵:關閉版本號顯示
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
# ... 其他配置
}
編輯 php.ini,找到 expose_php 項並設為 Off:
expose_php = Off
# PHP-FPM
/usr/local/php/sbin/php-fpm restart
# Nginx 平滑重載
kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
使用 curl -I https://your-domain.com 檢查響應頭,確認不再包含 Server: nginx/x.x.x 與 X-Powered-By: PHP/x.x.x。