syntax: rtmp { ... }
context: root
The block which holds all RTMP settings
syntax: server { ... }
context: rtmp
Declares RTMP server instance
rtmp {
server {
}
}
syntax: listen (addr[:port]|port|unix:path) [bind] [ipv6only=on|off] [so_keepalive=on|off|keepidle:keepintvl:keepcnt]
context: server
Adds listening socket to NGINX for accepting RTMP connections
server {
listen 1935;
}
syntax: application name { ... }
context: server
Creates RTMP application. Unlike http location application name cannotbe a pattern.
server {
listen 1935;
application myapp {
}
}
syntax: timeout value
context: rtmp, server
Socket timeout. This value is primarily used for writing. Most of time RTMP module does not expect any activity on all sockets except for publisher socket. If you want broken socket to get quickly disconnected use active tools like keepalive or RTMP ping. Default is 1 minute.
timeout 60s;
syntax: ping value
context: rtmp, server
RTMP ping interval. Zero turns ping off. RTMP ping is a protocol feature foractive connection check. A special packet is sent to remote peer and a replyis expected within a timeout specified with ping_timeout directive. If pingreply is not received within this time then connection is closed. Default value for ping is 1 minute. Default ping timeout is 30 seconds.
ping 3m;
ping_timeout 30s;
syntax: ping_timeout value
context: rtmp, server
See ping description above.
syntax: max_streams value
context: rtmp, server
Sets maximum number of RTMP streams. Data streams are multiplexed intoa single data stream. Different channels are used for sending commands,audio, video etc. Default value is 32 which is usually ok for many cases.
max_streams 32;
syntax: ack_window value
context: rtmp, server
Sets RTMP acknowledge window size. It's the number of bytes received afterwhich peer should send acknowledge packet to remote side. Default value is5000000.
ack_window 5000000;
syntax: chunk_size value
context: rtmp, server
Maximum chunk size for stream multiplexing. Default is 4096. The biggerthis value the lower CPU overhead. This value cannot be less than 128.
chunk_size 4096;
syntax: max_queue value
context: rtmp, server
Maximum size of input data message. All input data comes split intomessages (and further in chunks). A partial message is kept in memory whilewaiting for it to complete. In theory incoming message can bevery large which can be a problem for server stability. Default value1M is enough for many cases.
max_message 1M;
Syntax: allow [play|publish] address|subnet|all
Context: rtmp, server, application
Allow publishing/playing from addresses specified or from all addresses.Allow/deny directives are checked in order of appearance.
allow publish 127.0.0.1;
deny publish all;
allow play 192.168.0.0/24;
deny play all;
Syntax: deny [play|publish] address|subnet|all
Context: rtmp, server, application
See allow for description.
Syntax: exec command arg*
Context: rtmp, server, application
Specifies external command with arguments to be executed onevery stream published. When publishing stops the processis terminated. Full path to binary should be specified as thefirst argument. There are no assumptions about what this process shoulddo. However this feature is useful with ffmpeg for streamtranscoding. FFmpeg is supposed to connect to nginx-rtmp as a client and output transcoded stream back to nginx-rtmp as publisher. Substitutionsof form $var/${var} can be used within command line:
Shell-style redirects can be specified in exec directive for writing outputand accepting input. Supported are
>file
>>file
1>&2
The following ffmpeg call transcodes incoming stream to HLS-readystream (H264/AAC). FFmpeg should be compiled with libx264 & libfaac supportfor this example to work.
application src {
live on;
exec ffmpeg -i rtmp://localhost/src/$name -vcodec libx264 -vprofile baseline -g 10 -s 300x200 -acodec libfaac -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;
}
Syntax: exec_static command arg*
Context: rtmp, server, application
Similar to exec
but runs specified command at nginx start.Does not support substitutions since has no session context.
exec_static ffmpeg -i http://example.com/video.ts -c copy -f flv rtmp://localhost/myapp/mystream;
Syntax: exec_kill_signal signal
Context: rtmp, server, application
Sets process termination signal. Default is kill (SIGKILL).You can specify numeric or symbolic name (for POSIX.1-1990 signals).
exec_kill_signal term;
exec_kill_signal usr1;
exec_kill_signal 3;
Syntax: respawn on|off
Context: rtmp, server, application
If turned on respawns child process when it's terminated while publishingis still on. Default is on;
respawn off;
Syntax: respawn_timeout timeout
Context: rtmp, server, application
Sets respawn timeout to wait before starting new child instance.Default is 5 seconds.
respawn_timeout 10s;
Syntax: exec_publish command arg*
Context: rtmp, server, application
Specifies external command with arguments to be executed onpublish event. Return code is not analyzed. Substitutions of exec
are supported here as well. In addition args
variable is supportedholding query string arguments.
Syntax: exec_play command arg*
Context: rtmp, server, application
Specifies external command with arguments to be executed onplay event. Return code is not analyzed. Substitution listis the same as for exec_publish
.
Syntax: exec_play_done command arg*
Context: rtmp, server, application
Specifies external command with arguments to be executed onplay_done event. Return code is not analyzed. Substitution listis the same as for exec_publish
.
Syntax: exec_publish_done command arg*
Context: rtmp, server, application
Specifies external command with arguments to be executed onpublish_done event. Return code is not analyzed. Substitution listis the same as for exec_publish
.
Syntax: exec_record_done command arg*
Context: rtmp, server, application, recorder
Specifies external command with arguments to be executed whenrecording is finished. Substitution of exec_publish
are supported hereas well as additional variables path
and recorder
.
# 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 $path.mp4;
Syntax: live on|off
Context: rtmp, server, application
Toggles live mode i.e. one-to-many broadcasting.
live on;
Syntax: meta on|off
Context: rtmp, server, application
Toggles sending metadata to clients. Defaults to on.
meta off;
Syntax: interleave on|off
Context: rtmp, server, application
Toggles interleave mode. In this mode audio and videodata is transmitted on the same RTMP chunk stream.Defaults to off.
interleave on;
Syntax: wait_key on|off
Context: rtmp, server, application
Makes video stream start with a key frame. Defaults to off.
wait_key on;
Syntax: wait_video on|off
Context: rtmp, server, application
Disable audio until first video frame is sent. Defaults to off.Can be combined with wait_key
to make client receive videokey frame with all other data following it. However this usuallyincreases connection delay. You can tune keyframe interval in yourencoder to reduce the delay.
wait_video on;
Syntax: publish_notify on|off
Context: rtmp, server, application
Send NetStream.Publish.Start
and NetStream.Publish.Stop
tosubscribers. Defaults to off.
publish_notify on;
Syntax: drop_idle_publisher timeout
Context: rtmp, server, application
Drop publisher connection which has been idle (no audio/video data)within specified time. Default is off. Note this only works whenconnection is in publish mode (after sending publish
command).
drop_idle_publisher 10s;
Syntax: sync timeout
Context: rtmp, server, application
Synchronize audio and video streams. If subscriber bandwidthis not enough to receive data at ublisher rate some frames aredropped by server. This leads to synchronization problem. When timestamp difference exceeds the value specified as sync
argument an absolute frame is sent fixing that. Default is 300ms.
sync 10ms;
Syntax: play_restart on|off
Context: rtmp, server, application
If enabled nginx-rtmp sends NetStream.Play.Start and NetStream.Play.Stopto each subscriber every time publisher starts or stops publishing. If disabledeach subscriber receives those notifications only at the start and end ofplayback. Default is on.
play_restart off;
syntax: record [off|all|audio|video|keyframes|manual]*
context: rtmp, server, application, recorder
Toggles record mode. Stream can be recorded in flv file. This directivespecifies what exactly should be recorded:
There can be any compatible combination of keys in a single record directive.
record all;
record audio keyframes;
syntax: record_path path
context: rtmp, server, application, recorder
Specifies record path to put recorded flv files to.
record_path /tmp/rec;
syntax: record_suffix value
context: rtmp, server, application, recorder
Sets record file suffix. Defaults to '.flv'.
record_suffix _recorded.flv;
Record suffix can be a pattern in strftime
format.The following directive
record_suffix -%d-%b-%y-%T.flv
will produce files of the form mystream-24-Apr-13-18:23:38.flv
.All supported strftime
format options can be found on strftime man page.
syntax: record_unique on|off
context: rtmp, server, application, recorder
If turned on appends current timestamp to recorded files. Otherwise the same fileis re-written each time new recording takes place. Default is off.
record_unique on;
syntax: record_append on|off
context: rtmp, server, application, recorder
Toggles file append mode. When turned on recorder appends new data to the old fileor creates it when it's missing. There's no time gap between the old data and the newdata in file. Default is off.
record_append on;
syntax: record_lock on|off
context: rtmp, server, application, recorder
When turned on currently recorded file gets locked with fcntl
call.That can be checked from elsewhere to find out which file is being recorded.Default is off.
record_lock on;
On FreeBSD you can use flock
tool to check that. On Linux flock
and fcntl
are unrelated so you are left with writing a simple script checking file lock status.Here's an example of such script isunlocked.py
.
#!/usr/bin/python
import fcntl, sys
sys.stderr.close()
fcntl.lockf(open(sys.argv[1], "a"), fcntl.LOCK_EX|fcntl.LOCK_NB)
syntax: record_max_size size
context: rtmp, server, application, recorder
Set maximum recorded file size.
record_max_size 128K;
syntax: record_max_frames nframes
context: rtmp, server, application, recorder
Sets maximum number of video frames per recorded file.
record_max_frames 2;
syntax: record_interval time
context: rtmp, server, application, recorder
Restart recording after this number of (milli)seconds.Off by default. Zero means no delay between recordings. Ifrecord_unique is off then all record fragments are written to thesame file. Otherwise timestamp is appended which makes filesdiffer (given record_interval is longer than 1 second).
record_interval 1s;
record_interval 15m;
syntax: recorder name {...}
context: application
Create recorder block. Multiple recorders can be created withingsingle application. All the above mentioned recording-related directives can be specified in recorder{}
block. All settingsare inherited from higher levels.
application {
live on;
# default recorder
record 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;
}
}
syntax: record_notify on|off
context: rtmp, server, application, recorder
Toggles sending NetStream.Record.Start and NetStream.Record.Stopstatus messages (onStatus) to publisher when specific recorder starts or stops recording file. Status description field holdsrecorder name (empty for default recorder). Off by default.
recorder myrec {
record all manual;
record_path /var/rec;
record_notify on;
}
Syntax: play dir|http://loc [dir|http://loc]*
Context: rtmp, server, application
PLay flv or mp4 file from specified directory or HTTP location.If the argument is prefixed with http://
then it is assumedthat file should be downloaded from remote http location beforeplaying. Note playing is not started until the whole file isdownloaded. You can use local nginx to cache files on local machine.
Multiple play locations can be specified in a single play
directive.When multiple play
directives are specified the location listsare merged and inherited from higher scopes. An attempt to playeach location is made until a successful location is found.If such location is not found error status is sent to client.
Indexed FLVs are played with random seek capability.Unindexed FLVs are played with seek/pause disabled(restart-only mode). Use FLV indexer (for example, yamdi)for indexing.
Mp4 files can only be played if both video and audio codec are supportedby RTMP. The most common case is 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 location
play /var/local_mirror http://myserver.com/vod;
}
Playing /var/flvs/dir/file.flv:
ffplay rtmp://localhost/vod//dir/file.flv
The two slashes after vod
make ffplay use vod
and application nameand the rest of the url as playpath.
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;
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 valuedisables the feature. By default it's empty. The feature can be usedfor 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/videos
play_local_path /tmp/videos;
play /tmp/videos http://example.com/videos;
Syntax: pull url [key=value]*
Context: application
Creates pull relay. Stream is pulled from remote machineand becomes available locally. It only happens when at leastone player is playing the stream locally.
Url syntax: [rtmp://]host[:port][/app[/playpath]]
. If applicationis missing then local application name is used. If playpath is missingthen current stream name is used instead.
The following parameters are supported:
If a value for a parameter contains spaces then you should use quotes aroundthe 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;
Syntax: push url [key=value]*
Context: application
Push has the same syntax as pull. Unlike pull push directive publishes stream to remote server.
Syntax: push_reconnect time
Context: rtmp, server, application
Timeout to wait before reconnecting pushed connection after disconnect. Default is 3 seconds.
push_reconnect 1s;
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 relaycould possibly be created later. Default is off.
session_relay on;
Syntax: on_connect url
Context: rtmp, server
Sets HTTP connection callback. When clients issues connect commandan HTTP request is issued asynchronously and command processing issuspended until it returns result code. If HTTP 2xx code is returnedthen RTMP session continues. The code of 3xx makes RTMP redirectto another application whose name is taken from Location
HTTPresponse header. Otherwise connection is dropped.
Note this directive is not allowed in application scope sinceapplication is still unknown at connection stage.
HTTP request receives a number of arguments. POST method is used withapplication/x-www-form-urlencoded MIME type. The following arguments arepassed to caller:
In addition to the above mentioned items all arguments passed explicitly to connect command are also sent with the callback. You should distinguishconnect arguments from play/publish arguments. Players usually have a specialway 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;
}
Syntax: on_play url
Context: rtmp, server, application
Sets HTTP play callback. Each time a clients issues play commandan HTTP request is issued asynchronously and command processing issuspended until it returns result code. HTTP result code is then analyzed.
Location
HTTP response header. If new stream name is started with rtmp://
then remote relay is created instead. Relays require that IP address isspecified instead of domain name and only work with nginx versionsgreater than 1.3.10. See also notify_relay_redirect
.Redirect example
http {
...
location /local_redirect {
rewrite ^.*$ newname? permanent;
}
location /remote_redirect {
# no domain name here, only ip
rewrite ^.*$ 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.10
on_play http://localhost:8080/remote_redirect;
}
...
}
HTTP request receives a number of arguments. POST method is used withapplication/x-www-form-urlencoded MIME type. The following arguments arepassed to caller:
In addition to the above mentioned items all arguments passed explicitly to play command are also sent with the callback. For example if stream isaccessed with the url rtmp://localhost/app/movie?a=100&b=face&foo=bar
thena
, b
& foo
are also sent with callback.
on_play http://example.com/my_callback;
Syntax: on_publish url
Context: rtmp, server, application
The same as on_play above with the only difference that this directive setscallback on publish command. Instead of remote pull push is performed inthis case.
Syntax: on_done url
Context: rtmp, server, application
Sets play/publish terminate callback. All the above applies here. HoweverHTTP status code is not checked for this callback.
Syntax: on_publish_done url
Context: rtmp, server, application
Same behavior as on_done
but only for play end event.
Syntax: on_publish_done url
Context: rtmp, server, application
Same behavior as on_done
but only for publish end event.
syntax: on_record_done url
context: rtmp, server, application, recorder
Set record_done callback. In addition to common HTTP callbackvariables it receives recorded file path.
on_record_done http://example.com/recorded;
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 otherthan 2xx connection is terminated. This can be used to synchronizeexpired sessions. Additional time
argument is passed to this handlerwhich is the number of seconds since play/publish call.
on_update http://example.com/update;
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;
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 parseerrors and empty responses are treated as update failures and leadto connection termination. When off only valid HTTP response codesother that 2xx lead to failure.
notify_update_strict on;
on_update http://example.com/update;
syntax: notify_relay_redirect on|off
context: rtmp, server, application
Enables local stream redirect for on_play
and on_publish
remoteredirects. New stream name is RTMP URL used for remote redirect.Default is off.
notify_relay_redirect on;
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 casesGET is preferable, for example if you plan to handle the callin http{}
section of nginx. In this case you can use arg_*
variablesto 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;
}
Syntax: hls on|off
Context: rtmp, server, application
Toggles HLS on the application.
hls on;
hls_path /tmp/hls;
hls_fragment 15s;
In http{}
section set up the following location for clients to play HLS.
http {
...
server {
...
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
}
alias /tmp/hls;
}
}
}
Syntax: hls_path path
Context: rtmp, server, application
Sets HLS playlist and fragment directory. This directory shouldexist before NGINX starts.
Syntax: hls_fragment time
Context: rtmp, server, application
Sets HLS fragment length. Defaults to 5 seconds.
Syntax: hls_playlist_length time
Context: rtmp, server, application
Sets HLS playlist length. Defaults to 30 seconds.
hls_playlist_length 10m;
Syntax: hls_sync time
Context: rtmp, server, application
Sets HLS timestamp synchronization threshold. Default is 2ms.This feature prevents crackling noises after conversionfrom low-resolution RTMP (1KHz) to high-resolution MPEG-TS (90KHz).
hls_sync 100ms;
Syntax: hls_continuous on|off
Context: rtmp, server, application
Toggles HLS continuous mode. In this mode HLS sequence numberis started from where it stopped last time. Old fragments arekeeped. Default is off.
hls_continuous on;
Syntax: hls_nested on|off
Context: rtmp, server, application
Toggles HLS nested mode. In this mode a subdirectoryof hls_path
is created for each stream. Playlistand fragments are created in that subdirectory.Default is off.
hls_nested on;
Syntax: hls_base_url url
Context: rtmp, server, application
Sets base url for HLS playlist items. When empty thoseitems have no prefix and assumed to be at the same locationas parent playlist or one level lower when hls_nested
isused. This feature applies both to master (variant) and slave HLS playlists. It can let you download the playlist and play itlocally since it contains full references to child playlists orfragments. Empty by default.
hls_base_url http://myserver.com/hls/;
Syntax: hls_cleanup on|off
Context: rtmp, server, application
Toggles HLS cleanup. By default the feature is on.In this mode nginx cache manager process removes oldHLS fragments and playlists from HLS directory.
hls_cleanup off;
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 loggingis 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;
Syntax: log_format format_name format
Context: rtmp
Creates named log format. Log formats look very much the same as nginx HTTP logformats. 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 formatDefault 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)
Syntax: max_connections number
Context: rtmp, server, application
Sets maximum number of connections for rtmp engine. Off by refault.
max_connections 100;
Statistics module is NGINX HTTP module unlike all other modules listedhere. Hence statistics directives should be located within http{} block.
Syntax: rtmp_stat all
Context: http, server, location
Sets RTMP statistics handler to the current HTTP location. RTMP statistics isdynamic XML document. To watch this document in browser as XHTML pageuse 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;
}
}
}
Syntax: rtmp_stat_stylesheet path
Context: http, server, location
Adds XML stylesheet reference to statistics XML to make it viewablein browser. See rtmp_stat description and example for more information.
Multi-worker live streaming is implemented through pushing streamto remaining nginx workers.
Syntax: rtmp_auto_push on|off
Context: root
Toggles auto-push (multi-worker live streaming) mode.Default is off.
Syntax: rtmp_auto_push_reconnect timeout
Context: root
Sets auto-push reconnect timeout when worker is killed.Default is 100 milliseconds.
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 module is NGINX HTTP module and should be located within http{} block.
Syntax: rtmp_control all
Context: http, server, location
Sets RTMP control handler to the current HTTP location.
http {
server {
location /control {
rtmp_control all;
}
}
}