服務器環境
服務器OS:CentOS Linux release 7.2.1511
nginx版本:1.14.1
nginx-rtmp-module:基於Nginx的開源流媒體服務器
安裝nginx + nginx-rtmp-module
到nginx官網官網下載最新的源碼包,到nginx-rtmp-module項目地址下載最新源碼
編譯安裝nginx
,注意在參數里指定nginx-rtmp-module
:
# cd /software
# rz nginx-1.14.1.tar.gz
# tar xf nginx-1.14.1.tar.gz
# git clone https://github.com/arut/nginx-rtmp-module.git
# cd nginx-1.14.1
# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/soft/nginx-rtmp-module
# make && make install
/soft/nginx-rtmp-module 改為服務器存放的實際地址
配置nginx虛擬主機
在nginx.conf
中添加rtmp
服務配置:
# vim /usr/local/nginx/conf/nginx.conf
rtmp {
server {
listen 1935; #監聽的端口
chunk_size 4096;
application hls {
live on;
hls on;
hls_path /usr/local/nginx/html/hls; #視頻流文件目錄(自己創建)
hls_fragment 3s;
}
}
}
參數說明
- rtmp 是協議名稱
- server 說明內部中是服務器的相關配置
- listen 監聽的端口,rtmp協議的默認端口為1935
- application 訪問的應用路徑是hls
- live on 啟用rtmp直播
- record off 不記錄數據
- hls on 啟用hls直播
- hls_path 切片保存位置
- hls_fragment 每個切片的長度
配置直播,hls支持以及狀態監控頁面
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /usr/local/nginx/html/hls; #視頻流文件目錄(自己創建)
expires -1;
add_header Cache-Control no-cache;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/extend_module/nginx-rtmp-module/;
}
}
}
nginx最終配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /usr/local/nginx/html/hls; #視頻流文件目錄(自己創建)
expires -1;
add_header Cache-Control no-cache;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/extend_module/nginx-rtmp-module/;
}
}
}
rtmp {
server {
listen 1935;
chunk_size 4096;
application hls {
live on;
hls on;
hls_path /usr/local/nginx/html/hls; #視頻流文件目錄(自己創建)
hls_fragment 3s;
}
}
}
nginx常用操作
# cp /usr/local/nginx/sbin/nginx /usr/local/bin/ #把nginx加入到環境變量里,不用輸入全路徑了
# nginx #第一次啟動
# nginx -t #檢查配置文件是否正確
# nginx -s reload #平滑重啟,修改配置文件后,不斷服務重啟
# nginx -s stop #停止服務
客戶端推送
直播推流端使用rtmp協議推流,端口為1935。URL格式為:rtmp://ip:端口/hls。推流軟件推薦使用開源的OBS。
流名稱要與寫的觀看直播的頁面中的xxxx.m3u8名稱一致
查看直播
瀏覽器輸入http:/xx.xx.xx.xx/hls/test.m3u8
就能看直播了