Compile Nginx 64bit on Windows

Compile Nginx 64bit on Windows

Nginx is a well known webservice software, people usually use lnmp/lamp(linux nginx/apache mysql php) as Web server environment. This time I will try to make a simply index service for windows to provide download service. The Nginx has it’s basic index service named autoindex, but the layout is so bad, it didn’t provide any search service. I found a alternative index service fancyindex, it can elegant finish such work.

Prepare:

compile environment:

  • MSYS environment (or MSYS2)
  • Microsoft Visual Studio, usually requires version 2008 and above, I will use 2017 here.
  • Perl environment, it will be use to compile openssl, we can use Strawberry Perl or ActivePerl.
  • Mercurial and Git
  • nasm, ASM optimize package

nasm is optional, when nasm not exist, you have to disable assembler optimize.

get the source code:

nginx source code:

the source code for windows needs to be clone with Mercurial:

 
hg clone http://hg.nginx.org/nginx 

this step can be finished with CMD or MSYS.

other libs and additional modules:

download PCREzlib and OpenSSL, and uncompress them(following code should be run in MSYS terminal):

 

mkdir -p objs/lib
cd objs/lib
tar -xzf ../../pcre-8.42.tar.gz
tar -xzf ../../zlib-1.2.11.tar.gz
tar -xzf ../../openssl-1.0.2q.tar.gz

clone fanxyindex source code

 

git clone https://github.com/aperezdc/ngx-fancyindex.git ngx-fancyindex

Edit configuration for compiling:

Edit configuration for openssl (when you need to compile 64bit executable):

 


# Copyright (C) Igor Sysoev
# Copyright (C) Nginx, Inc.
 
 
all:
    cd $(OPENSSL)
 
    perl Configure VC-WIN64A no-shared                  \
        --prefix="%cd%/openssl"                         \
        --openssldir="%cd%/openssl/ssl"                 \
        $(OPENSSL_OPT)
 
    if exist ms\do_win64a.bat (                         \
        ms\do_win64a                                    \
        && $(MAKE) -f ms\nt.mak                         \
        && $(MAKE) -f ms\nt.mak install                 \
    ) else (                                            \
        $(MAKE)                                         \
        && $(MAKE) install_sw                           \
    )


Change warning level:

the default warning level may cause unexpected abort when compiling. So we can edit nginx\auto\cc\msvc (about line 83):

 

# warnings
 
CFLAGS="$CFLAGS -W3"

add msvc linker version:

