Windows 编译安装 nginx 服务器 + rtmp 模块
本人的有关博客:《Windows 编译安装 nginx 服务器 + rtmp 模块》、《Ubuntu 编译安装 nginx》、《Arm-Linux 移植 Nginx》
背景:
公司产品有使用 nginx+ rtmp 的技术要求,本人在 arm、Ubuntu都配置过。为了配合同事开发,所以尝试在Windows搭建这样的生产环境。
注意:虽然官网上有提供编译好的windows版本的nginx ,但凡需要添加删除模块的,都需要通过nginx源码进行编译。
Host :Windows-10,core-i7
编译之前的环境搭建
以下软件的安装,路径不要出现中文字符与空格
安装 MinGW
下载好MinGW以后,安装时勾选以下选项
- mingw-devloper-toolkit
- mingw32-base
- mingw32-gcc-g++
- msys-base
安装完成以后按住Win+R
打开cmd
,输入gcc --version
测试是否成功
C:\Users\Schips>gcc --version
gcc (x86_64-posix-seh, Built by strawberryperl.com project) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
下面是 msys的根目录,运行 msys.bat可以打开 MinGW 的shell窗口(待会会用到)
安装 mercurial
mercurial用于下载 nginx-win版本的源码,安装没有任何其他需要注意的地方
安装完成以后按住Win+R 打开cmd,输入hg 测试是否成功
安装 strawberryperl
Strawberry Perl是Windows环境的一个Perl环境,它的目的是尽可能接近UNIX系统上的Perl环境,在编译nginx及其依赖库的时候需要用到。
安装 nasm
NASM全称The Netwide Assembler,是一款基于80x86和x86-64平台的汇编语言编译程序,其设计初衷是为了实现编译器程序跨平台和模块化的特性,在编译nginx的时候可能需要对编译脚本进行修改的时候会用到。
安装完成后,需要将其安装路径加入到PATH环境变量。
下载有关的源码
nginx-win32-src 下载
在cmd中执行命令
hg clone http://hg.nginx.org/nginx nginx-win32-src
编译运行环境依赖
下载这些文件
nginx 额外的模块(可选)
编译nginx
解压
解压上面下载的源码,放置到自己喜欢的地方(建议是放到 msys/1.0/home/$USER/下面)
配置
在 MinGW 的 msys程序,来到 nginx-win32-src 源码根目录
根据自身的实际情况修改下方配置,并保存为一个.bat脚本文件
auto/configure --with-cc=cl --builddir=objs --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 \
--with-cc-opt=-DFD_SETSIZE=1024 \
--with-select_module \
--with-debug \
--with-http_ssl_module \
--with-pcre=../pcre-8.30 --with-zlib=../zlib-1.2.11 --with-openssl=../openssl-1.0.2t \
--add-module=../nginx-rtmp-module-1.1.11
配置成功的结果如下:
这里会发现有一个异样的提示,auto/cc/msvc: line 117: [: : integer expression expected
,只要修改文件 $nginx源码目录\auto\cc\msvc
即可,
在 echo " + cl version: $NGX_MSVC_VER"
的前面加入一行 NGX_MSVC_VER=15.00
,当然不修改也不会影响后续的编译。
如果需要指定后续编译nginx时禁用优化,则将其中的 CFLAGS="$CFLAGS -O2"
修改为 CFLAGS="$CFLAGS -Od"
。
到此为止,不再需要 msys(如果此后的步骤依旧使用msys的话,会出现找不到头文件等问题)
修修补补
修改一处会导致编译异常的错误
打开文件 $nginx源码目录\auto\lib\openssl\makefile.msvc,修改如下地方:
ms\do_ms 👉 ms\do_nasm
添加 一个环境变量
找到 vs ide中用到sed.exe添加到系统环境变量中,例如是在:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git\usr\bin\sed.exe
使用IDE进行编译
打开VS的命令行提示符窗口
切换到 nginx源码所在位置,并 在指定objs/Makefile以后 nmake
cd /d c:\MinGW\msys\1.0\home\Schips\nginx_on_windows\source\nginx-win32-src
nmake /f objs\MakeFile
成功编译之后,最终会有以下打印信息,并且在nginx源码目录的objs目录中可以看到最终生成的nginx.exe文件。
构建有关的目录结构
拷贝 objs/nginx.exe
、 objs/nginx.pdb(可选的调试项目)
拷贝 conf
目录, docs/html
目录
手动创建logs
和temp
目录
最终效果如下图:
# /cygdrive/c/MinGW/msys/1.0/home/Schips/nginx_output
$ tree
.
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi_params
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── nginx.conf
│ ├── scgi_params
│ ├── uwsgi_params
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
│ ├── access.log
│ ├── error.log
│ └── nginx.pid
├── nginx.exe
├── nginx.pdb
└── temp
测试:
通过cmd命令行运行nginx
start /b nginx.exe -c conf\nginx.conf
浏览器输入 localhost
,能够看到这些信息
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
输入 nginx.exe -s stop
关闭nginx 服务
添加 rtmp 配置
辛辛苦苦完成了对nginx的编译。
编辑 nginx运行时需要使用的 配置文件 conf/nginx.conf
(有关的配置可以参考 nginx-rtmp-module-1.1.11\test\nginx.conf)
添加以下内容
rtmp {
server {
listen 1935; #监听的端口号
application myapp { #自定义的名字
live on;
}
}
}
保存,重新运行 nginx服务
测试RTMP服务
使用FFMPEG 作为流的解析
FFMPEG的编译本人在这里跳过,直接在网上找到了别人编译好的程序。
这里有下载地址: https://dl.pconline.com.cn/download/406611-1.html
随便找一个视频,使用以下命令:
./ffmpeg.exe -i test.mp4 -acodec aac -strict experimental -ar 44100 -ac 2 -r 25 -f flv rtmp://192.168.10.213:1935/myapp/23
-i 要处理视频文件的路径,可以是其他的流(如rtsp)
-f 强迫采用flv格式
最后是输出到的地址
rtmp://192.168.1.11:1935/myapp/23 说明:rtmp://IP:PORT/ myapp指nginx配置文件中自定义的,23指输出文件的名字
使用VLC 作为 流的获取(在视频还没播放完之前)
媒体(M)-> 打开网络串流
rtmp://192.168.10.213:1935/myapp/23
最终效果
附录:
编译rtmp 遇到 以下错误:
ngx_rtmp_core_module.c
../nginx-rtmp-module-1.1.11/ngx_rtmp_core_module.c(611): error C2220: 警告被视为错误 - 没有生成“object”文件
../nginx-rtmp-module-1.1.11/ngx_rtmp_core_module.c(611): warning C4456: “sa”的声明隐藏了上一个本地声明
../nginx-rtmp-module-1.1.11/ngx_rtmp_core_module.c(506): note: 参见“sa”的声明
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\cl.EXE"”: 返回 代码“0x2”
Stop.
错误分析:
编译rtmp时,Makefile中应该是指定了类似 -Werror 的参数,所以会把 警告视为错误 最终导致了 编译中断
那么只需要找到有关代码进行修改即可。从错误的情况来看,只需要找到 rtmp模块目录下的ngx_rtmp_core_module.c,注释掉 611 行即可。
nginx支持 rtsp
nginx 没有处理 rtsp 的模块,所以可以使用第三方工具(如 ffmpeg)进行转推
ffmpeg -rtsp_transport tcp -i <rtsp流地址> -vcodec -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://<本机ip>/myapp/testav
正文完。