1、介紹
nginx-http-flv-module是在nginx-rtmp-module基礎上開發的一個直播模塊。
感謝Arut創造了nginx-rtmp-module,它是Nginx的一個優秀的第三方模塊,可以用來直播,支持RTMP,HLS和DASH方式直播,還支持調用第三方軟件進行轉碼,錄制視頻等功能,由於依托Nginx,性能也比較高。但是美中不足的地方也不少,例如首屏時間長,不支持HTTP-FLV方式直播,不支持虛擬主機(vhost)功能,省略listen配置無法接受連接,有很多很明顯的bug等問題。
nginx-http-flv-module解決了上述的問題。當然,還有些bug還有待修復。具體使用方法請參考說明文件:
為什么選擇Nginx作為支持HTTP-FLV方式直播的服務器呢?因為Nginx的Web服務器功能對HTTP協議的支持非常完善,Nginx的性能優秀,經過了很多場景的檢驗。另外,Nginx本身對第三方軟件的依賴較少,非常易於部署。這些都使得它成為HTTP-FLV方式直播服務器不可多得的選擇。
1.1 nginx-http-flv-module的功能
- 兼容nginx-rtmp-module所有功能,基於nginx-rtmp-module的流媒體服務器。
- 支持HTTP-FLV方式的直播
- 支持GOP緩存,以減少首屏時間
- 支持虛擬主機功能
- 可以省略listen配置項而不影響基本功能
- 修復nginx-rtmp-module已知的bug
1.2 nginx-http-flv-module 安裝配置
Github:nginx-http-flv-module
https://github.com/winshining/nginx-http-flv-module
查看英文介紹文檔 :English README。
1.3 功能介紹
-
nginx-rtmp-module提供的所有功能。
-
nginx-http-flv-module的其他功能與nginx-rtmp-module的對比:
功能 | nginx-http-flv-module | nginx-rtmp-module | 備注 |
---|---|---|---|
HTTP-FLV (播放) | √ | x | 支持HTTPS-FLV和chunked回復 |
GOP緩存 | √ | x | |
虛擬主機 | √ | x | |
省略listen 配置 |
√ | 見備注 | 配置中必須有一個listen |
純音頻支持 | √ | 見備注 | wait_video 或wait_key 開啟后無法工作 |
定時打印訪問記錄 | √ | x | |
JSON風格的stat | √ | x |
- NGINX的版本應該大於1.2.6,與其他版本的兼容性未知。
- Linux(推薦)/FreeBSD/MacOS/Windows(受限)
- 支持的播放器 VLC (RTMP & HTTP-FLV)/OBS (RTMP & HTTP-FLV)/JW Player (RTMP)/flv.js (HTTP-FLV).
- flv.js只能運行在支持Media Source Extensions的瀏覽器上
1.4依賴環境
-
在類Unix系統上,需要GNU make,用於調用編譯器來編譯軟件。
-
在類Unix系統上,需要GCC。或者在Windows上,需要MSVC,用於編譯軟件。
-
在類Unix系統上,需要GDB,用於調試軟件(可選)。
-
如果NGINX要支持正則表達式,需要PCRE庫。
-
如果NGINX要支持加密訪問,需要OpenSSL庫。
-
如果NGINX要支持壓縮,需要zlib庫。
2 安裝
2.1 RHEL/CentOS 6, 7
在這些操作系統上,最新發布且適配最新穩定版NGINX的模塊可以通過以下方式獲取:
yum install https://extras.getpagespeed.com/release-el$(rpm -E %{rhel})-latest.rpm
yum install nginx-module-flv
2.2 RHEL 8
dnf install https://extras.getpagespeed.com/release-el$(rpm -E %{rhel})-latest.rpm
sudo dnf --disablerepo=rhel-8-for-x86_64-appstream-rpms install nginx-module-flv
安裝完畢后,HTTP-FLV功能的配置文件http-flv.conf
和RTMP功能的配置文件rtmp.conf
會被放在/etc/nginx/http-flv
目錄下,通過include
手工將它們添加到/etc/nginx/nginx.conf
,以開啟HTTP-FLV和RTMP功能:
http {
...
include /etc/nginx/http-flv/http-flv.conf;
}
include /etc/nginx/http-flv/rtmp.conf;
添加以下配置到/etc/nginx/nginx.conf
,啟動或者重啟NGINX來啟用本模塊:
load_module modules/ngx_http_flv_live_module.so;
2.3 注意
上述的配置必須位於events
配置項之前,否則NGINX不能啟動。
更新可以通過yum update
來完成。關於其他NGINX模塊的詳情見GetPageSpeed。
對於其他操作系統,見下面源碼編譯安裝的說明。
3 源碼編譯安裝
3.1 注意
nginx-http-flv-module包含了nginx-rtmp-module所有的功能,所以不要將nginx-http-flv-module和nginx-rtmp-module一起編譯。
3.2 在Windows上
編譯步驟請參考Building nginx on the Win32 platform with Visual C,不要忘了在Run configure script
步驟中添加--add-module=/path/to/nginx-http-flv-module
。
3.3 在類Linux系統上
下載NGINX和nginx-http-flv-module。
將它們解壓到某一路徑 /path/to/nginx-http-flv-module。
打開NGINX的源代碼路徑(/usr/local/nginx )並執行:
將模塊編譯進NGINX,要到/usr/local/nginx
./configure --add-module=/path/to/nginx-http-flv-module
make
make install
或者
將模塊編譯為動態模塊
./configure --add-dynamic-module=/path/to/nginx-http-flv-module
make
make install
操作日志
[root@ip104 tengine-2.3.2]# ./configure --add-dynamic-module=/path/to/nginx-http-flv-module
checking for OS
+ Linux 3.10.0-957.21.3.el7.x86_64 x86_64
checking for C compiler ... found
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@ip104 tengine-2.3.2]#
4 使用方法
關於nginx-rtmp-module用法的詳情,請參考README.md。
4.1 發布 推流服務
為了簡單起見,不用轉碼:
ffmpeg -re -i MEDIA_FILE_NAME -c copy -f flv rtmp://example.com[:port]/appname/streamname
4.2 注意
-
一些舊版本的FFmpeg不支持選項
-c copy
,可以使用選項-vcodec copy -acodec copy
替代。 -
appname
用於匹配rtmp配置塊中的application塊(更多詳情見下文)。 -
streamname
可以隨意指定,但是不能省略。 -
RTMP默認端口為1935,如果要使用其他端口,必須指定
:port
。
4.3 播放
HTTP-FLV方式
http://example.com[:port]/dir?[port=xxx&]app=appname&stream=streamname
4.4 注意
-
如果使用ffplay命令行方式播放流,那么必須為上述的url加上引號,否則url中的參數會被丟棄(有些不太智能的shell會把"&"解釋為"后台運行")。
-
如果使用flv.js播放流,那么請保證發布的流被正確編碼,因為flv.js只支持H.264編碼的視頻和AAC/MP3編碼的音頻。
-
參數
dir
用於匹配http配置塊中的location塊(更多詳情見下文)。 -
HTTP默認端口為80, 如果使用了其他端口,必須指定
:port
。 -
RTMP默認端口為1935,如果使用了其他端口,必須指定
port=xxx
。 -
參數
app
的值(appname)用來匹配application塊,但是如果請求的app
出現在多個server塊中,並且這些server塊有相同的地址和端口配置,那么還需要用匹配主機名的server_name
配置項來區分請求的是哪個application塊,否則,將匹配第一個application塊。 -
參數
stream
的值(streamname)用來匹配發布的流的名稱。
例子
假設在http
配置塊中的listen
配置項是:
http {
...
server {
listen 8080; #不是默認的80端口
...
location /live {
flv_live on;
}
}
}
在rtmp
配置塊中的listen
配置項是:
rtmp {
...
server {
listen 1985; #不是默認的1935端口
...
application myapp {
live on;
}
}
}
並且發布的流的名稱是mystream
,那么基於HTTP的播放url是:
http://example.com:8080/live?port=1985&app=myapp&stream=mystream
注意
由於一些播放器不支持HTTP塊傳輸, 這種情況下最好在指定了flv_live on;
的location中指定chunked_transfer_encoding off
,否則播放會失敗。
RTMP方式
rtmp://example.com[:port]/appname/streamname
HLS方式
http://example.com[:port]/dir/streamname.m3u8
DASH方式
http://example.com[:port]/dir/streamname.mpd
示例圖片
RTMP (JW Player) & HTTP-FLV (VLC)
HTTP-FLV (flv.js)
nginx.conf實例
注意
配置項rtmp_auto_push
,rtmp_auto_push_reconnect
和rtmp_socket_dir
在Windows上不起作用,除了Windows 10 17063以及后續版本之外,因為多進程模式的relay
需要Unix domain socket的支持,詳情請參考Unix domain socket on Windows 10。
最好將配置項worker_processes
設置為1,因為在多進程模式下,ngx_rtmp_stat_module
可能不會從指定的worker進程獲取統計數據,因為HTTP請求是被隨機分配給worker進程的。ngx_rtmp_control_module
也有同樣的問題。這個問題可以通過這個補丁per-worker-listener優化。
另外,vhost
功能在多進程模式下還不能完全正確運行,等待修復。例如,不管向哪個域名推流,下面的配置在多進程模式下是沒有問題的:
rtmp {
...
server {
listen 1935;
server_name 1st_domain_name;
application myapp {
...
}
}
server {
listen 1935;
server_name 2nd_domain_name;
application myapp {
...
}
}
}
而使用下面的配置,當publisher在端口1945上發布媒體流,播放請求在此端口上訪問非publisher的worker進程時是有問題的:
rtmp {
...
server {
listen 1935;
server_name 1st_domain_name;
application myapp {
...
}
}
server {
listen 1945;
server_name 2nd_domain_name;
application myapp {
...
}
}
}
配置實例
worker_processes 1; #運行在Windows上時,設置為1,因為Windows不支持Unix domain socket
#worker_processes auto; #1.3.8和1.2.5以及之后的版本
#worker_cpu_affinity 0001 0010 0100 1000; #只能用於FreeBSD和Linux
#worker_cpu_affinity auto; #1.9.10以及之后的版本
error_log logs/error.log error;
#如果此模塊被編譯為動態模塊並且要使用與RTMP相關的功
#能時,必須指定下面的配置項並且它必須位於events配置
#項之前,否則NGINX啟動時不會加載此模塊或者加載失敗
#load_module modules/ngx_http_flv_live_module.so;
events {
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80;
location / {
root /var/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /live {
flv_live on; #打開HTTP播放FLV直播流功能
chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回復
add_header 'Access-Control-Allow-Origin' '*'; #添加額外的HTTP頭
add_header 'Access-Control-Allow-Credentials' 'true'; #添加額外的HTTP頭
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /stat {
#push和pull狀態的配置
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /var/www/rtmp; #指定stat.xsl的位置
}
#如果需要JSON風格的stat, 不用指定stat.xsl
#但是需要指定一個新的配置項rtmp_stat_format
#location /stat {
# rtmp_stat all;
# rtmp_stat_format json;
#}
location /control {
rtmp_control all; #rtmp控制模塊的配置
}
}
}
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模塊在access.log中記錄日志的間隔時間,對調試非常有用
log_size 1m; #log模塊用來記錄日志的緩沖區大小
server {
listen 1935;
server_name www.test.*; #用於虛擬主機名后綴通配
application myapp {
live on;
gop_cache on; #打開GOP緩存,減少首屏等待時間
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
server {
listen 1935;
server_name *.test.com; #用於虛擬主機名前綴通配
application myapp {
live on;
gop_cache on; #打開GOP緩存,減少首屏等待時間
}
}
server {
listen 1935;
server_name www.test.com; #用於虛擬主機名完全匹配
application myapp {
live on;
gop_cache on; #打開GOP緩存,減少首屏等待時間
}
}
}
events {
worker_connections 1024; #Nginx處理的最大連接數
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
server {
listen 80; #Nginx監聽的HTTP請求端口
location / {
root /var/www; #HTTP請求URL映射到服務器的位置
index index.html index.htm; #HTTP請求優先請求的文件,如http://localhost/,如果有index.html在/var/www目錄下,那么請求的是/var/www/index.html
}
error_page 500 502 503 504 /50x.html; #如果遇到這些HTTP請求錯誤,Nginx返回50x.html的內容
location = /50x.html {
root html; #因為/配置了root /var/www,所以這兒html對應的是/var/www/html,所以50x.html的路徑是/var/www/html/50x.html
}
location /live {
flv_live on; #當HTTP請求以/live結尾,匹配這兒,這個選項表示開啟了flv直播播放功能
chunked on; #HTTP協議開啟Transfer-Encoding: chunked;方式回復,已廢棄,使用標准的chunked_tranfer_encoding代替
}
}
}
rtmp_auto_push on; #因為Nginx可能開啟多個子進程,這個選項表示推流時,媒體流會發布到多個子進程
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp; #多個子進程情況下,推流時,最開始只有一個子進程在競爭中接收到數據,然后它再relay給其他子進程,他們之間通過unix domain socket傳輸數據,這個選項表示unix domain socket的路徑
rtmp {
out_queue 4096;
out_cork 8;
max_streams 64; #Nginx能接受的最大的推流數
server {
listen 1935; #Nginx監聽的RTMP推流/拉流端口,可以省略,默認監聽1935
application myapp {
live on; #當推流時,RTMP路徑中的APP(RTMP中一個概念)匹配myapp時,開啟直播
gop_cache on; #開啟GOP(Group of Picture)緩存,播放器解碼時,收到一個完整的GOP才會開始播放,這個是減少播放延遲的選項
pull rtmp://live.hkstv.hk.lxdns.com/live/hks; #如果懶得推流,那可以用這個,香港衛視的直播推流
}
#以下配置項已廢棄
application app1 {
proxy_pass rtmp://host(ip or domain name)[:host]/app2; #將推流反向代理到上游服務器,並將app1自動轉化為app2
#proxy_pass rtmp://backend; #將推流反向代理到上游服務器,見upstream配置
}
}
server {
listen 1935;
server_name *.test.com; #或者www.test.*/www.test.com
application myapp {
live on;
gop_cache on;
}
}
#以下配置項已廢棄,原因在下文更新中
upstream backend {
#開啟負載均衡
server host1:port1;
server host2:port2;
}
}
基本配置完成了,一下是我自己的配置文件
#aaa
#工作進程
worker_processes 2;
#worker_processes auto; #1.3.8和1.2.5以及之后的版本
#worker_cpu_affinity 0001 0010 0100 1000; #只能用於FreeBSD和Linux
#worker_cpu_affinity auto; #1.9.10以及之后的版本
#日志功能
error_log logs/error.log error;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log "pipe:rollback logs/error_log interval=1d baknum=7 maxsize=2G";
#pid logs/nginx.pid;
#如果此模塊被編譯為動態模塊並且要使用與RTMP相關的功
#能時,必須指定下面的配置項並且它必須位於events配置
#項之前,否則NGINX啟動時不會加載此模塊或者加載失敗
#load_module modules/ngx_http_flv_live_module.so;
#事件配置
events {
worker_connections 1024;
}
# load modules compiled as Dynamic Shared Object (DSO)
#
#dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
#}
#RTMP配置
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模塊在access.log中記錄日志的間隔時間,對調試非常有用
log_size 1m; #log模塊用來記錄日志的緩沖區大小
# 用於是直播的服務 1935端口 推送地址:rtmp://39xxxxxxx.120:1935/hls 獲取地址:http://live.xxxxxxxx.com/hls/222.m3u8
server {
listen 1935;
server_name live.xxxxx.com;
#server_name 1935.xxxxxx.com; #用於虛擬主機名后綴通配
#chunk_size 4000; #快大小
#電視模式
application mytv {
live on; #enable live streaming
#record first 1K of stream
record all;
record_path /tmp/av;
record_max_size 1K;
#append current timestamp to each flv
record_unique on;
#publish only from localhost
allow publish 127.0.0.1;
deny publish all;
#allow play all;
}
application myapp {
live on;
gop_cache on; #打開GOP緩存,減少首屏等待時間
}
#hls配置
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
#可以多個server的
}
# HTTP 相關網絡請求配置
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
#access_log "pipe:rollback logs/access_log interval=1d baknum=7 maxsize=2G" main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# 用於,net core 相關網站的服務
server {
listen 80;
server_name www.xxxxxxxx.com
#server_name xxxxxx.com *.xxxxxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
#access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main;
location / {
root html;
index index.html index.htm;
#proxy_pass http://localhost:5000;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection keep-alive;
#proxy_set_header Host $host;
# proxy_cache_bypass $http_upgrade;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#用於視頻直播服務先關的http 解析服務 add 2019-7-8
server {
listen 80;
server_name live.xxxxxxxxxx.com;
location / {
#index /stat;
proxy_pass http://localhost:1936;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /live {
flv_live on; #打開HTTP播放FLV直播流功能
chunked_transfer_encoding on; #支持'Transfer-Encoding: chunked'方式回復
add_header 'Access-Control-Allow-Origin' '*'; #添加額外的HTTP頭
add_header 'Access-Control-Allow-Credentials' 'true'; #添加額外的HTTP頭
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /dash {
root /tmp;
add_header 'Cache-Control' 'no-cache';
}
location /stat {
#push和pull狀態的配置
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx-http-flv-module; #指定stat.xsl的位置
}
#如果需要JSON風格的stat, 不用指定stat.xsl
#但是需要指定一個新的配置項rtmp_stat_format
#location /stat {
# rtmp_stat all;
# rtmp_stat_format json;
#}
location /control {
rtmp_control all; #rtmp控制模塊的配置
}
}
#測試服務器使用
server {
listen 80;
server_name 8056.xxxxxxxxxxx.com;
#server_name 1123.xxxxxxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
#access_log "pipe:rollback logs/host.access_log interval=1d baknum=7 maxsize=2G" main;
location / {
root html;
index 404.html 404.htm;
proxy_pass http://localhost:8056;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection keep-alive;
#proxy_set_header Host $host;
# proxy_cache_bypass $http_upgrade;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header X-Forwarded-Proto $scheme;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}