當前位置: 妍妍網 > 碼農

Nginx 動態編譯載入第三方串流媒體服務模組:Nginx-RTMP-Module

2024-02-29碼農

簡介

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® 是一個基於 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.111 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 的效能,建議在低峰期進行操作。

  • 載入和解除安裝模組可能會導致 Nginx 行程崩潰,建議備份配置檔和 Nginx 可執行檔。