1.核心配置
rtmp
????????保存所有RTMP設置的塊
server
????????聲明RTMP服務實例,語法server { ... }
rtmp {server {}
}
listen
????????為NGINX添加監聽套接字以接受RTMP連接。語法:?listen (addr[:port]|port|unix:path) [bind] [ipv6only=on|off][so_keepalive=on|off|keepidle:keepintvl:keepcnt|proxy_protocol]
server {listen 1935;
}
application
????????創建RTMP應用。語法:application name { ... }
server {listen 1935;application myapp {}
}
timeout
????????套接字超時。該值主要用于寫。大多數時候,RTMP模塊不期望在除發布者套接字之外的所有套接字上有任何活動。如果你想讓損壞的套接字快速斷開連接,請使用keepalive或RTMP ping等活動工具。默認值為1分鐘。語法:?timeout value
timeout 60s;
ping|ping_timeout
????????RTMP ping間隔。0關閉ping。RTMP ping是用于主動連接檢查的協議功能。一個特殊的數據包被發送到遠程對等端,預計會在用ping_timeout指令指定的超時內收到回復。如果在此時間內未收到ping回復,則關閉連接。ping的默認值為1分鐘。默認ping超時為30秒。
ping 3m;
ping_timeout 30s;
max_streams
????????設置RTMP流的最大數量。數據流被復用為單個數據流。不同的通道用于發送命令、音頻、視頻等。默認值為32,這在許多情況下是夠用的。
max_streams 32;
ack_window
????????設置RTMP確認窗口大小。它是接收到的字節數,在該字節數之后,對等方應向遠程側發送確認包。默認值為5000000。
ack_window 5000000;
chunk_size
????????流多路復用的最大塊大小,此值不能小于128,默認值為4096。值越大,CPU開銷越低。
chunk_size 4096;
max_queue|max_message
????????輸入數據消息的最大大小。所有輸入數據都被拆分為消息(并進一步拆分為塊)。部分消息在等待完成時保留在內存中。理論上,傳入消息可能非常大,這可能會對服務器穩定性造成問題。默認值1M在許多情況下就足夠了。
max_message 1M;
buflen
??設置默認緩沖區長度。通常客戶端在播放之前發送RTMP set_buflen命令并重置此設置。默認值為1000毫秒。
buflen 5s;
out_queue|out_cork
2.訪問
allow|deny
????????允許從指定地址或所有地址發布/播放。允許/拒絕指令按出現順序進行檢查。語法:?allow|deny [play|publish] address|subnet|all
allow publish 127.0.0.1;
deny publish all;
allow play 192.168.0.0/24;
deny play all;
3.執行指令
exec_push
? ? ? ? 語法:exec_push command arg*
????????指定要在每個發布的流上執行的帶有參數的外部命令。發布停止時,進程終止。第一個參數為二進制文件的完整路徑。對于這個過程應該做什么,沒有任何假設。這個功能在ffmpeg流轉碼中很有用。FFmpeg應該作為客戶端連接到nginx-rtmp,并將轉碼后的流作為發布者輸出回nginx rtmp。可以在命令行中使用$var/${var}形式的替換:
- $name - stream name
- $app - application name
- $addr - client address
- $flashver - client flash version
- $swfurl - client swf url
- $tcurl - client tc url
- $pageurl - client page url
????????Shell腳本重定向可以在exec_push指令中指定,用于寫入輸出和接受輸入。確保重定向字符和流名稱/編號之間沒有空格。
支持的有
- truncating output?
>file
- appending output?
>>file
- descriptor redirects like?
1>&2
- input?
<file
Make sure there's no space between redirection character and stream name/number.
????????您可以指定要執行的命令的完整路徑或短命令名。在后一種情況下,在PATH環境變量指定的目錄中查找二進制文件。默認情況下,nginx會清除環境,這通常會使rtmp模塊僅運行位于/bin和/usr/bin等標準目錄中的二進制文件。要使其始終有效,請使用以下nginx指令保留原始PATH變量值。
env PATH;
????????以下ffmpeg調用將傳入流轉碼為HLS就緒流(H264/AAC)
application src {live on;exec_push ffmpeg -i rtmp://localhost/src/$name -vcodec libx264 -vprofile baseline -g 10 -s 300x200 -acodec aac -ar 44100 -ac 1 -f flv rtmp://localhost/hls/$name 2>>/var/log/ffmpeg-$name.log;
}application hls {live on;hls on;hls_path /tmp/hls;hls_fragment 15s;
}
exec_pull
????????語法:exec_pull command arg*
????????指定要在播放事件上執行的帶有參數的外部命令。當第一個客戶端連接到流時執行該命令,當最后一個客戶端斷開連接時終止該命令。此指令允許為本地客戶端提取任何格式的遠程流。
????????該功能僅在單工模式下可靠工作。原因是我們無法確保外部進程始終連接到正確的worker。它顯然會連接到一個隨機的。雖然這在大多數情況下仍然有效,但它不是一個推薦的架構,它將是不穩定和有缺陷的。
application myapp {live on;exec_pull ffmpeg -i http://example.com/video_$name.ts -c copy -f flv rtmp://localhost/$app/$name;
}
????????在上述配置中,exec_pull指令為所有流提供服務。這導致了遠程流名稱格式的某些限制。應該可以使用$app、$name等可用變量構造遠程url。當不可能時,您可以在指令上添加exec_options,這允許在exec系列指令中設置其他流選項。現在唯一支持的選項是名稱選項。
application myapp {live on;exec_options on;exec_pull ffmpeg -i http://example.com/tv1.ts -c copy -f flv rtmp://localhost/$app/$name name=mystream;exec_pull ffmpeg -i http://another.example.com/video_plus.ts -c copy -f flv rtmp://localhost/$app/$name name=anotherstream;
}
exec
? ? ? ? exec是exec_push的別名
exec_options
????????該指令切換exec選項模式。激活后,您可以添加exec家族指令選項。唯一支持的exec選項是name。此選項允許僅對指定流應用exec。默認設置為禁用。語法:?exec_options on|off
exec_options on;
# call my_on_publish_command only for "mystream"
exec_publish my_on_publish_command name=mystream;# call my_on_play_command only for "another"
exec_play my_on_play_command name=another;# execute different ffmpeg's for different streams
exec_pull ffmpeg -i http://example.com/abc.ts -c copy -f flv rtmp://localhost/$name/$app name=mystream;
exec_pull ffmpeg -i http://my.example.com/tele.ts -c copy -f flv rtmp://localhost/$name/$app name=tv;
exec_pull ffmpeg -i http://another.example.com/hello/f.ts -c copy -f flv rtmp://localhost/$name/$app name=fun;
exec_static
????????語法:?exec_static command arg*
????????類似于exec,但在nginx啟動時運行指定的命令。不支持替換,因為沒有會話上下文。
exec_static ffmpeg -i http://example.com/video.ts -c copy -f flv rtmp://localhost/myapp/mystream;
exec_kill_signal
????????語法:?exec_kill_signal signa
????????設置進程終止信號。默認值為kill(SIGKILL),可以指定數字或符號名稱。
exec_kill_signal term;
exec_kill_signal usr1;
exec_kill_signal 3;
respawn
????????語法:respawn on|off
? ? ? ? 當終止子進程時,如果發布處于打開狀態時,將啟用respawns子進程。默認值為打開;
respawn off;
respawn_timeout
????????語法:?respawn_timeout timeout
????????將
respawn超時設置為在啟動新的子實例之前等待。默認值為5秒。
respawn_timeout 10s;
exec_publish
????????語法:?exec_publish command arg*
????????指定在發布事件時,執行的帶有參數的外部命令。不分析返回代碼。這里也支持exec的替換。此外,支持args變量保存查詢字符串參數。????????
exec_play
????????語法:??exec_play command arg*
????????指定要在播放事件上執行的帶有參數的外部命令。不分析返回代碼。
exec_play_done
????????語法:??exec_play_done command arg*
????????指定要在play_done事件上執行的帶參數的外部命令。不分析返回代碼。
exec_publish_done
????????語法:??exec_publish_done command arg*
????????指定要在publish_done事件上執行的帶參數的外部命令。不分析返回代碼。
exec_record_done
????????語法:?exec_record_done command arg*
????????指定錄制完成時要執行的帶參數的外部命令。這里支持替換exec_publish以及其他變量
recorder
?- recorder namepath
?- 錄制文件完整路徑 (/tmp/rec/mystream-1389499351.flv
)filename
?-省略目錄的路徑 (mystream-1389499351.flv
)basename
?- 省略擴展名的文件名 (mystream-1389499351
)dirname
?- 目錄的路徑 (/tmp/rec
)
例子
# track client info
exec_play bash -c "echo $addr $pageurl >> /tmp/clients";
exec_publish bash -c "echo $addr $flashver >> /tmp/publishers";# convert recorded file to mp4 format
exec_record_done ffmpeg -y -i $path -acodec libmp3lame -ar 44100 -ac 1 -vcodec libx264 $dirname/$basename.mp4;
4.直播
live
????????語法:?live on|off
????????切換直播模式,即一對多廣播。
live on;
meta
????????語法:meta on|copy|off
????????設置元數據發送模式。on的值使訂閱者接收到包含預定義字段(如寬度、高度等)的重建元數據包。copy的值使客戶端收到發布者元數據塊的精確副本,包括標準和特定字段。off的值將關閉向訂閱者發送任何RTMP元數據的功能。默認為on。????????
meta copy;
interleave
????????語法:?interleave on|off
????????切換交錯模式。在此模式下,音頻和視頻數據在同一RTMP塊流上傳輸。默認為關閉。
interleave on;
wait_key
????????語法:?wait_key on|off
????????使視頻流從關鍵幀開始,默認為關閉。
wait_key on;
wait_video
????????語法:??wait_video on|off
????????在發送第一個視頻幀之前禁用音頻。默認為關閉。可以與wait_key結合使用,使客戶端接收視頻關鍵幀及其后的所有其他數據。但是,這通常會增加連接延遲。您可以調整編碼器中的關鍵幀間隔以減少延遲。
????????注意:最近版本的IE需要啟用此選項才能正常播放。
wait_video on;
publish_notify
????????語法:?publish_notify on|off
????????發送NetStream.Play.PublishNotify
和?NetStream.Play.UnpublishNotify
通知訂閱者。默認為關閉。
publish_notify on;
drop_idle_publisher
????????語法:?drop_idle_publisher timeout
????????斷開在指定時間內處于空閑狀態(沒有音頻/視頻數據)的發布者連接。默認設置為關閉。請注意,這僅在連接處于發布模式時(發送發布命令后)有效。
drop_idle_publisher 10s;
sync
????????語法:?sync timeout
????????同步音頻和視頻流。若訂戶帶寬不足以以發布者速率接收數據,則服務器會丟棄一些幀。這會導致同步問題。當時間戳差異超過指定為同步參數的值時,會發送一個絕對幀來修復該問題。默認值為300ms
sync 10ms;
play_restart
????????語法:?play_restart on|off
????????如果啟用,在每次發布者發布或停止時,
nginx-rtmp將向訂閱者發送
NetStream.Play.Start 和
NetStream.Play.Stop。如果禁用,則每個訂閱者僅在播放開始和結束時收到這些通知。默認設置為off。
play_restart off;
idle_streams
????????語法:?idle_streams on|off
????????如果禁用,nginx-rtmp會阻止訂閱者連接到空閑/不存在的直播流,并在流發布者斷開連接時斷開所有訂閱者的連接。默認設置為啟用。
idle_streams off;
5.錄制
record
????????語法:?record [off|all|audio|video|keyframes|manual]*
????????切換錄制模式。流可以記錄在flv文件中。該指令詳細說明了應記錄的內容:
- off - 不錄制
- all - audio & video (所有)
- audio - 音頻
- video - 視頻
- keyframes - 僅僅關鍵視頻幀
- manual - 不自動啟動錄制,使用控制界面啟動/停止
在單個記錄指令中可以有任何兼容的鍵組合。
record all;record audio keyframes;
record_path
????????指定錄制flv文件的存放路徑。
record_path /tmp/rec;
record_suffix
????????設置錄制文件的后綴,默認為“.flv”。
record_suffix _recorded.flv;
????????記錄后綴可以是strftime格式的模式。如以下指令?將生成格式為mystream-24-Apr-13-18:23:38.flv的文件
record_suffix -%d-%b-%y-%T.flv;
record_unique
????????語法:record_unique on|off
????????如果啟用,則會將當前時間戳附加到錄制的文件中。否則,每次進行新的錄制時,都會重寫相同的文件。默認設置為關閉。
record_unique on;
record_append
????????語法:record_append on|off
????????切換文件追加模式。打開時,記錄器會將新數據附加到舊文件中,或在丟失時創建新數據。文件中的舊數據和新數據之間沒有時間間隔。默認設置為關閉。
record_append on;
record_lock
????????語法:record_lock on|off
????????打開時,當前錄制的文件會被fcntl調用鎖定。可以從其他地方檢查,以找出正在錄制的文件。默認設置為關閉。
record_lock on;
????????在FreeBSD上,你可以使用flock工具來檢查這一點。在Linux上,flock和fcntl是不相關的,所以你只需要編寫一個簡單的腳本來檢查文件鎖定狀態。這里有一個檢測腳本isunlocked.py的例子。
#!/usr/bin/pythonimport fcntl, syssys.stderr.close()
fcntl.lockf(open(sys.argv[1], "a"), fcntl.LOCK_EX|fcntl.LOCK_NB)
record_max_size
????????設置錄制的最大文件大小。
record_max_size 128K;
record_max_frames
????????設置每個錄制文件的最大視頻幀數。
record_max_frames 2;
record_interval
????????語法:record_interval time
????????在此(毫)秒數后重新開始錄制。默認情況下為關閉。零表示錄制之間沒有延遲。如果record_unique處于關閉狀態,則所有記錄片段都將寫入同一文件。否則,會附加時間戳,導致文件不同(給定的record_interval長于1秒)。
record_interval 1s;record_interval 15m;
recorder
????????語法:?recorder name {...}
????????創建錄制器塊。可以在單個應用程序中創建多個記錄器。上述所有與錄制相關的指令都可以在記錄器{}塊中指定。所有設置都是從更高級別繼承的。
application {live on;# default recorderrecord all;record_path /var/rec;recorder audio {record audio;record_suffix .audio.flv;}recorder chunked {record all;record_interval 15s;record_path /var/rec/chunked;}
}
record_notify
????????當特定錄制器啟動或停止錄制文件時,向發布者發送NetStream.Record.Start和NetStream.Record.Stop狀態消息(onStatus)。狀態描述字段保存記錄器名稱(默認記錄器為空)。默認情況下為關閉。
recorder myrec {record all manual;record_path /var/rec;record_notify on;
}
6.視頻點播
play
????????語法:?play dir|http://loc [dir|http://loc]*
????????從指定目錄或HTTP位置播放flv或mp4文件。如果參數前綴為http://,則假定在播放之前應從遠程http位置下載文件。在下載整個文件之前,不會開始播放。您可以使用本地nginx在本地計算機上緩存文件。
????????可以在單個播放指令中指定多個播放位置。當指定多個播放指令時,位置列表會合并并從更高的作用域繼承。嘗試播放每個位置,直到找到成功的位置。若未找到該位置,則向客戶端發送錯誤狀態。
????????索引FLV具有隨機尋道功能。未索引的FLV在禁用尋道/暫停(僅重啟模式)的情況下播放。使用FLV索引器(例如yamdi)進行索引。
????????如果您播放用記錄指令錄制的FLV,請不要忘記在播放前對其進行索引。因為它們創建時沒有索引。
????????只有RTMP支持視頻和音頻編解,才能播放Mp4文件,最常見的是H264/AAC。
application vod {play /var/flvs;
}application vod_http {play http://myserver.com/vod;
}application vod_mirror {# try local location first, then access remote locationplay /var/local_mirror http://myserver.com/vod;
}
????????播放 /var/flvs/dir/file.flv。vod后的兩個斜杠,使ffplay使用vod和應用程序名稱,其余url作為播放路徑。
ffplay rtmp://localhost/vod//dir/file.flv
play_temp_path
Syntax:?play_temp_path dir
Context: rtmp, server, application
Sets location where remote VOD files are stored before playing. Default is?/tmp
;
play_temp_path /www;
play http://example.com/videos;
play_local_path
Syntax:?play_local_path dir
Context: rtmp, server, application
Sets location where remote VOD files copied from?play_temp_path
?directory after they are completely downloaded. Empty value disables the feature. By default it's empty. The feature can be used for caching remote files locally.
This path should be on the same device as?play_temp_path
.
# search file in /tmp/videos.
# if not found play from remote location
# and store in /tmp/videosplay_local_path /tmp/videos;
play /tmp/videos http://example.com/videos;
Relay
pull
Syntax:?pull url [key=value]*
Context: application
Creates pull relay. Stream is pulled from remote machine and becomes available locally. It only happens when at least one player is playing the stream locally.
Url syntax:?[rtmp://]host[:port][/app[/playpath]]
. If application is missing then local application name is used. If playpath is missing then current stream name is used instead.
The following parameters are supported:
- app - explicit application name
- name - local stream name to bind relay to; if empty or non-specified then all local streams within application are pulled
- tcUrl - auto-constructed if empty
- pageUrl - page url to pretend
- swfUrl - swf url to pretend
- flashVer - flash version to pretend, default is 'LNX.11,1,102,55'
- playPath - remote play path
- live - toggles special behavior for live streaming, values: 0,1
- start - start time in seconds
- stop - stop time in seconds
- static - makes pull static, such pull is created at nginx start
If a value for a parameter contains spaces then you should use quotes around the?WHOLE?key=value pair like this :?'pageUrl=FAKE PAGE URL'
.
pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a;pull rtmp://cdn2.example.com/another/a?b=1&c=d pageUrl=http://www.example.com/video.html swfUrl=http://www.example.com/player.swf live=1;pull rtmp://cdn.example.com/main/ch?id=12563 name=channel_a static;
push
Syntax:?push url [key=value]*
Context: application
Push has the same syntax as pull. Unlike pull push directive publishes stream to remote server.
push_reconnect
Syntax:?push_reconnect time
Context: rtmp, server, application
Timeout to wait before reconnecting pushed connection after disconnect. Default is 3 seconds.
push_reconnect 1s;
session_relay
Syntax:?session_relay on|off
Context: rtmp, server, application
Toggles session relay mode. In this mode relay is destroyed when connection is closed. When the setting is off relay is destroyed when stream is closed so that another relay could possibly be created later. Default is off.
session_relay on;
Notify
on_connect
Syntax:?on_connect url
Context: rtmp, server
Sets HTTP connection callback. When clients issues connect command an HTTP request is issued asynchronously and command processing is suspended until it returns result code. If HTTP 2xx code is returned then RTMP session continues. The code of 3xx makes RTMP redirect to another application whose name is taken from?Location
?HTTP response header. Otherwise connection is dropped.
Note this directive is not allowed in application scope since application is still unknown at connection stage.
HTTP request receives a number of arguments. POST method is used with application/x-www-form-urlencoded MIME type. The following arguments are passed to caller:
- call=connect
- addr - client IP address
- app - application name
- flashVer - client flash version
- swfUrl - client swf url
- tcUrl - tcUrl
- pageUrl - client page url
In addition to the above mentioned items all arguments passed explicitly to connect command are also sent with the callback. You should distinguish connect arguments from play/publish arguments. Players usually have a special way of setting connection string separate from play/publish stream name. As an example here's how these arguments are set in JWPlayer
...
streamer: "rtmp://localhost/myapp?connarg1=a&connarg2=b",
file: "mystream?strarg1=c&strarg2=d",
...
Ffplay (with librtmp) example
ffplay "rtmp://localhost app=myapp?connarg1=a&connarg2=b playpath=mystream?strarg1=c&strarg2=d"
Usage example
on_connect http://example.com/my_auth;
Redirect example
location /on_connect {if ($arg_flashver != "my_secret_flashver") {rewrite ^.*$ fallback? permanent;}return 200;
}
on_play
Syntax:?on_play url
Context: rtmp, server, application
Sets HTTP play callback. Each time a clients issues play command an HTTP request is issued asynchronously and command processing is suspended until it returns result code. HTTP result code is then analyzed.
- HTTP 2xx code continues RTMP session
- HTTP 3xx redirects RTMP to another stream whose name is taken from?
Location
?HTTP response header. If new stream name is started with?rtmp://
?then remote relay is created instead. Relays require that IP address is specified instead of domain name and only work with nginx versions greater than 1.3.10. See also?notify_relay_redirect
. - Otherwise RTMP connection is dropped
Redirect example
http {...location /local_redirect {rewrite ^.*$ newname? permanent;}location /remote_redirect {# no domain name here, only iprewrite ^.*$ rtmp://192.168.1.123/someapp/somename? permanent;}...
}rtmp {...application myapp1 {live on;# stream will be redirected to 'newname'on_play http://localhost:8080/local_redirect;}application myapp2 {live on;# stream will be pulled from remote location# requires nginx >= 1.3.10on_play http://localhost:8080/remote_redirect;}...
}
HTTP request receives a number of arguments. POST method is used with application/x-www-form-urlencoded MIME type. The following arguments are passed to caller:
- call=play
- addr - client IP address
- clientid - nginx client id (displayed in log and stat)
- app - application name
- flashVer - client flash version
- swfUrl - client swf url
- tcUrl - tcUrl
- pageUrl - client page url
- name - stream name
In addition to the above mentioned items all arguments passed explicitly to play command are also sent with the callback. For example if stream is accessed with the url?rtmp://localhost/app/movie?a=100&b=face&foo=bar
?then?a
,?b
?&?foo
?are also sent with callback.
on_play http://example.com/my_callback;
on_publish
Syntax:?on_publish url
Context: rtmp, server, application
The same as on_play above with the only difference that this directive sets callback on publish command. Instead of remote pull push is performed in this case.
on_done
Syntax:?on_done url
Context: rtmp, server, application
Sets play/publish terminate callback. All the above applies here. However HTTP status code is not checked for this callback.
on_play_done
Syntax:?on_play_done url
Context: rtmp, server, application
Same behavior as?on_done
?but only for play end event.
on_publish_done
Syntax:?on_publish_done url
Context: rtmp, server, application
Same behavior as?on_done
?but only for publish end event.
on_record_done
syntax:?on_record_done url
context: rtmp, server, application, recorder
Set record_done callback. In addition to common HTTP callback variables it receives the following values
- recorder - recorder name in config or empty string for inline recorder
- path - recorded file path
Example
on_record_done http://example.com/recorded;
on_update
syntax:?on_update url
context: rtmp, server, application
Set update callback. This callback is called with period of?notify_update_timeout
. If a request returns HTTP result other than 2xx connection is terminated. This can be used to synchronize expired sessions. Two additional arguments?time
?and?timestamp
?are passed to this handler:
time
?is the number of seconds since play/publish calltimestamp
?is RTMP timestamp of the last audio/video packet sent to the client
You can use?timestamp
?argument to individually limit playback duration for each user.
on_update http://example.com/update;
notify_update_timeout
syntax:?notify_update_timeout timeout
context: rtmp, server, application
Sets timeout between?on_update
?callbacks. Default is 30 seconds.
notify_update_timeout 10s;
on_update http://example.com/update;
notify_update_strict
syntax:?notify_update_strict on|off
context: rtmp, server, application
Toggles strict mode for?on_update
?callbacks. Default is off. When turned on all connection errors, timeouts as well as HTTP parse errors and empty responses are treated as update failures and lead to connection termination. When off only valid HTTP response codes other that 2xx lead to failure.
notify_update_strict on;
on_update http://example.com/update;
notify_relay_redirect
syntax:?notify_relay_redirect on|off
context: rtmp, server, application
Enables local stream redirect for?on_play
?and?on_publish
?remote redirects. New stream name is MD5 hash of RTMP URL used for remote redirect. Default is off.
notify_relay_redirect on;
notify_method
syntax:?notify_method get|post
context: rtmp, server, application, recorder
Sets HTTP method for notifications. Default is POST with?application/x-www-form-urlencoded
?content type. In certain cases GET is preferable, for example if you plan to handle the call in?http{}
?section of nginx. In this case you can use?arg_*
?variables to access arguments.
notify_method get;
With GET method handling notifications in?http{}
?section can be done this way
location /on_play {if ($arg_pageUrl ~* localhost) {return 200;}return 500;
}
HLS輸出
hls
????????語法:?hls on|off
????????在應用程序上輸出HLS協議,基本配置如下:
hls on;
hls_path /tmp/hls;
hls_fragment 15s;
在http{}部分中,為客戶端設置以下配置,用來訪問播放HLS流
http {...server {...location /hls {types {application/vnd.apple.mpegurl m3u8;}root /tmp;add_header Cache-Control no-cache;# To avoid issues with cross-domain HTTP requests (e.g. during development)add_header Access-Control-Allow-Origin *;}}
}
hls_path
????????語法:hls_path path
????????設置HLS播放列表和分片的存放目錄。如果目錄不存在,將自動創建。
hls_fragment
????????語法:hls_fragment time
????????設置HLS分片時長,默認為5秒。
hls_playlist_length
????????語法:hls_playlist_length time
????????設置HLS播放列表時長,默認為30秒。
hls_playlist_length 10m;
hls_sync
????????語法:hls_sync time
????????設置HLS時間戳同步閾值,默認值為2ms。此功能可防止從低分辨率RTMP(1KHz)轉換為高分辨率MPEG-TS(90KHz)后出現爆裂噪聲。
hls_sync 100ms;
hls_continuous
????????語法:hls_continuous on|off
????????切換為HLS連續模式,默認設置為off。在此模式下,HLS序列號從上次停止的位置開始,且舊碎片被保留下來。
Toggles HLS continuous mode. In this mode HLS sequence number is started from where it stopped last time. Old fragments are kept. Default is off.
hls_continuous on;
hls_nested
????????語法:?hls_nested on|off
????????切換HLS嵌套模式,默認設置為off。在此模式下,將為每個流創建hls_path的子目錄,播放列表和片段在該子目錄中創建。
Toggles HLS nested mode. In this mode a subdirectory of?hls_path
?is created for each stream. Playlist and fragments are created in that subdirectory. Default is off.
hls_nested on;
hls_base_url
????????語法:?hls_base_url url
???????為HLS播放列表項設置前綴url,默認為空。當為空時,播放列表項沒有前綴,并且假設與父播放列表位于同一位置,或者在使用hls_nested時位于較低級別。
hls_base_url http://myserver.com/hls/;
hls_cleanup
????????語法:?hls_cleanup on|off
????????啟用HLS清理,默認為on。在這種模式下,nginx緩存管理器進程會從HLS目錄中刪除舊的HLS片段和播放列表。
hls_cleanup off;
hls_fragment_naming
????????語法:hls_fragment_naming sequential|timestamp|system
????????設置分片命名模式,默認為sequential
- sequential - 使用整數遞增
- timestamp - 使用流時間戳
- system - 使用系統時間
hls_fragment_naming system;
hls_fragment_naming_granularity
????????語法:hls_fragment_naming_granularity number
????????設置hls分片ids的粒度,默認值為0。如果大于零,則更改ids以除以提供的值。
# use system time rounded to 500ms as fragment names
hls_fragment_naming system;
hls_fragment_naming_granularity 500;
hls_fragment_slicing
????????語法:hls_fragment_slicing plain|aligned
????????設置分片切片模式,默認為plain。
- plain -達到目標持續時間時切換片段
- aligned - 當傳入的時間戳是片段持續時間的倍數時,切換片段。此模式可以在不同的nginx實例上生成相同的片段
hls_fragment_slicing aligned;
hls_variant
????????語法:hls_variant suffix [param*]
????????添加HLS變體條目。當流名稱上的后綴匹配時,將為當前流創建變體播放列表,其中包含當前應用程序中hls_variant指令指定的所有條目。不帶后綴的條帶名稱用作變體流名稱。原始流照常處理。
????????后綴后的可選參數附加到m3u8播放列表中的EXT-X-STREAM-INF。見HLS規范3.3.10。EXT-X-STREAM-INF獲取支持參數的完整列表。
rtmp {server {listen 1935;application src {live on;exec ffmpeg -i rtmp://localhost/src/$name-c:a aac -b:a 32k -c:v libx264 -b:v 128K -f flv rtmp://localhost/hls/$name_low-c:a aac -b:a 64k -c:v libx264 -b:v 256k -f flv rtmp://localhost/hls/$name_mid-c:a aac -b:a 128k -c:v libx264 -b:v 512K -f flv rtmp://localhost/hls/$name_hi;}application hls {live on;hls on;hls_path /tmp/hls;hls_nested on;hls_variant _low BANDWIDTH=160000;hls_variant _mid BANDWIDTH=320000;hls_variant _hi BANDWIDTH=640000;}}
}
hls_type
????????語法:hls_type live|event
???????在X-playlist-type播放列表指令中指定的HLS播放列表類型。直播HLS流通常從當前直播位置播放,該位置是播放列表末尾的幾個片段。Event HLS流始終從播放列表的開頭播放。在Event模式下,確保播放列表長度足以容納整個Event。默認為live
;
Sets HLS playlist type specified in?X-PLAYLIST-TYPE
?playlist directive. Live HLS stream is usually played from the current live position which is several fragments to the end of playlist. Event HLS stream is always played from the start of playlist. When in?event
?mode make sure playlist length is enough for the whole event. Default is?live
;
hls_type event;
hls_keys
????????語法:hls_keys on|off
????????啟用HLS加密,加密方式為AES-128,默認情況下為off
。
hls_keys on;
????????以下是使用HLS加密的示例配置。此配置要求nginx使用--with-http_ssl_module構建以支持https。
...http {...server {listen 443 ssl;server_name example.com;ssl_certificate /var/ssl/example.com.cert;ssl_certificate_key /var/ssl/example.com.key;location /keys {root /tmp;}}server {listen 80;server_name example.com;location /hls {root /tmp;}}
}rtmp {server {listen 1935;application myapp {live on;hls on;hls_path /tmp/hls;hls_keys on;hls_key_path /tmp/keys;hls_key_url https://example.com/keys/;hls_fragments_per_key 10;}}
}
hls_key_path
????????語法:hls_key_path path
????????設置保存自動生成的HLS密鑰的目錄。密鑰文件具有.key擴展名和使用OpenSSL RAND_bytes()例程創建的偽隨機16字節內容。如果目錄不存在,則在運行時創建。默認情況下,hls_path目錄用于密鑰文件。但是請記住,您通常應該限制對關鍵文件的訪問,當這些文件與播放列表和片段分開存儲時,訪問會更容易。
hls_key_path /tmp/keys;
hls_key_url
????????語法:?hls_key_url url
????????設置HLS密鑰文件的前綴url,默認為空。當為空時,這些項目沒有前綴,并且假設密鑰與播放列表位于同一位置。
hls_key_url https://myserver.com/keys/;
具有上述設置的示例播放列表條目
#EXT-X-KEY:METHOD=AES-128,URI="https://myserver.com/keys/337.key",IV=0x00000000000000000000000000000151
hls_fragments_per_key
????????語法:hls_fragments_per_key value
????????設置使用相同密鑰加密的HLS片段的數量,默認值為0。0意味著在發布開始時,只創建一個密鑰,會話中的所有片段都使用此密鑰進行加密。,
hls_fragments_per_key 10;
MPEG-DASH
dash
????????語法:dash on|off
????????在應用程序上輸出MPEG-DASH。
dash on;
dash_path /tmp/dash;
dash_fragment 15s;
在http{}部分中,為客戶端設置以下配置,用來訪問播放DASH流
http {...server {...location /dash {root /tmp;add_header Cache-Control no-cache;# To avoid issues with cross-domain HTTP requests (e.g. during development)add_header Access-Control-Allow-Origin *;}}
}
dash_path
????????語法:dash_path path
????????設置MPEG-DASH播放列表和片段目錄。如果目錄不存在,將創建它。
dash_fragment
????????語法:dash_fragment time
????????設置MPEG-DASH片段長度,默認為5秒。
dash_playlist_length
????????語法:?dash_playlist_length time
????????設置MPEG-DASH播放列表長度,默認為30秒。
dash_playlist_length 10m;
dash_nested
????????語法:?dash_nested on|off
????????切換MPEG-DASH嵌套模式。在此模式下,將為每個流創建dash_path的子目錄。播放列表和片段在該子目錄中創建。默認設置為off
。
dash_nested on;
dash_cleanup
????????語法:?dash_cleanup on|off
????????切換MPEG-DASH清理。默認情況下,該功能處于on
狀態。在這種模式下,nginx緩存管理器進程會從MPEG-DASH目錄中刪除舊的MPEG-DASH片段和清單。刪除流清單后,將刪除初始化片段。
dash_cleanup off;
訪問日志
access_log
Syntax:?access_log off|path [format_name]
Context: rtmp, server, application
Sets access log parameters. Logging is turned on by default. To turn it off use?access_log off
?directive. By default access logging is done to the same file as HTTP access logger (logs/access.log
). You can specify another log file path in?access_log
?directive. Second argument is optional. It can be used to specify logging format by name. See?log_format
?directive for more details about formats.
log_format new '$remote_addr';
access_log logs/rtmp_access.log new;
access_log logs/rtmp_access.log;
access_log off;
log_format
Syntax:?log_format format_name format
Context: rtmp
Creates named log format. Log formats look very much the same as nginx HTTP log formats. Several variables are supported within log format:
connection
?- connection numberremote_addr
?- client addressapp
?- application namename
?- last stream nameargs
?- last stream play/publish argumentsflashver
?- client flashVerswfurl
?- client swfUrltcurl
?- client tcUrlpageurl
?- client pageUrlcommand
?- play/publish commands sent by client:?NONE
,?PLAY
,?PUBLISH
,?PLAY+PUBLISH
bytes_sent
?- number of bytes sent to clientbytes_received
?- number of bytes received from clienttime_local
?- local time at the end of client connectionsession_time
?- connection duration in secondssession_readable_time
?- connection duration in human-readable formatmsec
?- current unix timestamp in SEC.MSEC format
Default log format has the name?combined
. Here's the definition of this format
$remote_addr [$time_local] $command "$app" "$name" "$args" -
$bytes_received $bytes_sent "$pageurl" "$flashver" ($session_readable_time)
Limits
max_connections
Syntax:?max_connections number
Context: rtmp, server, application
Sets maximum number of connections for rtmp engine. Off by default.
max_connections 100;
Statistics
Statistics module is NGINX HTTP module unlike all other modules listed here. Hence statistics directives should be located within http{} block.
rtmp_stat
Syntax:?rtmp_stat all
Context: http, server, location
Sets RTMP statistics handler to the current HTTP location. RTMP statistics is dynamic XML document. To watch this document in browser as XHTML page use rtmp_stat_stylesheet directive.
http {server {location /stat {rtmp_stat all;rtmp_stat_stylesheet stat.xsl;}location /stat.xsl {root /path/to/stat/xsl/file;}}
}
rtmp_stat_stylesheet
Syntax:?rtmp_stat_stylesheet path
Context: http, server, location
Adds XML stylesheet reference to statistics XML to make it viewable in browser. See rtmp_stat description and example for more information.
Multi-worker live streaming
Multi-worker live streaming is implemented through pushing stream to remaining nginx workers.
rtmp_auto_push
Syntax:?rtmp_auto_push on|off
Context: root
Toggles auto-push (multi-worker live streaming) mode. Default is off.
rtmp_auto_push_reconnect
Syntax:?rtmp_auto_push_reconnect timeout
Context: root
Sets auto-push reconnect timeout when worker is killed. Default is 100 milliseconds.
rtmp_socket_dir
Syntax:?rtmp_socket_dir dir
Context: root
Sets directory for UNIX domains sockets used for stream pushing. Default is?/tmp
.
rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /var/sock;rtmp {server {listen 1935;application myapp {live on;}}
}
Control
Control module is NGINX HTTP module and should be located within http{} block.
rtmp_control
Syntax:?rtmp_control all
Context: http, server, location
Sets RTMP control handler to the current HTTP location.
http {server {location /control {rtmp_control all;}}
}
More details about control moduleMore details about control moduleMore details about control module