前提:一臺已經安裝好系統的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