一,為什么要隱藏nginx真實的軟件名稱?
1,nginx響應的Server頭部都會攜帶上服務軟件的名字和版本信息,
服務器軟件的版本信息暴光在外部,很容易被黑客了解到,就通過相應版本的漏洞來攻擊服務器,引發安全問題。
針對生產環境的服務器,有必要隱藏或者修改軟件版本信息,以避免黑客的指向性攻擊.
2,看一下nginx作為web server返回的head信息
[root@centos8 ~]# curl --head http://127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.18.0 Date: Fri, 24 Apr 2020 06:51:51 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Wed, 22 Apr 2020 09:22:38 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "5ea00cde-264" Accept-Ranges: bytes
真實的軟件名稱和版本號都顯示出來了
3,在這個例子里,我們把nginx的軟件名稱改名為LWS
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,通過修改nginx源代碼文件來修改軟件名稱
1,復制一個文件夾,與無修改的源碼文件夾區分開
[root@centos8 source]# cp -axv nginx-1.18.0 nginx-1.18.0-LWS
然后我們基於新源碼文件夾做修改
2,修改第一個文件:核心的頭文件
[root@centos8 nginx-1.18.0-LWS]# vi src/core/nginx.h
把
#define NGINX_VER "nginx/" NGINX_VERSION
修改為:
#define NGINX_VER "lws/" NGINX_VERSION
把
#define NGINX_VAR "NGINX"
修改為
#define NGINX_VAR "LWS"
3,第二個文件:
[root@centos8 nginx-1.18.0-LWS]# vi src/http/ngx_http_header_filter_module.c
把
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
修改為:
static u_char ngx_http_server_string[] = "Server: lws" CRLF;
4,第三個文件:內置的響應信息:比如404之類的錯誤提示頁面
[root@centos8 nginx-1.18.0-LWS]# vi src/http/ngx_http_special_response.c
把
"<hr><center>nginx</center>" CRLF
修改為:
"<hr><center>lws</center>" CRLF
5,第四個文件:
[root@centos8 nginx-1.18.0-LWS]# vi src/http/v2/ngx_http_v2_filter_module.c
把第480行:
"http2 output header: \"server: nginx\"");
修改為:
"http2 output header: \"server: lws\"");
三,重新安裝nginx
configure
[root@centos8 nginx-1.18.0-LWS]# ./configure --prefix=/usr/local/soft/nginx-1.18.0 --with-http_stub_status_module --with-http_ssl_module
安裝
[root@centos8 nginx-1.18.0-LWS]# make && make install
四,隱藏nginx版本號
[root@centos8 conf]# vi nginx.conf
在http段內增加:
server_tokens off;
五,重啟服務並測試效果
重啟服務
[root@centos8 conf]# systemctl stop nginx
[root@centos8 conf]# systemctl start nginx
測試效果
[root@centos8 soft]# curl --head http://127.0.0.1 HTTP/1.1 200 OK Server: lws Date: Fri, 24 Apr 2020 07:34:38 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 24 Apr 2020 07:22:13 GMT Connection: keep-alive Vary: Accept-Encoding ETag: "5ea293a5-264" Accept-Ranges: bytes
也可以從瀏覽器端查看:
報錯頁面變成了lws

響應的頭信息變成了lws

六,服務端查看nginx的版本
[root@centos8 soft]# /usr/local/soft/nginx-1.18.0/sbin/nginx -v nginx version: lws/1.18.0
七,對修改nginx軟件名和版本號的建議:
1,我們不建議從源碼修改版本號,隱藏即可,
真實的版本號還是要保存下來后續的開發維護中參考
2,如果在生產環境中沒有修改nginx的軟件名稱,
那么版本號一定要做到隱藏
