百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

Nginx 动态编译加载第三方流媒体服务模块:Nginx-RTMP-Module

off999 2025-02-13 13:42 13 浏览 0 评论

简介

Nginx 1.9.11开始增加加载动态模块支持,可以在不停机的情况下加载和卸载模块。从此不再需要替换nginx文件即可增加第三方扩展。目前官方只有几个模块支持动态加载,第三方模块需要升级支持才可编译成模块。

通过帮助命令./configure --help | grep dynamic 查看是否支持动态加载模块

Bash
~/build/openresty-1.19.3.1$ ./configure --help | grep dynamic
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --add-dynamic-module=PATH          enable dynamic external module
  --with-compat                      dynamic modules compatibility

如上可看出官方支持9个动态模块编译,需要增加第三方模块,使用参数--add-dynamic-module=即可。

动态模块概述

  • 可以加载到NGINX中的模块是用C编写的
  • 获取匹配的NGINX开源版本
  • 获取模块源,并在必要时更改模块的配置文件
  • 使用configure命令的-?-add-dynamic-module参数针对NGINX开源版本构建动态模块
  • 将生成的动态模块(.so文件)加载到NGINX中(modules目录下),并像使用内置模块一样使用它

动态模块语法

  • 命令:load_module
  • Default: —
  • 上下文配置段: main
  • 说明:版本必须>=1.9.11
  • 实例:load_module modules/ngx_mail_module.so;

编译动态模块

这里安装基于Nginx的流媒体服务器:nginx-rtmp-module 模块

项目地址:
https://github.com/arut/nginx-rtmp-module

下载 OpenResty

OpenResty? 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

Bash
// 下载
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz

// 解压
tar -zxvf openresty-1.19.3.1.tar.gz

下载 nginx-rtmp-module

git clone https://github.com/arut/nginx-rtmp-module.git

下载模块路径地址为:
/home/www/build/nginx-rtmp-module

编译

进入 OpenResty 目录

cd openresty-1.19.3.1

编译

./configure --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -O3' \
--with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-pcre-jit \
--with-stream --with-stream_ssl_module --with-stream=dynamic --with-file-aio \
--with-threads --with-http_v2_module --with-http_realip_module \
--with-http_mp4_module --with-http_gzip_static_module --with-http_ssl_module \
--with-http_stub_status_module --with-http_xslt_module \
--with-openssl-opt='-g enable-tlsext' --with-stream \
--with-stream_ssl_preread_module \
--with-compat --add-dynamic-module=/home/www/build/nginx-rtmp-module

// 编译
make

添加 --with-compat 选项,生成可加载模块的 Nginx 可执行文件

注意:这里不要执行make install 命令,否则会覆盖已安装的Nginx二进制文件,我们这里是动态加载只需要编译模块生成第三方模块.so文件就行了。

复制模块到指定目录

将模块库ngx_rtmp_module.so文件复制到
/usr/local/openresty/nginx/modules

cp /home/www/build/openresty-1.19.3.1/build/nginx-1.19.3/objs/ngx_rtmp_module.so /usr/local/openresty/nginx/modules/

加载模块

要将模块加载到Nginx,请将load_module指令添加到nginx.conf主配置文件的主上下文中。

load_module modules/ngx_rtmp_module.so;

nginx.conf主配置文件参考

user tinywan;
worker_processes auto;

// 其他配置...

load_module modules/ngx_rtmp_module.so;

// 其他配置...

使用模块

新增流媒体服务器配置

rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        drop_idle_publisher 10s;
        idle_streams off;
    
        application live {
            live on;
        }

        application hls {
            live on;
            hls on;
            hls_path /tmp/hls;
        }

        # MPEG-DASH is similar to HLS
        application dash {
            live on;
            dash on;
            dash_path /tmp/dash;
        }
    }
}

注意:该配置需要和HTTP上下文并列,而不是放在HTTP模块里面

检测Nginx配置文件是否OK

sudo /usr/local/openresty/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful

没问题直接重启openresty服务即可。

sudo systemctl restart openresty.service

安全组设置

打开安全组设置,在[入方向] 开放1935端口,1935端口是rtmp默认监听端口,必须在这里设置开放,否则在服务器中打开和监听1935端口后公网环境连接不到该端口。

OBS 推流

推流地址:rtmp://{服务器公网IP}/live/tinywan2024

VLC 拉流播放

拉流地址:rtmp://{服务器公网IP}/live/tinywan2024

出现“is not binary compatible in”错误的解决方案

sudo /usr/local/openresty/nginx/sbin/nginx -t
nginx: [emerg] module "/usr/local/openresty/nginx/modules/ngx_rtmp_module.so" is not binary compatible in /usr/local/openresty/nginx/conf/nginx.conf:7
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test failed

原因

第三方模块的编译中包含的签名和使用的nignx不一致。

解决方案

先通过 nginx -V 命令得到当前配置的configure配置

