全部学科
NodeJS全栈
nodejs
Python全栈
python
小程序首页
📅 2026-05-20 5 分钟 ✍️ juanwangdev

隐藏Nginx版本号

默认情况下 NGINX 会在响应中暴露版本号,攻击者可根据版本号查找已知漏洞进行针对性攻击。隐藏版本号是最基本的安全加固措施。

基础隐藏

server_tokens 指令

nginx
http {
    server_tokens off;
    
    server {
        listen 80;
        server_name example.com;
        # ...
    }
}

server_tokens off 的效果:

  • 响应头 Server: nginx(不带版本号)
  • 错误页面不再显示 NGINX 版本

server_tokens 可放在 httpserverlocation 块。建议放在 http 块全局生效。

深度隐藏

清除 X-Powered-By 等头

后端框架可能自动添加技术栈标识:

nginx
server {
    listen 80;
    server_name example.com;
    
    server_tokens off;
    
    # 清除后端框架标识
    proxy_hide_header X-Powered-By;
    proxy_hide_header X-AspNet-Version;
    proxy_hide_header X-AspNetMvc-Version;
    
    location / {
        proxy_pass http://backend;
    }
}

自定义 Server 头(需源码编译)

完全隐藏或伪造 Server 头需要重新编译 NGINX:

Bash
# 修改源码 src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: WebServer" CRLF;

生产环境不建议修改源码,使用 server_tokens off 已足够。安全不应依赖于"隐蔽性"。

验证配置

配置生效后验证:

Bash
# 检查响应头
curl -I http://example.com

# 期望输出
HTTP/1.1 200 OK
Server: nginx
# 不应出现 Server: nginx/1.24.0 这样的格式

# 检查错误页面
curl http://example.com/nonexistent
# 错误页面不应显示 nginx 版本号

与 fastcgi 配合

FastCGI 后端也需隐藏版本:

nginx
location ~ \.php$ {
    fastcgi_pass unix:/run/php/php-fpm.sock;
    fastcgi_param PHP_ADMIN_VALUE "expose_php = Off";
}

或在 php.ini 中设置:

ini
expose_php = Off

要点总结

  • server_tokens off 是最基本的隐藏版本号方式
  • 同时清除后端的 X-Powered-By 等标识头
  • 错误页面和响应头都需要检查确认
  • PHP 的 expose_php = Off 防止 PHP 版本泄露
  • 隐藏版本号属于"纵深防御"策略的一层,不能替代其他安全措施

📝 发现内容有误?点击此处直接编辑

← 上一篇 跨域资源共享(CORS)安全配置
下一篇 → Nginx缓存机制(proxy_cache)
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库