從0開始搭建一個基于nginx的點播/直播服務器

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

1、官網下載最新nginx壓縮包

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

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

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

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

3、 下載nginx_mod_h264_streaming模塊

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

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

4、 資料準備

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

5、在/user/local下創建nginx 文件夾

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

6、安裝依賴項

注意Debian/Ubuntu系統下使用apt-get安裝,RedHat系的Linux系統使用yum命令獲取,本文以Debian系統作示例,RedHat系的系統請自行查找對應模塊的安裝指令

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

b)? ? ?apt-get update//更新環境

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

e)? ? ?apt-get install libpcre3libpcre3-dev//獲取prce依賴庫,用于正則驗證

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

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

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

目標文件夾為第5步創建的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源碼壓縮包,實際文件名以下載的為準

b)? ? cd nginx-1.14.2//進入解壓好的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編譯生成的目標文件夾,必須為第5步創建的nginx文件夾

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

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

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

每條指令之間用空格 隔開

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行)

//防止報錯

e)? ? ?make?CFLAGS='-Wno-implicit-fallthrough'? //防止報錯

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;//默認為1935,無需配置

server_name localhost;

application myapp{//直播應用名稱,與播放地址中的app對應

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服務端口,通過http獲取視頻流時從此端口獲取

server_name? localhost;

? ? ? ? location / {

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

? ? ? ? ? ? index? index.html index.htm;//指定默認首頁文件

? ? ? ? }

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

root /opt/tools/media/flv;#目標flv格式video文件夾位置,需手動創建此文件夾,并將對應格式的視頻文件放在此文件夾下

}

location ~ \.mp4$ {

root /opt/tools/media/mp4;#目標mp4格式video文件夾位置

}

location /live{

flv_live on;

chunked_transfer_encoding? on;

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

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、? ? 直播測試

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

對應播放地址:http://172.16.1.97:8081/live?port=1935&app=myapp&stream=123

11、? ? ? ? 點播測試

http:172.16.1.97:8088/test.flv

http:172.16.1.97:8088/1.mp4

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,460評論 6 538
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,067評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,467評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,468評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,184評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,582評論 1 325
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,616評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,794評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,343評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,096評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,291評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,863評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,513評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,941評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,190評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,026評論 3 396
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,253評論 2 375