前言
Nginx 在 Windows 下还是很实用的,可以作为前端的 HTTP 部署测试工具,或者当然可以用 Python 自带的 http.server 来部署,都挺快的。除了部署前端项目之外,放一些自制的基于 Web 的小工具都是极好的。
下载源码包
Nginx 在 Windows 下编译的源码与 Linux 下的不同,直接从官网的 download 里面下载是编译不过的,需要从下面的 Mercurial 链接下载,Mercurial 也是一种类似于 Git 的版本管理工具
Nginx 源码
官方hg仓库
http://hg.nginx.org/nginx
官方github mirror
https://github.com/nginx/nginx/tags
依赖工具
MSYS 2
msys2 在编译阶段仅用作配置 Makefile,因本文介绍的是在 MSVC 环境下的编译,所以不用 msys2 的 GNU 环境进行编译。
下载地址:https://www.msys2.org/
StrawberryPerl
因为需要编译 OpenSSL, 需要使用 perl 进行配置
https://strawberryperl.com/
sed
编译过程需要
windows 下载地址
https://sourceforge.net/projects/gnuwin32/files/sed/
** Tip: Git 的 Bash 环境也可以用作后面开始编译的配置 Makefile**
依赖源码
正式开始
打开MSYS 2进入到nginx源码包所在目录
下载依赖源码
wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.42/pcre2-10.39.tar.gz
wget https://zlib.net/zlib-1.3.tar.gz
wget https://www.openssl.org/source/old/3.0/openssl-3.0.10.tar.gz
这里以nginx-release-1.25.3.tar.gz 为例
解压nginx源码
tar xf nginx-release-1.25.3.tar.gz
cd nginx-release-1.25.3
创建临时目录
mkdir -p objs/lib
mkdir logs
mkdir -p temp/client_body_temp
mkdir -p temp/proxy_temp
mkdir -p temp/fastcgi_temp
mkdir -p temp/scgi_temp
在objs/lib 目录中解压依赖源码
cd objs/lib
tar -xzf ../../../pcre2-10.39.tar.gz
tar -xzf ../../../zlib-1.3.tar.gz
tar -xzf ../../../openssl-3.0.10.tar.gz
回到nginx源码根目录
执行配置脚本
以下命令行配置了 nginx 在 Windows 下的常用模块的编译,请根据自己需要调整相关模块的加入或删除
加入的特性包括 HTTP 2,SSL/TLS HTTPS 支持,HTTP 流媒体支持,socket 流转发支持,mail 服务器支持
./auto/configure \
--with-cc=cl \
--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/lib/pcre2-10.39 \
--with-zlib=objs/lib/zlib-1.3 \
--with-openssl=objs/lib/openssl-3.0.10 \
--with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0601' \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_dav_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-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
若脚本执行如果提示
auto/cc/msvc: line 132: [: : integer expression expected
是因为读取不到 VC 编译器的版本,需要手动指定。
打开 VS 的 VC 命令行,输入 cl 就能查看编译器版本了。
打开 auto/cc/msvc 文件,在文件的 NGX_MSVC_VER=19.37.32825 下方添加如下代码,即手动指定 MSVC 编译器的版本(采用截图原因是方便定位添加代码的位置)
再次执行配置脚本
开始编译
打开 VS2019 的 VC 命令行,注意选择 Native Tools,不要选择 Cross Tools。
cd 到源码根目录后,执行编译命令
nmake –f objs/Makefile
编译过程较久,视 CPU 单核性能而定,耗时十分钟到半小时不等。
没有其它报错退出时, Nginx 即编译完成。
整理文件
将源码目录下的conf、docs/html、logs、temp和objs\nginx.exe 整理到一个目录下,双击打开 nginx.exe,命令行一闪而过,打开浏览器输入 http://localhost/ 或 http://127.0.0.1/ 看到以下页面即成功。
输入以下命令结束运行 Nginx
nginx.exe -s stop
打包
回到msys
mkdir nginx
cp -r conf docs/html temp logs objs/nginx.exe nginx
tar czvf nginx.tar.gz nginx
rm -r nginx