Nginx 1.29.2源码通过Windows进行跨平台编译
前置准备
编译环境
在Windows
环境中构建configure
需要的环境,下载后进行安装。
[msys2-x86_64-20240113.exe]https://www.msys2.org/
提供必要的perl
执行环境,下载后进行安装。
[strawberry-perl-5.38.0.1-64bit.msi]https://strawberryperl.com/
安装VS2022
,并安装C++
桌面开发组件。
查看打开Visual Studio Installer
对当前 VS
进行修改,选择 C++
桌面应用程序,可看到对应的 MSVC
版本号。
编译依赖包
[pcre2-10.46]https://github.com/PhilipHazel/pcre2/releases
[zlib-1.3.1]http://zlib.net/
[openssl-3.5.4]https://www.openssl.org/source/
Nginx源码包
[nginx-1.29.2]https://github.com/nginx/nginx
编译过程
源码目录合并
解压源码,解压目录不能存在中文,保证路径的干净。
创建目录objs
,将 Openssl
、Zilb
、Pcre
分别解压到对应的 objs/lib/
对应文件夹下:
启动Msys2
安装并启动 Msys2
。去到安装目录,可以直接双击msys2.exe
。
命令行控制台:
安装编译依赖
执行依赖安装。
$pacman -S mingw-w64-ucrt-x86_64-gccresolving dependencies...
looking for conflicting packages...Packages (15) mingw-w64-ucrt-x86_64-binutils-2.41-3mingw-w64-ucrt-x86_64-crt-git-11.0.0.r547.g4c8123efb-1mingw-w64-ucrt-x86_64-gcc-libs-13.2.0-3 mingw-w64-ucrt-x86_64-gmp-6.3.0-2mingw-w64-ucrt-x86_64-headers-git-11.0.0.r547.g4c8123efb-1mingw-w64-ucrt-x86_64-isl-0.26-1 mingw-w64-ucrt-x86_64-libiconv-1.17-3mingw-w64-ucrt-x86_64-libwinpthread-git-11.0.0.r547.g4c8123efb-1mingw-w64-ucrt-x86_64-mpc-1.3.1-2 mingw-w64-ucrt-x86_64-mpfr-4.2.1-2mingw-w64-ucrt-x86_64-windows-default-manifest-6.4-4mingw-w64-ucrt-x86_64-winpthreads-git-11.0.0.r547.g4c8123efb-1mingw-w64-ucrt-x86_64-zlib-1.3-1 mingw-w64-ucrt-x86_64-zstd-1.5.5-1mingw-w64-ucrt-x86_64-gcc-13.2.0-3
安装gcc
与 cmake
。
$pacman -S base-devel gcc vim cmake
查看版本。
$ gcc --version
gcc (GCC) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
修改配置【可选】
configure
文件中,会将MSYS_
识别为win32
,如果是希望编译为win64
位则需要进行修改。
NGX_PLATFORM="$NGX_SYSTEM:$NGX_RELEASE:$NGX_MACHINE";case "$NGX_SYSTEM" inMINGW32_* | MINGW64_* | MSYS_*)NGX_PLATFORM=win32;;esac
修改后。
NGX_PLATFORM="$NGX_SYSTEM:$NGX_RELEASE:$NGX_MACHINE";case "$NGX_SYSTEM" inMINGW32_* | MINGW64_*)NGX_PLATFORM=win32;;esac
执行编译配置
切换目录到Nginx
源码目录下,执行如下配置指令,当不确定执行哪些模块时,可以通过--help
查看configure
支持的命令:
./configure \--with-cc=cl \--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/lib/pcre2-10.46 \--with-zlib=objs/lib/zlib-1.3.1 \--with-openssl=objs/lib/openssl-3.5.4 \--with-openssl-opt=no-asm \--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
提示如下错误:
checking for OS+ MINGW64_NT-10.0-19045 3.4.10.x86_64 x86_64+ using Microsoft Visual C++ compiler+ cl version:
auto/cc/msvc: line 136: [: : integer expression expected
找到x64 Native Tools Command Prompt for VS 2022
开发人员工具,执行命令cl
,可以看到版本号为19.44.35217
。
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.14.17
** Copyright (c) 2025 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
D:\Program Files\Microsoft Visual Studio\2022\Professional>cl
用于 x64 的 Microsoft (R) C/C++ 优化编译器 19.44.35217 版
版权所有(C) Microsoft Corporation。保留所有权利。
用法: cl [ 选项... ] 文件名... [ /link 链接选项... ]
修改auto/cc/msvc
中报错位置添加指定的编译器版本。
# MSVC 6.0 SP2 cl 12.00
# MSVC Toolkit 2003 (7.1) cl 13.10
# MSVC 2005 Express Edition SP1 (8.0) cl 14.00
# MSVC 2008 Express Edition (9.0) cl 15.00
# MSVC 2010 (10.0) cl 16.00
# MSVC 2015 (14.0) cl 19.00NGX_MSVC_VER=`$NGX_WINE $CC 2>&1 | grep 'C/C++.* [0-9][0-9]*\.[0-9]' 2>&1 \| sed -e 's/^.* \([0-9][0-9]*\.[0-9].*\)/\1/'`
NGX_MSVC_VER=19.44.35217 # MSVC 添加项 echo " + cl version: $NGX_MSVC_VER"
再次进行指令配置,输出正常:
checking for OS+ MSYS_NT-10.0-19045 3.5.3.x86_64 x86_64+ using Microsoft Visual C++ compiler+ cl version: 19.44.35217
checking for MSYS_NT-10.0-19045 specific features
creating objs/MakefileConfiguration summary+ using PCRE2 library: objs/lib/pcre2-10.46+ using OpenSSL library: objs/lib/openssl-3.5.4+ using zlib library: objs/lib/zlib-1.3.1nginx path prefix: ""nginx binary file: "/nginx.exe"nginx modules path: "/modules"nginx configuration prefix: "/conf"nginx configuration file: "/conf/nginx.conf"nginx pid file: "/logs/nginx.pid"nginx error log file: "/logs/error.log"nginx http access log file: "/logs/access.log"nginx http client request body temporary files: "temp/client_body_temp"nginx http proxy temporary files: "temp/proxy_temp"nginx http fastcgi temporary files: "temp/fastcgi_temp"nginx http uwsgi temporary files: "temp/uwsgi_temp"nginx http scgi temporary files: "temp/scgi_temp"
需要注意的是,如果将msys64\usr\bin
添加到系统环境变量,那在编译前尽可能将环境变量放到perl
安装目录对应环境变量之后,避免msys64\usr\bin
下的perl.exe
因为版本问题,导致后续编译openssl
失败。
执行程序构建
使用x64 Native Tools Command Prompt for VS 2022
开发人员工具进行编译。编译成功后,在objs
文件夹生成nginx.exe
文件。切换目录到 Nginx
解压目录中,执行编译指令。
**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.14.17
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'xxxx\Microsoft Visual Studio\2022\Enterprise>cd /d 解压目录\nginx-release-1.29.2
解压目录\nginx-release-1.29.2>nmake -f objs/Makefile
编译时间较长,需要 耐心等待~ ~ ~ ~。
提示’sed’ 不是内部或外部命令,也不是可运行的程序(实际编译已经完成,可忽略该问题)。
已完成库搜索sed -e "s|%PREFIX%||" -e "s|%PID_PATH%|/logs/nginx.pid|" -e "s|%CONF_PATH%|/conf/nginx.conf|" -e "s|%ERROR_LOG_PATH%|/logs/error.log|" < docs/man/nginx.8 > objs/nginx.8
'sed' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
解决办法:Git
下的 \usr\bin
路径添加到系统环境变量的Path
中,或者直接设置msys2
安装路径msys64\usr\bin
路径到Path
。
删除objs/nginx.8
文件,重新打开开发人员工具执行nmake -f objs/Makefile
。nginx.8
文件重新生成。
验证配置服务
拷贝源码中的conf,contrib,html。并复制objs
中生成的nginx.exe
,同时创建logs
以及temp
目录。执行如下指令:
>nginx -t
nginx: the configuration file xxx/conf/nginx.conf syntax is ok
nginx: configuration file xxx/conf/nginx.conf test is successful
>start nginx
>nginx -version
nginx version: nginx/1.29.2
>nginx -V
nginx version: nginx/1.29.2
built by cl 19.44.35217
built with OpenSSL 3.5.4 30 Sep 2025
TLS SNI support enabled
configure arguments: --with-cc=cl --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/lib/pcre2-10.46 --with-zlib=objs/lib/zlib-1.3.1 --with-openssl=objs/lib/openssl-3.5.4 --with-openssl-opt=no-asm --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
执行指令启动服务。
>start nginx
>netstat -ano|findstr 80
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 31940
>tasklist |findstr 31940
nginx.exe 31940 Console 1 12,492 K
访问页面http://localhost
,结果如下,表示编译基本成功。