Nginx通過nginx-rtmp-module模塊搭建流媒體服務器實現(xiàn)直播
準備篇
CentOS 7.x默認使用的是firewall作為防火墻,這里改為iptables防火墻。
1、關(guān)閉firewall
systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall開機啟動 systemctl mask firewalld systemctl stop firewalld yum remove firewalld
2、安裝iptables防火墻
RTMP基于TCP, 默認使用端口1935,再開放81端口
yum install iptables-services #安裝 vi /etc/sysconfig/iptables #編輯防火墻配置文件
# sample configuration for iptables service # you can edit this manually or use system-config-firewall # please do not ask us to add additional ports/services to this default configuration *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 81 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 1935 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT :wq! #保存退出
systemctl restart iptables.service #最后重啟防火墻使配置生效 systemctl enable iptables.service #設置防火墻開機啟動 /usr/libexec/iptables/iptables.init restart #重啟防火墻
3、關(guān)閉SELINUX
vi /etc/selinux/config #SELINUX=enforcing #注釋掉 #SELINUXTYPE=targeted #注釋掉 SELINUX=disabled #增加 :wq! #保存退出 setenforce 0 #使配置立即生效
4、軟件包下載
4.1、nginx-rtmp-module #nginx流媒體插件
https://github.com/arut/nginx-rtmp-module
https://github.com/arut/nginx-rtmp-module/archive/master.zip
https://github.com/arut/nginx-rtmp-module/archive/refs/tags/v1.2.2.tar.gz
4.2、ngx_cache_purge
http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
4.3、openssl
https://www.openssl.org/source/openssl-1.1.1w.tar.gz
4.4、pcre
https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz/download
4.5、zlib
https://www.zlib.net/zlib-1.3.tar.gz
4.6、nginx
https://nginx.org/download/nginx-1.24.0.tar.gz
5、創(chuàng)建安裝路徑
nginx安裝包存放目錄:mkdir -p /data/server/nginx/packages
nginx安裝目錄:mkdir -p /data/server/nginx
nginx擴展安裝目錄:mkdir -p /data/server/nginx/install
安裝篇
1、安裝編譯工具包
yum install make gcc gcc-c++ perl zlib-devel
2、安裝nginx
2.1安裝pcre
cd /data/server/nginx/packages
mkdir -p /data/server/nginx/install/pcre
tar zxvf pcre-8.45.tar.gz
cd pcre-8.45
./configure --prefix=/data/server/nginx/install/pcre
make
make install
2.2安裝zlib
cd /data/server/nginx/packages
mkdir -p /data/server/nginx/install/zlib
tar zxvf zlib-1.3.tar.gz
cd zlib-1.3
./configure --prefix=/data/server/nginx/install/zlib
make
make install
2.3安裝openssl
cd /data/server/nginx/packages
mkdir -p /data/server/nginx/install/openssl
tar zxvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config -fPIC shared zlib --prefix=/data/server/nginx/install/openssl
make
make install
2.4安裝nginx
nginx默認運行賬號和組是Linux系統(tǒng)的內(nèi)置賬號和組nobody
groupadd www
useradd -g www www -s /bin/false
cd /data/server/nginx/packages
tar zxvf ngx_cache_purge-2.3.tar.gz #加載ngx_cache_purge模塊
tar zxvf nginx-rtmp-module-1.2.2.tar.gz #加載nginx-rtmp-module模塊
tar zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure --prefix=/data/server/nginx --user=www --group=www --without-http_memcached_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_realip_module --with-stream --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/data/server/nginx/client --http-proxy-temp-path=/data/server/nginx/proxy --http-fastcgi-temp-path=/data/server/nginx/fcgi --http-uwsgi-temp-path=/data/server/nginx/uwsgi --with-openssl=/data/server/nginx/packages/openssl-1.1.1w --with-zlib=/data/server/nginx/packages/zlib-1.3 --with-pcre=/data/server/nginx/packages/pcre-8.45 --add-module=../ngx_cache_purge-2.3 --add-module=../nginx-rtmp-module-1.2.2
注意:--with-openssl=/data/server/nginx/packages/openssl-1.1.1w --with-zlib=/data/server/nginx/packages/zlib-1.3 --with-pcre=/data/server/nginx/packages/pcre-8.45指向的是源碼包解壓的路徑,而不是安裝的路徑,否則會報錯。
make
make install
/data/server/nginx/sbin/nginx #啟動Nginx
#查看nginx版本和安裝模塊信息
/data/server/nginx/sbin/nginx -V
/data/server/nginx/sbin/nginx -s stop #關(guān)閉Nginx
2.5配置nginx啟動腳本
vi /data/server/nginx/nginx.sh
#!/bin/bash NGINX_PATH="/data/server/nginx/sbin/nginx" PID_FILE="/data/server/nginx/logs/nginx.pid" function start_nginx() { if [ -f $PID_FILE ]; then echo "Nginx is already running." else echo "Starting Nginx..." $NGINX_PATH echo "Nginx started." fi } function stop_nginx() { if [ -f $PID_FILE ]; then echo "Stopping Nginx..." $NGINX_PATH -s stop echo "Nginx stopped." else echo "Nginx is not running." fi } function restart_nginx() { if [ -f $PID_FILE ]; then echo "Restarting Nginx..." $NGINX_PATH -s stop sleep 1 $NGINX_PATH echo "Nginx restarted." else echo "Nginx is not running. Starting it now..." $NGINX_PATH echo "Nginx started." fi } function reload_nginx() { if [ -f $PID_FILE ]; then echo "Reloading Nginx configuration..." $NGINX_PATH -s reload echo "Nginx configuration reloaded." else echo "Nginx is not running. Cannot reload the configuration." fi } function status_nginx() { if [ -f $PID_FILE ]; then echo "Nginx is running with PID $(cat $PID_FILE)." else echo "Nginx is stopped." fi } case "$1" in start) start_nginx ;; stop) stop_nginx ;; restart) restart_nginx ;; reload) reload_nginx ;; status) status_nginx ;; *) echo "Usage: $0 {start|stop|restart|reload|status}" exit 1 ;; esac
:wq! #保存退出
#添加執(zhí)行權(quán)限
chmod +x /data/server/nginx/nginx.sh
sh /data/server/nginx/nginx.sh start #啟動
配置篇
1、創(chuàng)建目錄
mkdir -p /data/video/vod #視頻點播文件存放目錄
mkdir -p /data/video/videolive #視頻直播文件存放目錄
mkdir -p /data/video/live
mkdir -p /data/video/tmp/hls
上傳視頻文件到相應目錄
/data/video/vod/vod.mp4
/data/video/videolive/videolive.mp4
chown www.www -R /data/video/ #設置目錄權(quán)限
2、配置nginx.conf文件,添加rtmp模塊
#備份文件配置
cp /data/server/nginx/conf/nginx.conf /data/server/nginx/conf/nginx.conf.bak
vi /data/server/nginx/conf/nginx.conf #編輯,添加配置 注意:rtmp配置和http是平級的
#在最后一行的“}” 上面添加以下代碼
server {
listen 81;
root /data/video/tmp/hls;
index index.html;
location / {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
expires -1;
}
}
#在最后一行的“}” 下面添加以下代碼
#rtmp config
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
}
application videolive {
live on;
record off;
play /data/video/videolive/;
}
application vod {
play /data/video/vod/;
}
application hls {
live on;
hls on;
hls_path /data/video/tmp/hls;
hls_fragment 5s;
}
}
}
sh /data/server/nginx/nginx.sh restart #重啟
測試篇
1、在同一網(wǎng)段的終端電腦上安裝VLC播放器
下載地址:http://www.videolan.org/
安裝完成后打開播放器-媒體-打開網(wǎng)絡串流-網(wǎng)絡,添加地址
rtmp://192.168.30.6:1935/vod/vod.mp4
rtmp://192.168.30.6:1935/videolive/videolive.mp4
即可播放視頻
2、安裝OBS Studio或者EV錄屏軟件,設置好直播畫面
2.1添加rtmp直播源
rtmp://192.168.30.6:1935/live
在VLC播放器添加地址
rtmp://192.168.30.6:1935/live
就能觀看直播了
2.2添加hls直播源
rtmp://192.168.30.6:1935/hls/mystream
在VLC播放器添加地址
http://192.168.30.6:81/mystream.m3u8
就能觀看直播了
在移動端手機瀏覽器里面可以直接打開這個鏈接觀看,手機瀏覽器內(nèi)置了對HLS協(xié)議的支持。
Safari瀏覽器默認也可以直接打開m3u8格式的視頻。
至此,Nginx通過nginx-rtmp-module模塊搭建流媒體服務器實現(xiàn)直播完成。
相關(guān)文章
nginx從安裝到配置詳細說明(安裝,安全配置,防盜鏈,動靜分離,配置 HTTPS,性能優(yōu)化)
這篇文章主要介紹了nginx從安裝到配置詳細說明(安裝,安全配置,防盜鏈,動靜分離,配置 HTTPS,性能優(yōu)化,緩存,url重寫),需要的朋友可以參考下2022-01-01SpringBoot前端后端分離之Nginx服務器下載安裝過程
Nginx是一款輕量級的Web服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,這篇文章主要介紹了SpringBoot前端后端分離之Nginx服務器,需要的朋友可以參考下2022-08-08通過Nginx搭建Tomcat9集群并實現(xiàn)Session共享
這篇文章主要介紹了通過Nginx搭建Tomcat9集群并實現(xiàn)Session共享,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06Windows?Server?Nginx?反向代理Spring?Boot配置無效報404未找到的問題
一個Spring?Boot的系統(tǒng),開發(fā)完成發(fā)布到Windows服務器里,使用nginx作為反向代理,修改刷新配置文件,nginx.conf,總是報錯404,這篇文章主要介紹了Windows?Server?Nginx?反向代理Spring?Boot配置無效?404?未找到的問題及解決方案2024-01-01Nginx重定向后請求參數(shù)丟失的原因分析及解決方案
在日常開發(fā)和運維中,我們經(jīng)常會遇到需要使用 Nginx 進行反向代理的場景,但在配置 proxy_pass 時,有時候可能會遇到請求參數(shù)丟失的問題,在這篇文章中,我們將會詳細探討這個問題并給出幾種解決方案,需要的朋友可以參考下2023-11-11