you will see a warning if you don’t add this parameter “auto/cc/msvc: line 117: [: : integer expression expected”, this will not affect the compile (about line 17)

 

NGX_MSVC_VER=19.12 # MSVC 2017 (15.5.7)  cl 19.12
 
echo " + cl version: $NGX_MSVC_VER"

delete edit of manpage:

the Windows CMD or powershell don’t have a sed command, it may cause a error, nginx\objs\Makefile (about line 1762):

 

manpage:    objs/nginx.8
 
objs/nginx.8:   docs/man/nginx.8 objs/ngx_auto_config.h
    #sed -e "s|%%PREFIX%%||" \
        #-e "s|%%PID_PATH%%|/logs/nginx.pid|" \
        #-e "s|%%CONF_PATH%%|/conf/nginx.conf|" \
        #-e "s|%%ERROR_LOG_PATH%%|/logs/error.log|" \
        #< docs/man/nginx.8 > $@

compile:

configure:

run config with MSYS, the configuration is the same as Nginx official website with extra fancyindex module:

 

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.42 \
--with-zlib=objs/lib/zlib-1.2.11 \
--with-select_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-stream \
--with-openssl=objs/lib/openssl-1.0.2q \
--with-http_ssl_module \
--with-mail_ssl_module \
--with-stream_ssl_module \
--add-module=objs/lib/ngx-fancyindex

you should add following parameter when you don’t have nasm

 

--with-openssl-opt=no-asm

compile:

after configuration, you can compile the executable file with MSVC tool-chain:
for 64bit os, you can use 64 Native Tools Command Prompt for VS 2017, add path for nasm before compile:

 

"C:\Program Files\NASM\nasmpath.bat"

compiling:

 

nmake /f objs\MakeFile

you can have a cup of coffee now.

In the end, you can find the nginx.exe in the folder. The useage of fancyindex can be found there Link

reference:

http://nginx.org/en/docs/howto_build_on_win32.html

https://blog.csdn.net/i348018533/article/details/51701865

https://www.cnblogs.com/lidabo/p/9077938.html

Amefs, EFS, Linux
Previous Post
Compile rtorrent-0.9.4 on ubuntu 18.04
Next Post
Install driver for Intel 9560 wireless on Ubuntu 18.04

4 Comments. Leave new

  • Thanks for the article. Compiling after these changes fails using 64bit VS2022.

    Also how to properly clean to compile again?

    cmd /C “””cl”” /Zs /showIncludes /Zi /Fdossl_static.pdb /MT /Zl /Gs0 /GF /Gy /W3 /wd4090 /nologo /O2 -I”.” -I”include” -I”apps\include” -D”L_ENDIAN” -D”OPENSSL_PIC” -D”OPENSSLDIR=\”C:\\build\\nginx\\nginx-1.21.5\\objs\\lib\\openssl-3.0.1\\openssl\\ssl\”” -D”ENGINESDIR=\”C:\\build\\nginx\\nginx-1.21.5\\objs\\lib\\openssl-3.0.1\\openssl\\lib\\engines-3\”” -D”MODULESDIR=\”C:\\build\\nginx\\nginx-1.21.5\\objs\\lib\\openssl-3.0.1\\openssl\\lib\\ossl-modules\”” -D”OPENSSL_BUILDING_OPENSSL” -D”OPENSSL_SYS_WIN32″ -D”WIN32_LEAN_AND_MEAN” -D”UNICODE” -D”_UNICODE” -D”_CRT_SECURE_NO_DEPRECATE” -D”_WINSOCK_DEPRECATED_NO_WARNINGS” -D”NDEBUG” “apps\lib\tlssrp_depr.c” > apps\lib\libapps-lib-tlssrp_depr.d 2>&1″
    “lib” /nologo /out:apps\libapps.lib @C:\Users\DAVIDB~1\AppData\Local\Temp\nmF3B0.tmp
    apps\lib\libapps-lib-fmt.obj : fatal error LNK1112: module machine type ‘x86’ conflicts with target machine type ‘x64’
    NMAKE : fatal error U1077: ‘”C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\bin\HostX64\x64\lib.EXE”‘ : return code ‘0x458’
    Stop.
    NMAKE : fatal error U1077: ‘”C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\bin\HostX64\x64\nmake.exe”‘ : return code ‘0x2’
    Stop.
    NMAKE : fatal error U1077: ‘if’ : return code ‘0x2’
    Stop.
    NMAKE : fatal error U1077: ‘”C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\bin\HostX64\x64\nmake.exe”‘ : return code ‘0x2’
    Stop.

    Reply
  • Thanks for the article. However the 64bit compile fails with the following error:

    Also, how to do a complete clean and recompile?

    cmd /C “””cl”” /Zs /showIncludes /Zi /Fdossl_static.pdb /MT /Zl /Gs0 /GF /Gy /W3 /wd4090 /nologo /O2 -I”.” -I”include” -I”apps\include” -D”L_ENDIAN” -D”OPENSSL_PIC” -D”OPENSSLDIR=\”C:\\build\\nginx\\nginx-1.21.5\\objs\\lib\\openssl-3.0.1\\openssl\\ssl\”” -D”ENGINESDIR=\”C:\\build\\nginx\\nginx-1.21.5\\objs\\lib\\openssl-3.0.1\\openssl\\lib\\engines-3\”” -D”MODULESDIR=\”C:\\build\\nginx\\nginx-1.21.5\\objs\\lib\\openssl-3.0.1\\openssl\\lib\\ossl-modules\”” -D”OPENSSL_BUILDING_OPENSSL” -D”OPENSSL_SYS_WIN32″ -D”WIN32_LEAN_AND_MEAN” -D”UNICODE” -D”_UNICODE” -D”_CRT_SECURE_NO_DEPRECATE” -D”_WINSOCK_DEPRECATED_NO_WARNINGS” -D”NDEBUG” “apps\lib\tlssrp_depr.c” > apps\lib\libapps-lib-tlssrp_depr.d 2>&1″
    “lib” /nologo /out:apps\libapps.lib @C:\Users\DAVIDB~1\AppData\Local\Temp\nmF3B0.tmp
    apps\lib\libapps-lib-fmt.obj : fatal error LNK1112: module machine type ‘x86’ conflicts with target machine type ‘x64’
    NMAKE : fatal error U1077: ‘”C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\bin\HostX64\x64\lib.EXE”‘ : return code ‘0x458’
    Stop.
    NMAKE : fatal error U1077: ‘”C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\bin\HostX64\x64\nmake.exe”‘ : return code ‘0x2’
    Stop.
    NMAKE : fatal error U1077: ‘if’ : return code ‘0x2’
    Stop.
    NMAKE : fatal error U1077: ‘”C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.30.30705\bin\HostX64\x64\nmake.exe”‘ : return code ‘0x2’
    Stop.

    Reply
    • try nmake /f objs\MakeFile clean to clean the build. It shows libapps-lib-fmt.obj is a x86 build.

      Reply
  • Tzahi Cohen
    2023/1/10 11:07

    Hi, thank you for you work, I build it in to a dockerfile.. hope it will help
    https://github.com/mjhfvi/dockerfile-nginx-win-64

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

keyboard_arrow_up