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

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

wptr33 2025-01-21 21:57 18 浏览

简介

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

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

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

// 下载
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 的性能,建议在低峰期进行操作。

相关推荐

【推荐】一款开源免费、美观实用的后台管理系统模版

如果您对源码&技术感兴趣,请点赞+收藏+转发+关注,大家的支持是我分享最大的动力!!!项目介绍...

Android架构组件-App架构指南,你还不收藏嘛

本指南适用于那些已经拥有开发Android应用基础知识的开发人员,现在想了解能够开发出更加健壮、优质的应用程序架构。首先需要说明的是:AndroidArchitectureComponents翻...

高德地图经纬度坐标批量拾取(高德地图批量查询经纬度)

使用方法在桌面上新建一个index.txt文件,把下面的代码复制进去保存,再把文件名改成index.html保存,双击运行打开即可...

flutter系列之:UI layout简介(flutter ui设计)

简介对于一个前端框架来说,除了各个组件之外,最重要的就是将这些组件进行连接的布局了。布局的英文名叫做layout,就是用来描述如何将组件进行摆放的一个约束。...

Android开发基础入门(一):UI与基础控件

Android基础入门前言:...

iOS的布局体系-流式布局MyFlowLayout

iOS布局体系的概览在我的CSDN博客中的几篇文章分别介绍MyLayout布局体系中的视图从一个方向依次排列的线性布局(MyLinearLayout)、视图层叠且停靠于父布局视图某个位置的框架布局(M...

TDesign企业级开源设计系统越发成熟稳定,支持 Vue3 / 小程序

TDesing发展越来越好了,出了好几套组件库,很成熟稳定了,新项目完全可以考虑使用。...

WinForm实现窗体自适应缩放(winform窗口缩放)

众所周知,...

winform项目——仿QQ即时通讯程序03:搭建登录界面

上两篇文章已经对CIM仿QQ即时通讯项目进行了需求分析和数据库设计。winform项目——仿QQ即时通讯程序01:原理及项目分析...

App自动化测试|原生app元素定位方法

元素定位方法介绍及应用Appium方法定位原生app元素...

61.C# TableLayoutPanel控件(c# tabcontrol)

摘要TableLayoutPanel在网格中排列内容,提供类似于HTML元素的功能。TableLayoutPanel控件允许你将控件放在网格布局中,而无需精确指定每个控件的位置。其单元格...

想要深入学习Android性能优化?看完这篇直接让你一步到位

...

12个python数据处理常用内置函数(python 的内置函数)

在python数据分析中,经常需要对字符串进行各种处理,例如拼接字符串、检索字符串等。下面我将对python中常用的内置字符串操作函数进行介绍。1.计算字符串的长度-len()函数str1='我爱py...

如何用Python程序将几十个PDF文件合并成一个PDF?其实只要这四步

假定你有一个很无聊的任务,需要将几十个PDF文件合并成一个PDF文件。每一个文件都有一个封面作为第一页,但你不希望合并后的文件中重复出现这些封面。即使有许多免费的程序可以合并PDF,很多也只是简单的将...

Python入门知识点总结,Python三大数据类型、数据结构、控制流

Python基础的重要性不言而喻,是每一个入门Python学习者所必备的知识点,作为Python入门,这部分知识点显得很庞杂,内容分支很多,大部分同学在刚刚学习时一头雾水。...