# nginx-windows-static-build **Repository Path**: hyx-work/nginx-windows-static-build ## Basic Information - **Project Name**: nginx-windows-static-build - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-08-27 - **Last Updated**: 2026-09-03 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Nginx Windows Static Build 在 Windows 上使用 MSVC 编译器静态构建 nginx 的自动化脚本。 ## 项目简介 本项目提供一键式脚本,将 nginx 及其所有依赖库(OpenSSL、PCRE2、zlib)静态编译为单个 `nginx.exe` 可执行文件,无需额外的 DLL 依赖。 ## 编译版本 | 组件 | 版本 | |------|------| | Nginx | 1.30.4 | | OpenSSL | 3.5.7 | | PCRE2 | 10.47 | | zlib | 1.3.2 | | MSVC | 14.44 (Visual Studio 2022) | | Windows SDK | 10.0.19041.0 | ## 目录结构 ``` nginx-windows-build/ ├── download.bat # 下载源码脚本 ├── static-x86-build.bat # x86 静态编译脚本 ├── static-x64-build.bat # x64 静态编译脚本 ├── ng.bat # Nginx 命令行管理脚本 ├── ngxmanage.bat # Nginx 全能管理脚本 (交互菜单) ├── nginx_install.bat # Nginx 开机自启安装脚本 ├── nginx_uninstall.bat # Nginx 开机自启卸载脚本 ├── src/ # 源码包 │ ├── nginx-1.30.4.tar.gz │ ├── openssl-3.5.7.tar.gz │ ├── pcre2-10.47.tar.gz │ └── zlib-1.3.2.tar.gz ├── tool/ # 编译工具链 │ ├── vc-sdk19041-Host_x64-Target_x86/ # MSVC + Windows SDK (x86) │ ├── vc-sdk19041-Host_x64-Target_x64/ # MSVC + Windows SDK (x64) │ ├── msys2-portable-v2.30.0-*/ # MSYS2 (提供 bash) │ ├── strawberry-perl-5.42.2.1-*-portable/ # Perl (OpenSSL 编译需要) │ ├── single-exe/ # sed, awk, grep, curl, wget, zip │ ├── ansi-bin.zip # ansicon 彩色文字支持 (Win10以下) │ └── PortableBuildTools.exe # MSVC/SDK 下载器 ├── build/ # 编译中间目录(git-ignored) ├── nginx-static-x86/ # x86 编译输出目录 └── nginx-static-x86.zip # x86 编译产物压缩包 ``` ## 快速开始 ### 1. 准备工具链 首次使用需要下载编译工具链。 **方法一:使用分卷压缩包(x86)** 将以下文件放入 `tool/` 目录后用 WinRAR 或 7z 解压: ```batch cd tool 7z x msys2-portable-v2.30.0-ucrt64-ucrt-x86_64.zip 7z x strawberry-perl-5.42.2.1-64bit-portable.zip 7z x vc-sdk19041-Host_x64-Target_x86.zip ``` **方法二:使用 PortableBuildTools.exe(x64)** `tool/PortableBuildTools.exe` 是一个便携式 Visual Studio Build Tools 安装器,可下载 MSVC 编译器和 Windows SDK: 1. 双击运行 `tool\PortableBuildTools.exe` 2. 选择目标目录(如 `tool\vc-sdk19041-Host_x64-Target_x64`) 3. 选择 MSVC 版本(14.44)和 Windows SDK 版本(10.0.19041.0) 4. 选择目标架构为 **x64**(用于编译 x64 版本) 5. 点击安装 **`tool/` 目录最终结构:** ``` tool/ ├── vc-sdk19041-Host_x64-Target_x86/ # MSVC + Windows SDK (x86) ├── vc-sdk19041-Host_x64-Target_x64/ # MSVC + Windows SDK (x64) ├── msys2-portable-v2.30.0-ucrt64-ucrt-x86_64/ # MSYS2 (提供 bash) ├── strawberry-perl-5.42.2.1-64bit-portable/ # Perl ├── single-exe/ # sed, awk, grep, curl, wget └── PortableBuildTools.exe # MSVC/SDK 下载器 ``` 其他工具下载地址: - **MSYS2 Portable**:[下载地址](https://github.com/dirkarnez/msys2-portable/releases/#release-v2.30.0)(选择 UCRT64 版本) - **Strawberry Perl Portable**:[下载地址](https://strawberryperl.com) - **PortableBuildTools**:[GitHub](https://github.com/Data-Oriented-House/PortableBuildTools) ### 2. 下载源码 ```batch download.bat ``` 源码包将下载到 `src/` 目录。 ### 3. 编译 ```batch static-x86-build.bat # 编译 x86 版本 static-x64-build.bat # 编译 x64 版本 ``` 编译完成后,产物位于 `nginx-static-x86/` 或 `nginx-static-x64/` 目录。 ## 编译产物 ``` nginx-static-x86/ ├── nginx.exe # 静态编译的 nginx ├── conf/ # 配置文件模板 │ ├── nginx.conf │ ├── mime.types │ └── ... ├── html/ # 默认静态页面 │ ├── index.html │ └── 50x.html ├── logs/ # 日志目录 ├── temp/ # 临时文件目录 ├── ansi-bin/ # ANSI 彩色文字支持 (Win10 以下自动加载) │ ├── x64/ │ │ ├── ansicon.exe │ │ ├── ANSI32.dll │ │ └── ANSI64.dll │ └── x86/ │ ├── ansicon.exe │ └── ANSI32.dll ├── ng.bat # 命令行管理脚本 ├── ngxmanage.bat # 全能交互菜单脚本 ├── nginx_install.bat # 开机自启安装脚本 └── nginx_uninstall.bat # 开机自启卸载脚本 ``` ## 编译参数 ### Nginx Configure 选项 ```bash ./configure --with-cc=cl --builddir=objs.msvc8 --with-debug --with-http_ssl_module --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-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module ``` ### OpenSSL 编译选项 ``` no-asm no-tests no-makedepend -D_WIN32_WINNT=0x0501 ``` - `no-asm`:禁用汇编优化,避免 NASM 依赖 - `no-tests`:跳过测试用例编译 - `-D_WIN32_WINNT=0x0501`:兼容 Windows XP+ ## 使用方法 ### 管理脚本(放入 nginx 目录使用) 项目包含 4 个管理脚本,复制到编译产物目录(如 `nginx-static-x86/`)后使用: > **彩色文字支持**:输出目录包含 `ansi-bin/` 工具,在 Windows 10 以下系统自动加载 ansicon 启用 ANSI 彩色文字。Win10+ 原生支持,无需额外加载。 #### ng.bat — 命令行管理 ```batch ng.bat start # 启动 Nginx ng.bat stop # 停止 Nginx ng.bat reload # 重载配置 (不中断服务) ng.bat restart # 重启 Nginx ng.bat status # 查看运行状态 ``` > **注意**:`ng.bat` 放在 nginx 根目录执行(当前目录),不要放在外部目录调用。 > stop/reload 通过 `nginx.exe -s` 信号方式工作,无需管理员权限。 #### ngxmanage.bat — 全能交互菜单(管理员权限) ``` [1] 启动 Nginx [5] 测试配置文件 [2] 停止 Nginx [6] 安装开机自启 (schtasks) [3] 强制关闭 Nginx [7] 卸载开机自启 [4] 重载配置文件 [8] 查看状态 [0] 退出 ``` #### nginx_install.bat / nginx_uninstall.bat — 开机自启管理 通过 `schtasks` 创建/删除系统计划任务实现开机自启(无需安装 Windows 服务)。 #### 验证开机自启是否成功 安装后可通过以下方式验证: - **命令行**:`schtasks /query /tn "Nginx_Auto_Start" /v /fo list` - **图形界面**:`Win+R` → 输入 `taskschd.msc` → 左侧展开"任务计划程序库" → 查找 `Nginx_Auto_Start` ### 手动操作 #### 启动 nginx ```batch cd nginx-static-x86 start nginx.exe ``` #### 指定配置文件 ```batch nginx.exe -c conf/nginx.conf ``` #### 测试配置 ```batch nginx.exe -t ``` #### 停止 nginx ```batch nginx.exe -s stop ``` ## 环境要求 - Windows 10/11 - 至少 4GB 可用磁盘空间(编译过程需要) - 约 20-30 分钟编译时间(取决于硬件) ## 注意事项 1. **编译是静态链接的**:生成的 nginx.exe 不依赖任何外部 DLL 2. **支持 x86 和 x64**:分别使用 `static-x86-build.bat` 和 `static-x64-build.bat` 3. **包含调试信息**:编译使用了 `--with-debug` 选项,可用于排查问题 4. **支持 SSL/TLS**:完整支持 HTTPS 反向代理 5. **自动打包**:编译完成后自动生成 `nginx-static-x86.zip`(或 x64) 6. **二进制分析**:编译后自动用 `dumpbin` 检查 PE 头和 DLL 依赖 ## 故障排除 ### 编译失败 - 确保 `src/` 目录中包含所有必需的源码包 - 确保 `tool/` 目录中的工具链完整 - 检查是否有足够的磁盘空间 ### rc.exe 找不到 脚本会自动将 Windows SDK 的 x64 目录加入 PATH(因为 rc.exe 仅存在于 x64 版本中)。 ### PCRE2 编译失败 确保 nginx 源码目录下的 `objs.msvc8/lib/` 目录结构正确: ``` build/nginx/objs.msvc8/lib/ ├── pcre2-10.47/ ├── zlib-1.3.2/ └── openssl-3.5.7/ ``` ## 许可证 本项目中的脚本和配置文件无特殊许可证要求。 nginx 本身使用 BSD 许可证,OpenSSL 使用 Apache License 2.0,PCRE2 和 zlib 使用各自的宽松许可证。