參考地址:
我在git上傳了已經編譯好的帶有:http_gzip_static_module模塊的1.20.2版本,需要的可以自取,需要集成其它模塊的,也可以拿下代碼之后自行按文章內容進行重新編譯。
https://gitee.com/lingyangcj/nginx-1.20.2-build-windows
當前目錄下的壓縮文件 nginx-1.20.2(編譯后的文件).zip
准備工作
要在 Microsoft Win32平台上構建 nginx,您需要:
- Microsoft Visual C 編譯器。因為需要用到nmake,所以勾選了使用C++的桌面開發。
- MSYS或 MSYS2。
- Perl,如果您想構建具有 SSL 支持的OpenSSL ®和 nginx。例如ActivePerl 或Strawberry Perl。
- Mercurial客戶端。
- PCRE、zlib 和OpenSSL庫源。
構建步驟
下載源碼
需要Mercurial客戶端: https://www.mercurial-scm.org/release/tortoisehg/windows/tortoisehg-6.0.0-x64.msi hg clone http://hg.nginx.org/nginx
下載解壓Nginx所依賴的庫
https://www.openssl.org/source/openssl-1.1.1m.tar.gz
http://zlib.net/zlib-1.2.11.tar.gz
https://udomain.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.zip
Openssl:實現安全套接字ssl功能
Pcre:實現正則表達式解析
Zlib:實現gzip壓縮解壓縮功能
安裝Perl解釋器,主要為了解析openssl中的腳本
https://strawberryperl.com/download/5.32.1.1/strawberry-perl-5.32.1.1-32bit.msi
在源碼根目錄下新建一個objs目錄,在objs目錄下新建lib文件夾,將解壓好的openssl、pcre、zlib拷貝到lib目錄下
mkdir objs mkdir objs/lib cd objs/lib tar -xzf ../../pcre-8.44.tar.gz tar -xzf ../../zlib-1.2.11.tar.gz tar -xzf ../../openssl-1.1.1l.tar.gz
下載安裝MSYS2(模擬unix環境)
https://github.com/msys2/msys2-installer/releases/download/2021-11-30/msys2-x86_64-20211130.exe
生成Makefile之前先將源碼的auto\cc目錄的msvc文件做如下修改:
- 將83行的優化選項設置為-W3
- 將117和119行注釋掉(因為我用的VS2019,版本較高,需要注釋掉,不然會報異常)
打開MSYS2,並切換到Nginx源碼包的解壓目錄下(根目錄)
cd /H/Work/company_work_space/scmp-project/nginx-1.20.2
執行以下命令,注意configure文件的位置在根目錄還是auto下(根目錄可以剪切到auto下使用下方的命令) ,注意修改pcre和zlib和openssl的路徑是否對應,
with-http_gzip_static_module是我環境需要gzip模塊額外加上的,默認模塊不包括這個。可以通過在這里添加模塊實現nginx 的自定義模塊。
auto/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/pcre-8.45 \ --with-zlib=objs/lib/zlib-1.2.11 \ --with-openssl=objs/lib/openssl-1.1.1m \ --with-openssl-opt=no-asm \ --with-http_ssl_module \ --with-http_gzip_static_module
上述命令是生成可以通過VS的nmake編譯的Makefile
編譯整個過程是這樣(此過程發生在MSYS2命令行內):
Administrator@WIN-7ECCVP09AEC MSYS /H/Work/company_work_space/scmp-project/nginx-1.20.2
# auto/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/pcre-8.45 \
--with-zlib=objs/lib/zlib-1.2.11 \
--with-openssl=objs/lib/openssl-1.1.1m \
--with-openssl-opt=no-asm \
--with-http_ssl_module \
--with-http_gzip_static_module
checking for OS
+ MSYS_NT-10.0-17763 3.2.0-340.x86_64 x86_64
+ using Microsoft Visual C++ compiler
+ cl version:
checking for MSYS_NT-10.0-17763 specific features
creating objs/Makefile
Configuration summary
+ using PCRE library: objs/lib/pcre-8.45
+ using OpenSSL library: objs/lib/openssl-1.1.1m
+ using zlib library: objs/lib/zlib-1.2.11
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"
看到ngin xxx 很多行,至此,可以關閉MSYS2了。此時源碼根目錄下生成一個Makefile文件,objs文件夾下生成一些Makefile和編譯要用到的頭文件
運行制作
1.運行VS命令行x86 Native Tools Command Prompt for VS 2022(Visual Studio 2022社區版) 2.切換到Nginx根目錄 cd H:\Work\company_work_space\scmp-project\nginx-1.20.2 3.執行以下命令:nmake -f objs/Makefile 4.用VS的nmake進行編譯后會在在objs目錄下生成exe文件 5.新建一個nginx的文件夾,在源碼中的conf,contrib,html拷貝過來,將objs目錄中生成的nginx.exe放進來,再新建logs和temp文件夾,就完成了nginx for windows的工作。
Nginx build for windows總結:
1.Nginx是32位的軟件,所以最好使用32位的MSYS2和VS的32位命令行編譯
2.如果要在64位上編譯,需要修改openssl中生成的Makefile文件
3.Openssl1.1以上的版本需要將Makefile中的 VC-WIN32 改成 VC-WIN64A
4.Openssl1.0的版本需要更改INSTALL.W64文件文件找到To build for Win64/x64,用perl執行那幾行腳本。