当前位置: 首页 > news >正文

Nginx 1.29.2源码通过Windows进行跨平台编译

image.png

前置准备

编译环境

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 版本号。
msvsc.png

编译依赖包

[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

编译过程

源码目录合并

解压源码,解压目录不能存在中文,保证路径的干净。
Pasted image 20251019032018.png
创建目录objs,将 OpensslZilbPcre 分别解压到对应的 objs/lib/对应文件夹下:
Pasted image 20251019022707.png

启动Msys2

安装并启动 Msys2。去到安装目录,可以直接双击msys2.exe
image.png
命令行控制台:
image.png

安装编译依赖

执行依赖安装。

$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

安装gcccmake

$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/Makefilenginx.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,结果如下,表示编译基本成功。
Pasted image 20251019161844.png

http://www.dtcms.com/a/504556.html

相关文章:

  • joomla网站建设互联网平台推广方案
  • 建设网站的一般过程视频网站用什么cms
  • C++:jieba库的安装使用保姆级教程
  • 网站空间的根目录刷粉网站推广便宜
  • 建设网站需要哪些流程图办公室装饰
  • 校园官方网站建设营销软文范文200字
  • 找网络公司做网站需要注意的青岛建设银行股份有限公司网站
  • 苏州设置网站建设怎么看网站是哪家公司做的
  • 使用ACME自动签发SSL 证书
  • 泉州网站建设技术公司成功的网络营销案例及分析
  • 网站数据库连接错误建设项目所在地公共媒体网站
  • 网站如何提高转化率社区网站设计
  • 建站优化公司黎平网站开发
  • 免费php企业网站源码外贸网站建设厦门
  • 个人做的网站能备案吗帝国cms 网站名称
  • 广州智能模板建站大型公司办公室设计
  • 做网站需要什么基础主体备案与网站备案
  • 惠东网站建设网站制作用的软件有哪些
  • 我想网上做网站搜索引擎实训心得体会
  • 猪八戒网站怎么做任务wordpress位置
  • 网站建设unohacha电子招标投标平台网站建设
  • 夹娃娃网站如何做网站开发struts
  • 国外毕业设计网站网站建设 钱
  • android 利用反射和注解绑定控件id和点击事件
  • windows 网站模板网站流量统计查询
  • 601138(工业富联)-2025年10月19日
  • 桂林企业网站建设做网站的作用
  • 免费网站模块一个人在线观看免费中文
  • 比亚迪召回超11万辆车:质量管控亮起警示灯
  • 企业网站建设推广费用网站里添加百度地图