🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 隐藏nginx版本号的安全性和方法 [TOC]   搭建好nginx或者apache,为了安全起见我们都会隐藏他们的版本号,这边讲的是nginx的版本号,如果你也想隐藏apache的版本号,那请点前面的链接。请看nginx版本号信息隐藏文章。 ## 查看Nginx默认是显示版本号 ~~~ curl -I localhost HTTP/1.1 200 OK Server: nginx/1.2.9 Date: Wed, 30 Dec 2015 01:44:34 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 03 Nov 2015 14:31:43 GMT Connection: keep-alive Accept-Ranges: bytes ~~~   这样就给别人看到服务器nginx版本是1.2.9,前些时间暴出了一些Nginx版本漏洞,就是说有些版本有漏洞,而有些版本没有。这样暴露出来的版本号就容易变成攻击者可利用的信息。所以,从安全的角度来说,隐藏版本号会相对安全些!    ## 编辑nginx主配置文件   进入nginx配置文件的目录,在`http { }`段里加上`server_tokens off;` 如: ~~~ http { server_tokens off; include mime.types; default_type application/octet-stream; ... 省略其他 ... } ~~~ ## 编辑php-fpm配置文件   如fastcgi.conf或fcgi.conf(这个配置文件名也可以自定义的,根据具体文件名修改): 找到: `fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;` 改为: `fastcgi_param SERVER_SOFTWARE nginx;` ## 测试、重载nginx配置 ~~~ nginx -t # 得到如下结果: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful ## 重载配置 nginx -s reload ~~~ ## 查看修改后的结果 ### 修改前响应头信息如下 > Cache-control:private Connection:keep-alive Content-Type:text/html; charset=utf-8 Date:Wed, 30 Dec 2015 01:51:20 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Pragma:no-cache Server:**nginx/1.2.9** Transfer-Encoding:chunked X-Powered-By:ThinkPHP **修改前响应头效果如下图所示** ![](https://box.kancloud.cn/2015-12-30_56833fac1b246.png)    * * * * * ### 修改后响应头信息如下 > Cache-control:private Connection:keep-alive Content-Type:text/html; charset=utf-8 Date:Wed, 30 Dec 2015 01:53:11 GMT Expires:Thu, 19 Nov 1981 08:52:00 GMT Pragma:no-cache **Server:nginx** Transfer-Encoding:chunked X-Powered-By:ThinkPHP **修改后响应头效果如下图所示** ![](https://box.kancloud.cn/2015-12-30_56833fb6abc60.png)