一般使用Nginx的web網(wǎng)站,訪問網(wǎng)站時(shí),可以從請(qǐng)求頭中看到使用了nginx以及nginx的版本號(hào)。暴露這些信息將給網(wǎng)站帶來一定的風(fēng)險(xiǎn),因此安裝nginx時(shí)最好把這些信息隱藏。

隱藏nginx 版本號(hào)與WEB服務(wù)器信息
如果未安裝nginx,先去官網(wǎng)下載壓縮包并解壓到指定目錄,然后進(jìn)入nginx安裝目錄
1、 修改src/http文件夾下的兩個(gè)文件,具體見下圖

(1)修改src/http/ngx_http_header_filter_module.c文件
vim src/http/ngx_http_header_filter_module.c
#大約在49行, 修改Server后面服務(wù)器信息為自定義服務(wù)器信息
static u_char ngx_http_server_string[] = "Server: listen/1.1" CRLF;

改成自定義名稱:

(2)修改src/http/ngx_http_special_response.c文件
vim src/http/ngx_http_special_response.c
#大約在36行,修改為自定義服務(wù)器信息
"<hr><center>listen/1.1</center>" CRLF

改成自定義名稱

2、編譯配置(如果已經(jīng)安裝過nginx,這一步需要先通過nginx –V查看安裝信息,然后把相關(guān)配置加入下面配置中)
./configure --prefix=/usr/local/src/nginx
3、編譯安裝(已經(jīng)安裝過的這步只執(zhí)行make,不執(zhí)行make install,不然會(huì)把以前的配置覆蓋,編譯后直接手動(dòng)復(fù)制nginx執(zhí)行文件到原安裝目錄)
make && make install
4、修改nginx配置文件,http節(jié)點(diǎn)下添加 server_tokens off
vim /usr/local/nginx/conf/nginx.conf
....
http {
server_tokens off;
.....

5、啟動(dòng)nginx(正在運(yùn)行的,需要?dú)⒌暨M(jìn)程再重新啟動(dòng))
/usr/local/src/nginx/sbin/nginx
6、測(cè)試
[root@node1 nginx]# sudo curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: listen/1.1
Date: Fri, 16 Oct 2020 10:49:48 GMT
Content-Type: text/html;charset=ISO-8859-1
Connection: keep-alive
Vary: Accept-Encoding
瀏覽器訪問如下,看不出網(wǎng)站使用了nginx。

主要:如果只想隱藏版本號(hào),而不想自定義服務(wù)器信息,則不需要修改src/http下的ngx_http_header_filter_module.c和ngx_http_special_response.c文件