如何Nginx隐藏版本号信息

2,603次阅读

共计 877 个字符,预计需要花费 3 分钟才能阅读完成。

当我们通过 yum 或者其他包管理工具安装完 Nginx 之后,访问页面时 Header 里面会携带 Nginx 的版本号信息

$ curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.16.1  # 这里就是 Nginx 版本号
Date: Fri, 23 Aug 2019 08:54:07 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 15:04:31 GMT
Connection: keep-alive
ETag: "5d52d17f-264"
Accept-Ranges: bytes

这是一个小小的安全隐患,所以这个时候我们需要进行版本号的隐藏,打开 nginx 的配置文件,然后在 http 段中添加以下配置

$ sudo vi /etc/nginx/nginx.conf
http {
    ......
    server_tokens off;
    ......
}

保存之后我们对 nginx 进行 reload 让其生效配置

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo nginx -s reload

然后再次通过 curl 指令进行访问

$ curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx  # Nginx 版本号已隐藏
Date: Fri, 23 Aug 2019 09:02:18 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Aug 2019 15:04:31 GMT
Connection: keep-alive
ETag: "5d52d17f-264"
Accept-Ranges: bytes

此时发现 Nginx 的版本号已经完全隐藏了。

参考文献

正文完
 
Blood.Cold
版权声明:本站原创文章,由 Blood.Cold 2019-08-25发表,共计877字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。