背景介绍

Nginx 有Windows下打包好的版本,但是在做SNI代理的时候,缺少了stream_ssl_preread_module模块。所以需要重新编译Nginx。

准备工作

工具下载

  • Visual Studio 2015以上版本都可以。
  • MSYS – 默认安装就可以。 – 安装完成后,双击打开bin/mingw-get.exe 如下图步骤,依次勾选mingw-devloper-toolkit / mingw32-base / mingw32-gcc-g++ / msys-base
  • windows版nginx添加模块 windows版本nginx添加模块_nginx

  • – 如下图,点击”Apply Changes“进行安装
  • windows版nginx添加模块 windows版本nginx添加模块_运维_02

  • Mercurial – 默认安装就可以
  • Strawberry Perl – 默认安装就可以

Nginx 依赖包下载

  • openssl-1.1.1s
  • pcre2-10.35
  • zlib-1.2.13
  • 这里下载的都是tar.gz的格式,这个格式可以用7-Zip进行解压

Nginx 源码包

打开cmd窗口,执行下面的命令,获取Nginx源码包

# 下载的文件存放在cmd界面中的目录位置下。
# 整理默认获取最新的源码包
hg clone http://hg.nginx.org/nginx

编译目录准备

  1. 在Nginx目录下,创建build目录。这个目录存放编译的相关文件和目录。
  2. 在build目录下,创建lib目录。将Nginx依赖包解压后放置到这个目录下。
  3. 这个windows设备是64位的,所以在创建MakeFile前,要将Openssl模块的配置修改为x64。可以在cmd窗口中执行
# 这两句命令 到nginx/build/lib/openssl-1.0.1s目录下,找到INSTALL.W64文件,用编辑器打开后,就可以看到
perl Configure VC-WIN64A
ms\do_win64a

编译Nginx

创建MakeFile文件

  1. 打开VS2017命令行x86 Native Tools Command Prompt for VS 2017
  2. 打开的命令行界面中启动msys.bat,如下图
  3. 打开的MINGW32界面中,由于它是模拟linux环境的,所以刚进入的目录是/home/下的。要需要通过cd命令,然后进入到Nginx目录下。
  4. 在这个界面中可以按照下面的内容进行编译,也可以按照各自的需求添加删除模块
auto/configure \
--with-cc=cl \
--builddir=build \
--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=build/lib/pcre2-10.35 \
--with-zlib=build/lib/zlib-1.2.13 \
--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=build/lib/openssl-1.1.1s \
--with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' \
--with-http_ssl_module \
--with-mail_ssl_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module

编译完成后,在build目录下就会产生许多文件,Makefile文件也在其中。

编译

  1. 我们切换到x86 Native Tools Command Prompt for VS 2017界面中
  2. cd到Nginx目录下。切记:这是是Nginx目录,不是build目录。
  3. 执行下面的命令,完成编译。
nmake -f build/Makefile

编译完成后,会在bulid目录下产生nginx.exe文件

创建整体Nginx

  1. 重新创建个文件目录,取名nginx
  2. 将nginx.exe拷贝到这个目录下,同时将nginx源码包中的目录(conf,contrib,docs也拷贝到这个目录下)
  3. 创建temp,logs,html目录