Windows下利用MSYS2和VS的nmake编译nginx源码

目录

一、使用说明

二、安装软件

2.1 下载依赖库

2.3 下载并安装 StrawberryPerl

2.4 下载并安装 MSYS 2

2.5 nginx源代码下载 

三、编译配置

3.1 设置NGX_MSVC_VER

3.2 配置 Makefile

扫描二维码关注公众号,回复: 17464199 查看本文章

3.3 编译代码

3.4 整理Nginx发布环境

四、错误处理


一、使用说明

本文章主要记录Windows下利用MSYS2和VS的nmake编译nginx源码。

这是Nginx官方说明 https://nginx.org/en/docs/howto_build_on_win32.html

To build nginx on the Microsoft Win32® platform you need:

  • Microsoft Visual C compiler. Microsoft Visual Studio® 8 and 10 are known to work.
  • MSYS or MSYS2.
  • Perl, if you want to build OpenSSL® and nginx with SSL support. For example ActivePerl or Strawberry Perl.
  • Git client.
  • PCREzlib and OpenSSL libraries sources.

需要安装软件Visual Studio 2022 (一定要包含C++ x64/x86 生成工具,需要通过nmake执行编译)

Openssl:实现安全套接字ssl功能

Pcre:实现正则表达式解析

Zlib:实现gzip压缩解压缩功能

二、安装软件
2.1 下载依赖库

zlib源代码下载

https://zlib.net/ 

PCRE源代码  

https://sourceforge.net/projects/pcre/files/pcre/

openssl源代码下载 https://github.com/openssl/openssl/releases/tag/openssl-3.3.2

2.3 下载并安装 StrawberryPerl

因为需要编译 OpenSSL, 需要使用 perl 进行配置

下载地址: https://strawberryperl.com/

默认安装到结束

2.4 下载并安装 MSYS 2

在编译阶段用作配置 Makefile  下载地址:https://www.msys2.org/

全部默认安装

2.5 nginx源代码下载 

地址 https://hg.nginx.org/nginx

点左侧ZIP进行下载

将文件解压,在nginx源码根目录下创建objs文件夹,再在objs下创建lib文件夹,结构如下图

将pcre-8.45.zip、zlib131.zip、openssl-openssl-3.3.2.zip解压到刚刚的lib目录

三、编译配置
3.1 设置NGX_MSVC_VER

如果不指定版本会在配置时出现报错auto/cc/msvc: line 132: [: : integer expression expected

需要根据你自己的实际vs版本号填写,只有填写成功,后面nmake才能编译成功。修改nginx\auto\cc下的msvc文件,指定NGX_MSVC_VER版本号,我vs是2022,所以是1930

3.2 配置 Makefile

使用 msys2 的任意环境进入 nginx 源码目录

从开始菜单打开安装的msys2 msys

进入到nginx源码根目录下

cd /c/sourcecode/nginx

 执行以下命令,用于生成适用于 MSVC 的 Makefile

输入如下配置命令后回车:(注意pcre,zlib,openssl版本要与你实际的文件夹一致)

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=32768 \
  --with-pcre=objs/lib/pcre-8.45 \
  --with-zlib=objs/lib/zlib-1.3.1 \
  --with-openssl=objs/lib/openssl-3.3.2 \
  --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

执行成功,会出现以下界面 

Administrator@DESKTOP-0JFTJUC MSYS /c/sourcecode/nginx
# 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=32768 \
  --with-pcre=objs/lib/pcre-8.45 \
  --with-zlib=objs/lib/zlib-1.3.1 \
  --with-openssl=objs/lib/openssl-3.3.2 \
  --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
checking for OS
 + MSYS_NT-10.0-19045 3.5.3-d8b21b8c.x86_64 x86_64
 + using Microsoft Visual C++ compiler
 + cl version: 1930
checking for MSYS_NT-10.0-19045 specific features
creating objs/Makefile

Configuration summary
  + using PCRE library: objs/lib/pcre-8.45
  + using OpenSSL library: objs/lib/openssl-3.3.2
  + using zlib library: objs/lib/zlib-1.3.1

  nginx 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"


Administrator@DESKTOP-0JFTJUC MSYS /c/sourcecode/nginx

可以看到根目录下的objs下已经生成了makefile文件

3.3 编译代码

以管理员方式打开VS对应的 X64 Native Tools Command … for VS 2022

进入到nginx根目录,输入如下命令后回车

nmake -f objs/Makefile

 就开始编译

等待编译完成,会在 nginx/objs/ 文件夹内出现 nginx.exe

至此,Nginx编译完成。打开objs目录,显示如下

3.4 整理Nginx发布环境

nginx.exe运行依赖同级目录下存在conf、html、logs、temp目录及配置文件,否则无法启动。将源码目录下的conf、contrib、html、logs、temp和objs\nginx.exe整理到一个目录下。

启动nginx.exe,一个nginx服务器就搭建起来了,运行截图  

看生成的nginx编译信息  nginx -V

C:\nginx>nginx -V
nginx version: nginx/1.27.2
built by cl 1930
built with OpenSSL 3.3.2 3 Sep 2024
TLS SNI support enabled
configure arguments: --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=32768 --with-pcre=objs/lib/pcre-8.45 --with-zlib=objs/lib/zlib-1.3.1 --with-openssl=objs/lib/openssl-3.3.2 --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
四、错误处理

4.1 auto/cc/msvc: line 132: [: : integer expression expected

这里报错原因是msvc版本缺失,无法自动识别,根据3.1章节进行设置

4.2 找不到VS带的X64 Native Tools Command … for VS xxxx

检查VS安装是否包含了C++ x64/x86 生成工具。