從0開始搭建一個(gè)基于nginx的點(diǎn)播/直播服務(wù)器

前提:一臺(tái)已經(jīng)安裝好系統(tǒng)的Linux服務(wù)器,這里我使用的是Linux debian10 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u2 (2019-11-11) x86_64版本

1、官網(wǎng)下載最新nginx壓縮包

http://nginx.org/en/download.html

2、Github下載nginx-http-flv-module

https://github.com/winshining/nginx-http-flv-module

直播依賴模塊,基于nginx-rtmp-module開發(fā),支持flv直播

3、 下載nginx_mod_h264_streaming模塊

https://download.csdn.net/download/robinson_0612/9738948

點(diǎn)播核心模塊,這里使用的是nginx_mod_h264_streaming-2.2.7.tar版本

4、 資料準(zhǔn)備

在opt文件夾下新建一個(gè)文件夾tools ,將下載的nginx 、nginx_mod_h264_streaming-2.2.7.tar和nginx-http-flv-module-master.zip?拷貝到tools 文件夾下

5、在/user/local下創(chuàng)建nginx 文件夾

非常重要!用于安裝nginx的目標(biāo)位置,也是編譯nginx的指定生成位置

6、安裝依賴項(xiàng)

注意Debian/Ubuntu系統(tǒng)下使用apt-get安裝,RedHat系的Linux系統(tǒng)使用yum命令獲取,本文以Debian系統(tǒng)作示例,RedHat系的系統(tǒng)請(qǐng)自行查找對(duì)應(yīng)模塊的安裝指令

a)? ? ? apt-get install unzip//獲取unzip工具用于解壓zip壓縮包

b)? ? ?apt-get update//更新環(huán)境

c)? ? ? apt-get install build-essential//獲取gcc編譯環(huán)境

e)? ? ?apt-get install libpcre3libpcre3-dev//獲取prce依賴庫(kù),用于正則驗(yàn)證

f)? ? ? apt-get insatll zlib1g-dev//獲取zlib依賴庫(kù),注意是1g不是lg

g)? ? ?apt-get install openssllibssl-dev//獲取openssl依賴庫(kù),用于加密訪問

7、 將tools 下面的nginx-http-flv-module壓縮包和nginx_mod_h264_streaming解壓到/usr/local/nginx下面

目標(biāo)文件夾為第5步創(chuàng)建的nginx文件夾

a)? ? ? cd /opt/tools

b)? ? ?unzip nginx-http-flv-module-1.2.6.zip

c)? ? ?mv /opt/tools/nginx-http-flv-module? /usr/local/nginx/

d)? ? ?tar -zxvf?nginx_mod_h264_streaming-2.2.7.tar

e)? ? mv /opt/tools/nginx_mod_h264_streaming-2.2.7 /usr/local/nginx/

8、 將nginx-http-flv-module模板添加到nginx中,生成make文件 并安裝nginx(核心步驟)

a)? ? tar -zxvf nginx-1.14.2.tar.gz//解壓下載好的nginx源碼壓縮包,實(shí)際文件名以下載的為準(zhǔn)

b)? ? cd nginx-1.14.2//進(jìn)入解壓好的nginx源碼目錄

c)? ? ./configure--prefix=/usr/local/nginx --without-http_gzip_module --with-http_ssl_module--with-http_flv_module --with-http_mp4_module --add-module=/usr/local/nginx/nginx-http-flv-module-master? ? ? --add-module=/usr/local/nginx/nginx_mod_h264_streaming-2.2.7//核心步驟

--prefix=? 用于指定nginx編譯生成的目標(biāo)文件夾,必須為第5步創(chuàng)建的nginx文件夾

--without-指定不需要編譯的模塊

--with-指定要編譯的模塊

--add-module=指定要添加的第三方模塊,其后跟該模塊源碼的目錄,即本文第6點(diǎn)文件解壓之后的安裝目錄

每條指令之間用空格 隔開

d)? ? 打開/usr/local/nginx/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c文件,將

if (r->zero_in_uri)

? {

? ? return NGX_DECLINED;

? }這段代碼注釋(我這里是158-161行)

//防止報(bào)錯(cuò)

e)? ? ?make?CFLAGS='-Wno-implicit-fallthrough'? //防止報(bào)錯(cuò)

f)? ? ?make install//安裝

9、 修改配置文件

打開/usr/local/nginx/conf/nginx.conf文件修改配置

#user nobody;

worker_processes? 1;

events {

? ? worker_connections? 1024;

}

rtmp_auto_push on;

rtmp_auto_push_reconnect 1s;

rtmp_socket_dir /tmp;

rtmp{

out_queue 4096;

out_cork 8;

max_streams 128;

timeout 15s;

drop_idle_publisher 15s;

log_interval 5s;

log_size 1m;

server{

listen 1935;//默認(rèn)為1935,無(wú)需配置

server_name localhost;

application myapp{//直播應(yīng)用名稱,與播放地址中的app對(duì)應(yīng)

live on;

gop_cache on;

? }

application hls{

? live on;

? hls on;

? hls_path /usr/local/nginx/html/hls;

}

application dash{

? live on;

? dash on;

? dash_path /usr/local/nginx/html/dash;

}

}

}

http {

? ? include? ? ? mime.types;

? ? default_type? application/octet-stream;

? ? sendfile? ? ? ? on;

tcp_nopush? ? on;

tcp_nodelay? ? on;

? ? keepalive_timeout? 65;

? ? server {

? ? ? ? listen? ? ? 8088;//http服務(wù)端口,通過http獲取視頻流時(shí)從此端口獲取

server_name? localhost;

? ? ? ? location / {

? ? ? ? ? ? root? html;//web文件根目錄

? ? ? ? ? ? index? index.html index.htm;//指定默認(rèn)首頁(yè)文件

? ? ? ? }

location ~* \.flv$ {#flv 支持

root /opt/tools/media/flv;#目標(biāo)flv格式video文件夾位置,需手動(dòng)創(chuàng)建此文件夾,并將對(duì)應(yīng)格式的視頻文件放在此文件夾下

}

location ~ \.mp4$ {

root /opt/tools/media/mp4;#目標(biāo)mp4格式video文件夾位置

}

location /live{

flv_live on;

chunked_transfer_encoding? on;

add_header 'Access-Control-Allow-Origin' '*';//設(shè)置允許跨域播放

add_header 'Access-Control-Allow-Credentials' 'true';

}

location /hls{

types {

application/vnd.apple.mpegurl m3u8;

video/mp2t ts;

}

root /usr/local/nginx/html/hls;

add_header 'Cache-Control' 'no-cache';

}

location /dash {

root /usr/local/nginx/html/dash;

add_header 'Cache-Control' 'no-cache';

}

location /stat {

#configuration of push & pull status

? rtmp_stat all;

? rtmp_stat_stylesheet stat.xsl;

}

location /stat.xsl {

? root /usr/local/nginx/nginx-http-flv-module;

}

location /control {

? ? ? ? ? ? rtmp_control all; #configuration of control module of rtmp

? ? ? ? } ? ? ?

? ? ? ? error_page? 500 502 503 504? /50x.html;

? ? ? ? location = /50x.html {

? ? ? ? ? ? root? html;

? ? ? ? }

? ? }

}

10、? ? 直播測(cè)試

推流地址:rtmp://172.16.1.97:1935/myapp/123

對(duì)應(yīng)播放地址:http://172.16.1.97:8081/live?port=1935&app=myapp&stream=123

11、? ? ? ? 點(diǎn)播測(cè)試

http:172.16.1.97:8088/test.flv

http:172.16.1.97:8088/1.mp4

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容