Skip to content

Instantly share code, notes, and snippets.

@sweetlilmre
Last active November 14, 2024 01:29
Show Gist options
  • Save sweetlilmre/43b0c05e17f9fb0aacab0381d1db0fe7 to your computer and use it in GitHub Desktop.
Save sweetlilmre/43b0c05e17f9fb0aacab0381d1db0fe7 to your computer and use it in GitHub Desktop.
Building nginx with addtional modules on Windows

Building nginx on Windows (x86 and x64) with additional modules (VS 2022)

These sources were used in compiling this doc:

Table of contents

Intro

Why do this? If all you want is a stock build of nginx, you can download one from the site: https://nginx.org/en/download.html

If however, you want to add a custom module into nginx, you'll need to roll your own build. If you want to pay for a commercial version with rtmp support, it looks like these folks will help you out: http://nginx-win.ecsds.eu/ (I am in no way affiliated or endorse this product, this is just an FYI)

Prerequisites

  1. Install Visual Studio 2022, ensure that you have native C/C++ build environments included in the install selection (x86 and x64)
  2. Download and install StrawberryPerl: https://strawberryperl.com/
  3. Download and install MSYS2: https://www.msys2.org/
  4. Download and install sed for windows: http://gnuwin32.sourceforge.net/packages/sed.htm
  5. Optional: download and install NASM: https://www.nasm.us/

Source packages

Download the following source packages:

  1. nginx source: https://nginx.org/en/download.html
  2. Zlib: https://zlib.net/
  3. OpenSSL: Make sure this is the v1 version of OpenSSL, not the v3 version: https://github.com/openssl/openssl/tags
  4. PCRE: make sure this is the original version of PCRE not the v2 version: https://sourceforge.net/projects/pcre/files/pcre/

Setup

  1. Extract nginx to a folder of your choice e.g. C:\temp
  2. Create an objs folder in the nginx folder e.g. C:\temp\nginx-1.23.1\objs
  3. Create a lib folder under the objs folder e.g. C:\temp\nginx-1.23.1\objs\lib
  4. Extract zlib, OpenSSL and PCRE in to the lib folder
  5. RENAME the extracted folders to remove version information e.g.
    • zlib-1.2.12 to zlib
    • OpenSSL_1_1_1q to openssl
    • pcre-8.45 to pcre

Target choice

It's now time to decide if you want to build for an x86 or x64 target.

This will have implications on editing configuration further down the line.

x86:

  • use the x86 Native Tools Command Prompt for Visual Studio 2022

x64:

  • use the x64 Native Tools Command Prompt for Visual Studio 2022

cl.exe version

  • Open the relevant Native Tools Command Prompt for Visual Studio 2022

  • Type:

cl.exe
  • You should get something like this:
Microsoft (R) C/C++ Optimizing Compiler Version 19.33.31629 for x64
  • Copy the version number and save it somewhere for later e.g. 19.33.31629
  • Leave this window open for later

nginx makefile changes

x86 and x64

Edit the nginx msvc file e.g. C:\temp\nginx-1.23.1\auto\cc\msvc

  • Find the line that has:
echo " + cl version: $NGX_MSVC_VER"
  • Remember the cl.exe version we stored earlier? We'll need this now.
  • Insert a line above the echo like this:
NGX_MSVC_VER='19.33.31629'
echo " + cl version: $NGX_MSVC_VER"
  • Find the line that has:
# stop on warning
CFLAGS="$CFLAGS -WX"
  • comment the stop on warnings option:
# stop on warning
# CFLAGS="$CFLAGS -WX"

x64

Edit the openssl makefile.msvc file e.g. C:\temp\nginx-1.23.1\auto\lib\openssl\makefile.msvc

You will need to change 3 lines, replace the bold text with the alternative as shown below:

Replace this With this
perl Configure VC-WIN32 no-shared perl Configure VC-WIN64A no-shared
ms\do_ms.bat ms\do_win64a.bat
ms\do_ms ms\do_win64a

MSYS2 configuration

  1. Open MSYS2
  2. Navigate to the nginx folder e.g.
  cd /C/temp/nginx-1.23.1
  1. Read the Adding Modules section before proceeding if you wish to add e.g. RTMP or other nginx modules to the build

  2. Run the following command:

./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 \
--with-zlib=objs/lib/zlib \
--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 \
--with-http_ssl_module \
--with-mail_ssl_module \
--with-stream_ssl_module \
--with-openssl-opt=no-asm
  1. Building with NASM seems broken at the moment, further research is required. The instuctions below are retained for interest.
If you installed NASM then remove the --with-openssl-opt=no-asm option and the trailing slash on the previous line, the last line should look like this:

--with-stream_ssl_module
  1. Once this completes successfully you are now ready to compile nginx!

Adding Modules

To add a module to the build you will have to:

  1. obtain the module source
  2. extract it to the objs/lib folder
  3. add it to the configure flags

As an example to add RTMP support to nginx:

  1. Download the latest RTMP module: https://github.com/sergey-dryabzhinsky/nginx-rtmp-module/releases

  2. Extract it to the objs/lib folder and remove the version information

    • e.g. objs/libs/nginx-rtmp-module
  3. add a line to the configure command to include this module:

   --with-stream_ssl_module \
   --add-module=objs/lib/nginx-rtmp-module
  1. Run configure command

RTMP module notes:

This module failed to compile due to an auto generated file that was missing some includes:

\objs\lib\nginx-rtmp-module\hls\ngx_rtmp_mpegts_crc.c

needs to have the nginx includes added e.g. the include section should look something like this:

#include <ngx_config.h>
#include <ngx_core.h>
#include "ngx_rtmp_mpegts_crc.h"     /* include the header file generated with pycrc */
#include <stdlib.h>
#include <stdint.h>

Compiling

  1. Switch back to or open the relevant Native Tools Command Prompt for Visual Studio 2022
  2. Add the path to sed:
SET PATH=%PATH%;"c:\Program Files (x86)\GnuWin32\bin"
  1. Building with NASM seems broken at the moment, further research is required. The instuctions below are retained for interest.

If you installed NASM and did not set --with-openssl-opt=no-asm, you will need to add NASM to the path:

SET PATH=%PATH%;"c:\Program Files\NASM\"
  1. You can run nmake to build:
nmake /f objs\Makefile
  1. Go and have a cup of tea, hopefully if all goes well when you are finished drinking it, nginx should have compiled.

Running

I suggest you download a pre-built package from nginx and just replace the binary which should be in the objs/ directory after a successful compile

RTMP nginx config

To set up rtmp add this block to your nginx.conf file:

rtmp {
        server {
                listen 1935;
                chunk_size 4096;

                application live {
                        live on;
                        record off;
                }
        }
}

To stream to your server, start nginx and then for e.g. OBS, set up a new profile and change the broadcast settings to:

Streaming Service: Custom
Server: rtmp://<your server ip>/live
Play Path/Stream Key: test 

To view the stream, use e.g. VLC and point to rtmp://localhost/live/test

@WalkInCyber
Copy link

Very useful, these steps work well ❤️

@sweetlilmre
Copy link
Author

I'm glad it helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment