在Nginx中隐藏版本号和服务器信息可以增强安全性,防止攻击者利用特定版本漏洞进行攻击。以下是几种有效的解决方案:
http {
server_tokens off;
}
这个设置会移除响应头中的Server
字段的Nginx版本信息。
src/http/ngx_http_header_filter_module.c
文件static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_string[] = "Server: Web Server" CRLF;
fastcgi_hide_header X-Powered-By;
在php.ini中设置:
expose_php = Off
使用curl命令验证:
curl -I http://yourdomain.com
理想响应头部应类似:
HTTP/1.1 200 OK
Server: Web Server
Date: Mon, 01 Jan 2023 00:00:00 GMT
Content-Type: text/html
location / {
limit_except GET POST HEAD {
deny all;
}
}
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
通过以上方法,可以显著减少服务器信息的暴露,提高网站安全性。