windows下nginx1.20.1隐藏HTTP 请求头文件中
简介
本文主要是隐藏HTTP 请求头文件中的Server信息
解决问题主要分下面几步
一、准备编译所需要的环境
二、修改nginx源码改变配置
三、编译nginx源码生成新的nginx.exe
四、替换nginx.exe,重新启动nginx
1. 准备所需环境
环境都需要准备好,因为编译是都需要用到
1.1 MSYS

打开cmd,输入gcc --version 测试是否成功

1.2 Strawberry perl
下载地址:https://strawberryperl.com
1.3 NASM
下载地址:https://www.nasm.us/pub/nasm/releasebuilds/?C=M;O=D
(1)配置环境变量
将安装路径配置到path

(2)打开cmd,输入nasm -v 测试是否成功

1.4 Visual Studio
能够打开命令提示行,且有nmake.exe cl.exe的版本即可
1.5 Mercurial
其实就是TortoiseHg,下载安装即可,主要是用来下载nginx的源码包
下载地址:https://www.mercurial-scm.org

打开cmd,输入hg 测试是否成功

1.6 下载nginx源码包
1.6.1 下载
方法一:打开cmd进入想要检出到的目录,执行下面的命令检出代码
hg clone http://hg.nginx.org/nginx


方法二:进入官网下载
选择自己想要下载的版本即可

1.6.2 修改文件
(1)修改 makefile.msvc 文件
路径:nginx\auto\lib\openssl\makefile.msvc
将ms\do_ms改成ms\do_nasm

(2)修改ngx_http_header_filter_module.c
路径:nginx\src\http\ngx_http_header_filter_module.c
将
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_string[] = "Server: Please guess it!" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " CRLF;

(3)修改 ngx_http_special_response.c
路径:nginx\src\http\ngx_http_special_response.c
将36行的nginx改成Please guess it!,这个可以自定义

1.7 编译环境依赖包下载
依赖包下载后直接解压到指定目录
(1)在nginx源码包目录下新建objs.msvc8目录,在objs.msvc8目录下再新建lib目录用于存放下面的依赖包

(2) 将下载的包解压后放到nginx源码包lib目录

1.7.2 OpenSSL
openssl-1.1之后的版本,目录结构及文件有了较多改变
下载地址:https://www.openssl.org/source/old
1.7.1 pcre
官网地址:http://www.pcre.org
其他下载地址:https://sourceforge.net/projects/pcre/files
1.7.3 zlib
下载地址:http://zlib.net
2. 重新编译源码生成nginx.exe
2.1 生成 Makefile 文件
2.1.1 取出 configure arguments 的相关信息
进入nginx的目录(不是源码路径)使用nginx -V取出configure arguments 后面的信息

2.1.2 开始生成Makefile文件
(1)进入MSYS的安装路径,找到msys2.exe文件,双击打开

(2)进入nginx源码目录
cd D:/Software/nginx/nginxSourceCode/nginx

(3)开始生成Makefile文件(执行下面语句,记得改成自己的路径),这个过程需要等待几分钟
auto/configure --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-8.44 --with-zlib=objs.msvc8/lib/zlib-1.2.11 --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-openssl=objs.msvc8/lib/openssl-1.1.1k --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

2.2 编译Makefile文件
打开 Visual Studio 的命令提示行,我用的是 Visual Studio 2019
(1)进入nginx源码目录
cd D:\Software\nginx\nginxSourceCode\nginx

(2)开始编译,这个过程需要等待几分钟
nmake -f build/Makefile

(3)编译成功后生成一个nginx.exe

(4)将新生成的nginx.exe替换原来的nginx.exe即可

3. 测试
重新启动nginx,在浏览器访问127.0.0.1,或者其它由nginx代理的地址,可以看到Server的信息已经没有了