/usr/local/openresty/nginx/sbin/nginx -V
nginx version: openresty/1.19.3.1built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) built with OpenSSL 1.1.1  11 Sep 2018TLS SNI support enabledconfigure arguments: --prefix=/usr/local/openresty/nginx \
--with-cc-opt='-O2 -O3' --add-module=../ngx_devel_kit-0.3.1 \
--add-module=../iconv-nginx-module-0.14 \
--add-module=../echo-nginx-module-0.62  \
--add-module=../xss-nginx-module-0.06  \
--add-module=../ngx_coolkit-0.2  \
--add-module=../set-misc-nginx-module-0.32  \
--add-module=../form-input-nginx-module-0.12  \
--add-module=../encrypted-session-nginx-module-0.08  \
--add-module=../srcache-nginx-module-0.32  \
--add-module=../ngx_lua-0.10.19  \
--add-module=../ngx_lua_upstream-0.07  \
--add-module=../headers-more-nginx-module-0.33  \
--add-module=../array-var-nginx-module-0.05  \
--add-module=../memc-nginx-module-0.19  \
--add-module=../redis-nginx-module-0.3.7  \
--add-module=../rds-json-nginx-module-0.15  \
--add-module=../rds-csv-nginx-module-0.09  \
--add-module=../ngx_stream_lua-0.0.9  \
--with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib  \
--with-pcre-jit --with-stream --with-stream_ssl_module  \
--with-stream=dynamic --with-file-aio --with-threads  \
--with-http_v2_module --with-http_realip_module  \
--with-http_mp4_module --with-http_gzip_static_module  \
--with-http_ssl_module --with-http_stub_status_module  \
--with-http_xslt_module --with-openssl-opt='-g enable-tlsext'  \
--with-stream --with-stream_ssl_preread_module

在复制所有的配置命令。添加到:

./configure [“你的nignx -V 得到的配置参数”] --add-dynamic-module=/home/www/build/nginx-rtmp-module

注意事项:

  • 动态模块只能在 Nginx 1.9.11 及以上版本中使用。
  • 加载和卸载模块需要 root 权限。
  • 加载和卸载模块会影响 Nginx 的性能,建议在低峰期进行操作。

相关推荐

一键打包,随时运行,Python3项目虚拟环境一键整合包的制作(Venv)

之前我们介绍了如何使用嵌入式Python3环境给项目制作一键整合包,在使用嵌入式Python环境时,通常是作为另一个应用程序的一部分,而Python3虚拟环境是为了在开发过程中隔离项目所需的...

PyInstaller 是一个将 Python 代码打包成可执行文件的工具

PyInstaller是一个将Python代码打包成可执行文件的工具。它可以将Python代码打包成Windows、Mac、Linux等平台下的可执行文件,使得你可以将Python应...

知识储备之用py2app将Python代码打包成MacOS可用的APP

自己电脑上有完整的python环境,所以偶尔写个小工具什么的都很easy,直接命令行run一波就OK,但是如果需要再朋友的电脑上运行,帮别人写了一个小工具,他没有运行环境,就很麻烦。不能让人家也从ho...

使用PyInstaller将Python文件打包成Windows系统可执行文件

官网PyInstaller官方网站:http://www.pyinstaller.org/国内镜像库PyInstallerGitee:https://gitee.com/mirrors/pyinst...

松勤技术精选:Python打包exe,换电脑也可直接运行哦!

为什么要打包exe有的时候只需要让别人运行某种功能,传输文件以及代码是需要别人配置好一定的环境才可以操作,而打包成exe文件就可以直接运行文件。pyinstaller打包python中毕竟常用的打包方...

111.Python——基于pipenv打包PaddlePaddle的GUI项目

飞桨PaddlePaddle是百度的深度学习框架,用来做一些项目还是非常不错。但是打包就是一件非常麻烦的过程。在文中有讲过打包问题。29.Python程序打包成可执行文件——常见疑难问题解决办法。本文...

「Python自学笔记」Beeware初体验之Python全平台应用打包

内容更新地址:【Python自学笔记】Beeware初体验,Python如何实现全平台应用打包(exeapkios)_xiaoqiangclub的博客-CSDN博客第一个应用安装环境这里的环境是W...

Python项目pyinstaller打包工具提示词整理出来了,太酷了

Python项目pyinstaller打包工具的提示词整理出来了,下面是完整的提示词请开发一个PythonGUI程序,功能是Python项目打包工具,具有以下特点:1.界面要求:使用PyQt5开发...

Python GUI开发:打包PySide2应用(spyder打包python)

之前的文章我们介绍了怎么使用PySide2来开发一个简单PythonGUI应用。这次我们来将上次完成的代码打包。我们使用pyinstaller。注意,pyinstaller默认会将所有安装的pack...

用Docker打包Python应用的关键要点与实践

引言在微服务架构和云原生时代,Docker已成为应用打包与部署的标准工具。本文将通过一个完整示例,介绍如何用Docker高效打包Python应用,并提炼出关键实践要点。一、Dockerfile基础结构...

精品收藏!Python 程序封装!打包成exe程序!

在Windows操作系统中,我们常用的桌面软件都是带有操作界面的软件,那么Python编写的程序如何才能让用户方便使用呢?因此,程序打包也成为用户的需求,下面详细介绍一下如何进行Python的程序打包...

用python开发的APP程序如何打包成APK安装文件

要将Python开发的APP程序打包成APK安装文件,可以使用第三方工具PyInstaller和Buildozer。下面是一个简单的步骤指南:安装PyInstaller和Buildozer:使用pip...

python打包按部就班(python打包安装文件)

一步一步安装软件包1,pywin32python.exe-mpipinstall--upgradepip更新pippipinstallPyInstaller安装打包工具编写最简单的hel...

爆强!直接把 Python 编写的图形程序打包为安卓 APP

请大家多多关注点赞哦如果想使用Python语言编写图形界面程序,那么有不少的框架可以提供支持,比如Tkinter、QtforPython、WxPython等等。不过这些框架都是只能创建桌面图...

PyOxidizer:将 Python 应用打包成单一可执行文件的神器

三、PyOxidizer基本使用使用PyOxidizer打包应用的基本流程如下:1.创建新项目首先,我们使用pyoxidizerinit命令创建一个新的PyOxidizer项目:py...

取消回复欢迎 发表评论: