These sources were used in compiling this doc:
- https://amefs.net/en/archives/1935.html
- https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.50/
- Intro
- Prerequisites
- Source packages
- Setup
- Target choice
- cl.exe version
- nginx makefile changes
- MSYS2 configuration
- Adding Modules
- RTMP module notes:
- Compiling
- Running
- RTMP nginx config
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)
- Install Visual Studio 2022, ensure that you have native C/C++ build environments included in the install selection (x86 and x64)
- Download and install StrawberryPerl: https://strawberryperl.com/
- Download and install MSYS2: https://www.msys2.org/
- Download and install sed for windows: http://gnuwin32.sourceforge.net/packages/sed.htm
- Optional: download and install NASM: https://www.nasm.us/
Download the following source packages:
- nginx source: https://nginx.org/en/download.html
- Zlib: https://zlib.net/
- OpenSSL: Make sure this is the v1 version of OpenSSL, not the v3 version: https://github.com/openssl/openssl/tags
- PCRE: make sure this is the original version of PCRE not the v2 version: https://sourceforge.net/projects/pcre/files/pcre/
- Extract nginx to a folder of your choice e.g. C:\temp
- Create an objs folder in the nginx folder e.g. C:\temp\nginx-1.23.1\objs
- Create a lib folder under the objs folder e.g. C:\temp\nginx-1.23.1\objs\lib
- Extract zlib, OpenSSL and PCRE in to the lib folder
- 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
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
-
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
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"
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 |
- Open MSYS2
- Navigate to the nginx folder e.g.
cd /C/temp/nginx-1.23.1
-
Read the Adding Modules section before proceeding if you wish to add e.g. RTMP or other nginx modules to the build
-
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
- 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
- Once this completes successfully you are now ready to compile nginx!
To add a module to the build you will have to:
- obtain the module source
- extract it to the objs/lib folder
- add it to the configure flags
As an example to add RTMP support to nginx:
-
Download the latest RTMP module: https://github.com/sergey-dryabzhinsky/nginx-rtmp-module/releases
-
Extract it to the objs/lib folder and remove the version information
- e.g. objs/libs/nginx-rtmp-module
-
add a line to the configure command to include this module:
--with-stream_ssl_module \
--add-module=objs/lib/nginx-rtmp-module
- Run configure command
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>
- Switch back to or open the relevant Native Tools Command Prompt for Visual Studio 2022
- Add the path to sed:
SET PATH=%PATH%;"c:\Program Files (x86)\GnuWin32\bin"
- 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\"
- You can run nmake to build:
nmake /f objs\Makefile
- Go and have a cup of tea, hopefully if all goes well when you are finished drinking it, nginx should have compiled.
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
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
There isn't, generating the markup was enough work. Is there something specific that is unclear in the instructions?