2020年 · Linux

nginx 隐藏版本号与WEB服务器信息

nginx不仅可以隐藏版本信息,还支持自定义web服务器信息

先看看最终的隐藏结果吧

1 官网下载最新稳定版
wget http://nginx.org/download/nginx-1.18.0.tar.gz
2 解压
tar -xf nginx-1.18.0.tar.gz
cd nginx-1.18.0
3 修改C文件
(1)vim 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_full_string[] = “Server: ” NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = “Server: ” NGINX_VER_BUILD CRLF;

修改为:

static u_char ngx_http_server_string[] = “Server: unkown” CRLF;
static u_char ngx_http_server_full_string[] = “Server: unkown” CRLF;
static u_char ngx_http_server_build_string[] = “Server: unkown” CRLF;
(2)vim src/http/ngx_http_special_response.c      #修改36行
“<hr><center>unkown</center>” CRLF    #再写一遍刚才的字符串
4 编译安装
./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_stub_status_module –with-http_v2_module –with-http_ssl_module –with-http_gzip_static_module –with-http_realip_module –with-http_flv_module –with-http_mp4_module –with-openssl=../openssl-1.1.1c –with-pcre=../pcre-8.43 –with-pcre-jit –with-ld-opt=-ljemalloc
make && make install

5 修改nginx配置文件,http节点下添加 server_tokens off
vim /usr/local/nginx/conf/nginx.conf
….
http {
server_tokens off;
…..
6 重启NGINX后测试结果

说明:(1)要是只想隐藏版本号,而不想自定义服务器信息,不需要执行第3步.
     (2)要是对nginx升级同时还要做字符串自定义,也是没有问题的,可以先修改C文件–>./configure –> make 即可
参考:https://www.cnblogs.com/Sunzz/archive/2018/11/15/9962891.html
https://blog.csdn.net/honyer455/article/details/86491269