Windows从源代码编译Nginx
安装 Visual Studio Build Tools
2019 Build Tools
2022 Build Tools
打开后等待下载即可
下载安装C++开发工具
勾选安装C++桌面开发
耐心等待下载完成
然后重启系统
下载安装Git
不会装的可以百度Git如何安装,还是不会的留言我补充如何安装
下载安装Perl
下载完成后安装即可
下载安装Nasm
https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/win64/
接着安装
添加环境变量
Windows键
+S
搜索 env
安装sed-windows
https://github.com/mbuilov/sed-windows
重命名为sed.exe
放到任意目录下添加环境变量
下载Pcre
https://sourceforge.net/projects/pcre/files/pcre/8.44/
下载zlib
下载Openssl
https://github.com/openssl/openssl/releases/tag/OpenSSL_1_1_1l
下载Nginx
https://github.com/nginx/nginx/tags
开始构建
创建一个目录放Nginx
禁止包含空格,中文
例子C:\Makefile\nginx
解压Nginx源代码到
C:Makefilenginx
创建objslib文件夹
解压
- zlib
- openssl
- pcre
注意不要多层嵌套了
比如
openssl/openssl/源码
重命名openssl的文件夹为openssl-1.1.1l
解压完成后确认无嵌套问题后复制到nginx\objs\lib
即可
编辑配置文件
修改nginx\auto\lib\openssl\makefile.msvc
WIN32改为WIN64A
ms改为win64a
记得CTRL+S保存!!!!!!!!!!!!!!!!!!!!!!!
修改nginx\auto\cc\msvc
搜索CFLAGS="$CFLAGS -W4"
改为CFLAGS="$CFLAGS -W3"
查看VS版本
WIN+S搜索native Tools
选择X64版
输入cl
2019版为19.29
2022版为19.31
注释
*# NGX_MSVC_VER=`$NGX_WINE $CC 2>&1 | grep 'Compiler Version' 2>&1 \*
*# | sed -e 's/^.\* Version \(.\*\)/\1/'`
添加
NGX_MSVC_VER=19.31 #输入上面你自己的版本
保存
打开Gitbash
Win+S搜索Git bash
以管理员身份运行
切换到Nginx源码目录,目录加""号
cd "C:\Makefile\nginx"
右键paste粘贴
命令
auto/configure \
--with-cc=cl \
--builddir=objs \
--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/pcre-8.44 \
--with-zlib=objs/lib/zlib-1.2.12 \
--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=objs/lib/openssl-1.1.1l \
--with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' \
--with-http_ssl_module \
--with-mail_ssl_module \
--with-stream_ssl_module \
复制以上代码
放到任意文本编辑器
根据你的实际情况修改
--with-pcre=objs/lib/pcre-8.44 \
--with-zlib=objs/lib/zlib-1.2.12 \
--with-openssl=objs/lib/openssl-1.1.1l \
就是你的nginx/objs/lib文件夹名字
然后粘贴到Git bash执行
WIN+S管理员打开Native Tools
选择X64版的
一样的,切换目录到Nginx源码
cd C:\Makefile\nginx
编译
nmake /f objs\MakeFile