1. 程式人生 > >nginx-rtmp-module搭建流媒體服務器

nginx-rtmp-module搭建流媒體服務器

復制 file ftp fault ces 只需要 record chunk inf

nginx搭建流媒體服務器

進入新建的文件夾prog

1、下載nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git

2、下載nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz

3、下載nginx依賴模塊
gzip模塊需要 zlib 庫、rewrite模塊需要 pcre 庫、ssl 功能需要openssl庫

①安裝pcre,只需要裝開發庫libpcre3-dev
sudo apt install libpcre3 libpcre3-dev

②安裝openssl,只需要裝個開發庫
sudo apt install openssl libssl-dev


(這裏選的是1.14.0版本,選擇其他穩定版本可直接到官網下載 http://nginx.org/en/download.html)
也可以到https://ftp.pcre.org/pub/pcre/獲取pcre編譯安裝包當前最新的版本
在http://www.openssl.org/source/上可以獲取openssl當前最新的版本
獲取zlib編譯安裝包,在http://www.zlib.net/上可以獲取當前最新的版本

4、安裝nginx

tar -zxvf nginx-1.8.1.tar.gz
cd nginx-1.8.1
(運行configure,prefix後面接你的nginx安裝目錄,添加模塊接第三方nginx-rtmp-module模塊所在目錄)
./configure --prefix=/usr/local/nginx --add-module=/home/yl/prog/nginx-rtmp-module --with-http_ssl_module
make
sudo make install

5、修改nginx配置文件

#user nobody; #配置用戶或者組,默認為nobody nobody
worker_processes 1; #允許生成的進程數,默認為1

error_log logs/error.log; #制定日誌路徑,級別
error_log logs/error.log notice;
error_log logs/error.log info;

#pid logs/nginx.pid; #指定nginx進程運行文件存放地址

events { #events塊:配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網絡連接序列化等
worker_connections 1024; #最大連接數,默認為512
}

http { #http塊:可以嵌套多個server,配置代理,緩存,日誌定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日誌自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等
include mime.types; #文件擴展名與文件類型映射表
default_type application/octet-stream; #默認文件類型,默認為text/plain

sendfile on; #允許sendfile方式傳輸文件,默認為off,可以在http塊,server塊,location塊。
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65; #連接超時時間,默認為75s,可以在http,server,location塊。

#gzip on;

server { #server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
listen 8080; #監聽端口
server_name 192.168.1.244; #監聽地址

#charset koi8-r;

#access_log logs/host.access.log main;
location / { #location塊:配置請求的路由,以及各種頁面的處理情況
add_header Cache-Control no-cache;
root html; #根目錄
index index.html index.htm; #設置默認頁
}

location /stat {
rtmp_stat all; #為當前 HTTP location 設置 RTMP statistics 處理程序。RTMP statistics 是一個靜態的 XML 文檔。可以使用 rtmp_stat_stylesheet 指令在瀏覽器中作為 XHTML 頁面查看這個文檔
rtmp_stat_stylesheet stat.xsl; #添加 XML 樣式表引用到 statistics XML 使其可以在瀏覽器中可視。
}
location /stat.xsl {
root /home/tqhy/prog/nginx-rtmp-module; #stat.xsl所在目錄位置,該文件可復制
}
# location /control {
# rtmp_control all;
# }
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /home/tqhy/obs/hls;
expires -1;
}
# location ~\.flv {
# flv;
# }
# location ~\.mp4$ {
# mp4;
# }

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

}
rtmp{ #保存所有 RTMP 配置的塊
server { #聲明一個 RTMP 實例
listen 1935; #監聽的端口,以接收rtmp連接

chunk_size 4000; #流整合的最大的塊大小。默認值為 4096。這個值設置的越大 CPU 負載就越小。這個值不能低於 128

application live { #rtmp推流請求路徑,創建一個 RTMP 應用
live on; #切換直播模式,即一對多廣播

record all; #切換錄制模式。流可以被記錄到 flv 文件, all - 音頻和視頻(所有)
record_path /home/tqhy/obs/live; #指定錄制的 flv 文件存放目錄。
#record_max_size 200M; #設置錄制文件的最大值

hls on; #在 application 切換 HLS
hls_path /home/tqhy/obs/hls; #設置 HLS 播放列表和分段目錄。這一目錄必須在 NGINX 啟動前就已存在
hls_fragment 1s; #設置 HLS 分段長度。默認為 5 秒鐘
hls_playlist_length 5; #設置 HLS 播放列表長度。默認為 30 秒鐘

allow play all; #允許來自指定地址或者所有地址發布/播放。allow 和 deny 指令的先後順序可選
}

application vod {
play /home/tqhy/obs/vod; #同一個 play 指令可以定義多個播放地址。當多個 play 指令定義時,地址列表將被合並,並進行從更高域中繼承。嘗試播放每一個地址,直到發現一個成功的地址。如果沒有找到成功地址,將發送錯誤狀態到客戶端。
索引的 FLV 播放具有隨機查找能力。沒有索引的 FLV 則不具備查找/暫停能力(重播模式)。使用 FLV 索引器(比如 yamdi)來編索引。
mp4 文件只有在音頻和視頻編碼都被 RTMP 支持時才可以播放。最常見的情況是 H264/AAC。
}
}
}


Nginx rtmp模塊nginx-rtmp-module指令詳解
https://www.cnblogs.com/yisionwa/p/5882585.html


然後啟動nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nginx-rtmp-module搭建流媒體服務器