Nginx for Windows

技术 秋水逸冰 58浏览 0评论

Nginx 官方网站提供的 Windows 版本也是可以直接适用于 Windows 系统的。
本文提供的 Nginx 区别于上述官方版,是利用 MSYS2 环境 gcc compiler 自行编译的,并不是使用 Microsoft Visual C compiler(Microsoft Visual Studio)。

1. 版本号

Nginx 版本 1.29.4

nginx version: nginx/1.29.4
built by gcc 15.2.0 (Rev8, Built by MSYS2 project)
built with OpenSSL 3.5.4 30 Sep 2025
TLS SNI support enabled
configure arguments: --with-cc=cc --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-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-stream_realip_module --with-stream_ssl_preread_module --with-pcre=objs/lib/pcre2-10.47 --with-zlib=objs/lib/zlib-1.3.1 --with-openssl=objs/lib/openssl-3.5.4 --with-openssl-opt='no-asm no-tests no-makedepend -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

2. 如何在 MSYS2 环境编译 Nginx

先来看看官方版本的编译选项:

configure arguments: --with-cc=cl --builddir=objs.msvc8 --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.msvc8/lib/pcre2-10.46 --with-zlib=objs.msvc8/lib/zlib-1.3.1 --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-stream_realip_module --with-stream_ssl_preread_module --with-openssl=objs.msvc8/lib/openssl-3.5.4 --with-openssl-opt='no-asm no-tests no-makedepend -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

这些选项基本都可以沿用。我在编译时,将编译器换成 gcc 了,同时升级了 pcre2 到版本 10.47。
编译前的准备:
安装 MSYS2,以及 GCC 编译器等。此过程略过不提。
编译过程如下:
打开 MSYS2 MINGW64 的 shell 窗口。
下载并解压所需软件。

curl -Lo nginx-1.29.4.tar.gz https://nginx.org/download/nginx-1.29.4.tar.gz
tar -xzf nginx-1.29.4.tar.gz
cd nginx-1.29.4
curl -Lo openssl-3.5.4.tar.gz https://github.com/openssl/openssl/releases/download/openssl-3.5.4/openssl-3.5.4.tar.gz
curl -Lo pcre2-10.47.tar.gz https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.47/pcre2-10.47.tar.gz
curl -Lo zlib-1.3.1.tar.gz https://www.zlib.net/zlib-1.3.1.tar.gz
mkdir -pv objs/lib
tar -xzf pcre2-10.47.tar.gz -C objs/lib
tar -xzf zlib-1.3.1.tar.gz -C objs/lib
tar -xzf openssl-3.5.4.tar.gz -C objs/lib

编译选项:

./configure \
    --with-cc=cc \
    --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-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-stream_realip_module \
    --with-stream_ssl_preread_module \
    --with-pcre=objs/lib/pcre2-10.47 \
    --with-zlib=objs/lib/zlib-1.3.1 \
    --with-openssl=objs/lib/openssl-3.5.4 \
    --with-openssl-opt='no-asm no-tests no-makedepend -D_WIN32_WINNT=0x0501' \
    --with-http_ssl_module \
    --with-mail_ssl_module \
    --with-stream_ssl_module

接下来使用 make 命令编译,-j 后的数字与 CPU 核数一致即可:

make -j8

编译完成后,安装到指定目录:

mkdir -pv /home/user/nginx
make DESTDIR=/home/user/nginx install

接着使用 strip 命令删除二进制文件中所有调试符号,以减少文件大小

cd /home/user/nginx
strip nginx.exe

一些已知问题,参照这里
虽然可以启动多个 workers,但实际上只有一个 worker 在工作。
不支持 UDP(以及本质上的 QUIC)功能,也就是 --with-http_v3_module 编译选项不可用。

3. 下载已经编译好的二进制文件

下载地址:
https://dl.lamp.sh/files/nginx-1.29.4.zip

文件 nginx-1.29.4.zip 的属性
Size: 3003629 byte,2.9 MB
md5sum: 81535102beb53e16efb5a4833881f16c
sha1sum: d2dc7fe38d504ddafdeafb48dbd4cdeef6e175a9
sha256sum: d432232bd9770c0e56ea192660b85410141cbdf050db68db99ba760b6bf3ce2d

同时编译了其另一个分支 freenginx

下载地址:
https://dl.lamp.sh/files/freenginx-1.29.4.zip
文件 freenginx-1.29.4.zip 的属性
Size: 2974879 byte,2.9 MB
md5sum: c0b156933ffe556e9a97e05e2951a79c
sha1sum: 88fa63cda7aecccf8f9b60db84a2fe934ad4e035
sha256sum: 9177fb01e5fbf004781871656672af46b931b231b1751fe27a501f78aa623dbf

写在最后

请关注我的 Telegram 频道:https://t.me/qiushuiyibing
我会在此不定期发布一些杂七杂八的作品。
同时也欢迎加入交流群:https://t.me/qiushui2018

转载请注明:秋水逸冰 » Nginx for Windows

发表我的评论
取消评论

请输入正确答案后提交评论 *超出时限。 请再次填写验证码。

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址