分析
此前搭建的Nginx环境中,请求响应头包含特定字段。
[nginx@node01 sbin]$ curl -I 127.0.0.1:8090
HTTP/1.1 200 OK
Server: nginx/1.9.9
Date: Fri, 11 Nov 2026 14:56:38 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 11 Nov 2026 12:47:59 GMT
Connection: keep-alive
ETag: "636e447f-264"
Accept-Ranges: bytes
响应头中存在Server: nginx/1.9.9这一行,直接暴露了服务类型为Nginx及其具体版本号。若攻击者获知此信息,且该版本存在已知漏洞,便能轻易制定攻击方案,因此隐藏此类敏感信息对保障网站安全至关重要。
针对此安全隐患,Nginx提供了server_tokens配置项。将其置于http块中即可实现版本号的隐藏。
隐藏版本号
需编辑nginx.conf文件,加入server_tokens指令,具体配置如下。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
keepalive_timeout 65;
server {
listen 8090;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
完成配置后重启Nginx服务。
此时版本号已被成功隐藏。
[nginx@node01 sbin]$ ./nginx -s reload
[nginx@node01 sbin]$ curl -I 127.0.0.1:8090
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 11 Nov 2026 15:08:55 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 11 Nov 2026 12:47:59 GMT
Connection: keep-alive
ETag: "636e447f-264"
Accept-Ranges: bytes
php-fpm服务器隐藏版本号
若部署的是php-fpm服务器,还须修改fastcgi.conf配置文件。
该配置文件中存在如下行。
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
修改后
fastcgi_param SERVER_SOFTWARE nginx;
隐藏Server
上述修改仅隐藏了版本号,若希望彻底消除Server信息,必须修改源代码。
需修改C语言源文件src/http/ngx_http_header_filter_module.c。
定位至大约第50行附近,将默认的nginx名称替换为其他自定义名称。
//static char ngx_http_server_string[] = "Server: nginx" CRLF;
//static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static char ngx_http_server_string[] = "Server: juan" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
重新编译前务必停止Nginx运行,并备份现有的nginx.conf文件。
Nginx源码安装教程参考链接:https://www.jb51.net/article/142431.htm
编译完成后,用新构建的文件替换之前备份的文件,随后启动Nginx。
[nginx@node01 sbin]$ curl -I 127.0.0.1:8090
HTTP/1.1 200 OK
Server: juan
Date: Fri, 11 Nov 2026 15:34:27 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 11 Nov 2026 15:30:46 GMT
Connection: keep-alive
ETag: "636e6aa6-264"
Accept-Ranges: bytes
此时Server响应头已变更为自定义名称,至此Nginx的安全加固介绍完毕。
关于Nginx隐藏server头信息的实现文章到此结束,更多相关内容请搜索本站历史文章或继续浏览下方推荐内容,感谢支持。