Nginx软件名更改

为什么要隐藏Nginx真实的软件名称

Nginx响应的Server头部都会携带上服务软件的 名字 和 版本信息

服务器软件的版本信息暴光在外部,很容易被黑客了解到,就通过相应版本的漏洞来攻击服务器,引发安全问题

针对生产环境的服务器,有必要隐藏或者修改软件版本信息,以避免黑客的指向性攻击

看一下 Nginx 作为Web Server返回的 head信息

[root@VM-8-17-centos ~]# curl --head http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.18.1
Date: Mon, 03 Aug 2020 02:26:34 GMT
Content-Type: text/html
Content-Length: 1326
Last-Modified: Wed, 26 Apr 2017 08:03:47 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "59005463-52e"
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=31536000
Accept-Ranges: bytes

我们可以看到nginx真实的软件名称和版本号都显示出来了

在这个例子里,我们把 Nginx 1.18.0 的软件名称改为 Sajuna

通过修改nginx源代码文件来修改软件名称

[scode type="green"]建议修改前为您的硬盘创建快照[/scode]

进入终端执行以下命令

cd /www/server/nginx/src/src
sed -i "s#\"NGINX\"#\"SAJUNA\"#" core/nginx.h
sed -i "s#\"nginx/\"#\"Sajuna/\"#" core/nginx.h
sed -i "s#Server: nginx#Server: Sajuna#" http/ngx_http_header_filter_module.c
sed -i "s#\"<hr><center>nginx<\/center>\"#\"<hr><center>Sajuna<\/center>\"#" http/ngx_http_special_response.c
sed -i "s#server: nginx#server: Sajuna#" http/v2/ngx_http_v2_filter_module.c

当然,您也可以选择手动更改

修改第一个核心的头文件 src/core/nginx.h

#define nginx_version      1018000
#define NGINX_VERSION      "1.18.0"
#define NGINX_VER          "nginx/" NGINX_VERSION

#ifdef NGX_BUILD
#define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD    NGINX_VER
#endif

#define NGINX_VAR          "NGINX"
#define NGX_OLDPID_EXT     ".oldbin"

修改为 (tip:修改了2处,版本号可修改)

#define nginx_version      1018000
#define NGINX_VERSION      "1.18.0"
#define NGINX_VER          "Sajuna/" NGINX_VERSION

#ifdef NGX_BUILD
#define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD    NGINX_VER
#endif

#define NGINX_VAR          "Sajuna"
#define NGX_OLDPID_EXT     ".oldbin"

修改第二个文件src/http/ngx_http_header_filter_module.c

把49行

static u_char ngx_http_server_string[] = "Server: nginx" CRLF;

修改为

static u_char ngx_http_server_string[] = "Server: Sajuna" CRLF;

修改第三个文件,它包含了内置的响应信息:比如404之类的错误提示页面 src/http/ngx_http_special_response.c

把36行

"<hr><center>nginx</center>" CRLF

修改为

"<hr><center>Sajuna</center>" CRLF

修改第四个文件 src/http/v2/ngx_http_v2_filter_module.c

把480行

"http2 output header: \"server: nginx\"");

修改为

"http2 output header: \"server: Sajuna\"");

然后 重新编译

隐藏Nginx版本号

在Nginx配置中的 http字段 添加,宝塔面板无需添加

server_tokens off;

重启服务器查看效果

service nginx restart
[root@VM-8-17-centos ~]# curl --head http://127.0.0.1
HTTP/1.1 200 OK
Server: Sajuna
Date: Mon, 03 Aug 2020 09:16:03 GMT
Content-Type: text/html
Content-Length: 1326
Last-Modified: Wed, 26 Apr 2017 08:03:47 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "59005463-52e"
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=31536000
Accept-Ranges: bytes

文章来源:

Author:朱茱
link:https://www.isajuna.com/archives/15/