ustoopia

nginx.conf with descriptions

Nov 25th, 2020
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.40 KB | None | 0 0
  1. ## ====================================================================================================== ##
  2. ## Visit this page for a list of all variables: https://github.com/arut/nginx-rtmp-module/wiki/Directives ##
  3. ## Visit this site for many more configuration examples: https://github.com/arut/nginx-rtmp-module ##
  4. ## This example file was put together by Andre (https://youtube.com/ustoopia & https://www.ustoopia.nl) ##
  5. ## ====================================================================================================== ##
  6.  
  7. user www-data; # Only used on linux. Nginx will run under this username.
  8. worker_processes 1; # Set this to how many processors/cores the CPU has. Relates to "worker_connections".
  9. pid /run/nginx.pid; # Sets the location of the process id file (used on linux only).
  10. include /etc/nginx/modules-enabled/*.conf; # Include all the optional configuration files stored in this folder.
  11.  
  12. events {
  13. worker_connections 768; # Worker_processes * worker_connections = max clients. So in this setup: 1 * 768 = 768 max clients.
  14. # multi_accept on; # "Off" will accept 1 new connection at a time. "On" will accept all new connections. Default is off.
  15. }
  16.  
  17. http {
  18. sendfile off; # on|off. Toggles the use of sendfile. Default=off. For optimal HLS delivery disable this.
  19. tcp_nodelay on; # on|off. Forces a socket to send the data in its buffer, whatever the packet size. Default=on.
  20. tcp_nopush on; # on|off. Sends the response header and beginning of a file in one packet. Default=off.
  21. server_tokens off; # on|off|build. Toggles showing nginx version in the response header field. Default=on.
  22. keepalive_timeout 65; # A keep-alive client connection will stay open for .. on the server side. Use 0 to disable. Default=75s
  23. types_hash_max_size 2048; # Sets the maximum size of the types hash tables. Default=1024.
  24. # server_name_in_redirect on; # Toggles the use of the primary server name, specified by the server_name directive. Default=off.
  25. # server_names_hash_bucket_size 64; # Sets the bucket size for the server names hash tables depending on processor's cache line, 32|64|128.
  26. default_type application/octet-stream; # Emit this MIME type for all requests.
  27.  
  28. ## Include configuration files from these locations ##
  29. include /etc/nginx/mime.types;
  30. include /etc/nginx/conf.d/*.conf;
  31. include /etc/nginx/sites-enabled/*.conf; # Holds symlinks to the actual config files in /etc/nginx/sites-available
  32.  
  33. access_log /var/log/nginx/access.log; # Set this here or in the virtual hosts config file in sites-available folder.
  34. error_log /var/log/nginx/error.log warn; # Set this here or in the virtual hosts config file in sites-available folder.
  35.  
  36. gzip on; # on|off. Compresses responses using gzip method. Helps to reduce size of transmitted data by half or more. Default=off
  37. # gzip_vary on; # More info on zip variables is found here: https://nginx.org/en/docs/http/ngx_http_gzip_module.html
  38. # gzip_proxied any;
  39. # gzip_comp_level 6;
  40. # gzip_buffers 16 8k;
  41. # gzip_http_version 1.1;
  42. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  43.  
  44. ## EITHER SET YOUR SERVER {} BLOCK HERE, OR RATHER IN YOUR VIRTUAL HOSTS CONFIG FILES. CUT FROM BEGIN TO END SERVER BLOCK ##
  45.  
  46. ## BEGIN SERVER BLOCK ##
  47. server {
  48. ## STATISTICS - These lines will allow us to view stats the stats for RTMP streams ##
  49. location /stat {
  50. rtmp_stat all;
  51. rtmp_stat_stylesheet stat.xsl;
  52. }
  53. location stat.xsl {
  54. root /var/www/html/stat.xsl;
  55. }
  56.  
  57. ## RTMP control module - More info: https://github.com/arut/nginx-rtmp-module/wiki/Control-module ##
  58. location /control {
  59. rtmp_control all;
  60. }
  61.  
  62. ## HLS Keys location - See description below to find out why we may need this to be set ##
  63. location /keys {
  64. root /tmp;
  65. }
  66. }
  67. ## END SERVER BLOCK ##
  68. }
  69.  
  70. ## RTMP CORE ##
  71.  
  72. rtmp {
  73. server {
  74. listen 1935; # The RTMP listening port. Open it in your router/firewall. Options: (addr[:port]|port|unix:path) [bind] [ipv6only=on|off] [so_keepalive=on|off|keepidle:keepintvl:keepcnt|proxy_protocol]
  75.  
  76. application live { # Name it whatever you prefer. You will need at least one application, you can have many more.
  77. live on; # on|off. Enables this application and allowing live streaming to it. Default=on.
  78. ping 1m; # A ping is sent out using this interval. Set to 0 to disable. Default=1m.
  79. ping_timeout 30s; # If ping reply is not received within this time then connection is closed. Default=30s.
  80. # max_streams 32; # Sets maximum number of RTMP streams. Default value is 32 which is usually ok for most cases. Default=32.
  81. # max_connections 100; # Sets maximum number of connections for rtmp engine. Default=off.
  82. ack_window 5000000; # RTMP acknowledge window size. Number of bytes received after which peer should send acknowledge packet. Default=5000000.
  83. chunk_size 4096; # Max chunk size for multiplexing. The bigger this value, the lower CPU overhead. Cannot be < 128. Default=4096.
  84. max_message 1M; # All input data splits into messages (and further in chunks). 1M is enough for most cases. Default=1M.
  85. buflen 1000ms; # Sets buffer length. Usually client sends RTMP set_buflen before playing and it resets this setting. Default=1000ms.
  86. meta on; # on|copy|off. Receive metadata packets containing predefined fields like width, height etc. Default=on.
  87. interleave off; # on|off. Audio and video data is transmitted on the same RTMP chunk stream. Default=off.
  88. wait_key on; # on|off. Makes video stream start with a key frame. Default=off.
  89. wait_video off; # on|off. Disable audio until first video frame is sent (can cause delay). Default=off.
  90. publish_notify off; # on|off. Send "NetStream.Play.PublishNotify" and "NetStream.Play.UnpublishNotify" to subscribers. Default=off.
  91. drop_idle_publisher 10s;# Drop publisher that has been idle for this time. Only works when connection is in publish mode. Default=off
  92. sync 300ms; # When timestamp difference exceeds the value specifiedan absolute frame is sent fixing that. Default=300ms.
  93. play_restart off; # on|off. If enabled sends "NetStream.Play.Start" and "NetStream.Play.Stop" every time publishing starts or stops. Default=off.
  94. idle_streams on; # on|off. If disabled prevents viewers from connecting to idle/nonexistent streams and disconnects all. Default=on.
  95.  
  96. ## TRANSCODING USING FFMPEG EXEC ##
  97. ## The following lines will take our incoming RTMP stream and transcode it to several different HLS streams with variable bitrates ##
  98. ## Once receive stream, transcode for adaptive streaming. This single ffmpeg command takes the input and transforms the source into ##
  99. ## 4 or 5 different streams with different bitrate and quality. P.S. The scaling done here respects the aspect ratio of the input. ##
  100. ## EXEC - Many things are possible using exec. To learn more visit https://github.com/arut/nginx-rtmp-module/wiki/Directives#exec ##
  101.  
  102. # exec ffmpeg -i rtmp://localhost/$app/$name -async 1 -vsync -1
  103. # -c:v libx264 -acodec copy -b:v 256k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -g 60 -hls_list_size 0 -f flv rtmp://localhost/hls/$name_low
  104. # -c:v libx264 -acodec copy -b:v 768k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -g 60 -hls_list_size 0 -f flv rtmp://localhost/hls/$name_mid
  105. # -c:v libx264 -acodec copy -b:v 1024k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -g 60 -hls_list_size 0 -f flv rtmp://localhost/hls/$name_high
  106. # -c:v libx264 -acodec copy -b:v 1920k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -g 60 -hls_list_size 0 -f flv rtmp://localhost/hls/$name_higher
  107. # -c copy -f flv rtmp://localhost/hls/$name_src;
  108.  
  109. ## ACCESS ##
  110. ## PUBLISHING ##
  111. allow publish 192.168.2.0/24; # Anybody from this local IP address range can stream to this application.
  112. allow publish 192.168.2.50; # Allow a single IP address to stream to this application (multiple lines with different ip's are possible)
  113. allow publish all; # Anybody from any location can stream to this application. Comment this out if you want to use deny publish.
  114. # deny publish 10.0.0.50; # Deny this specific IP address from streaming to this application. Can also be an IP address range.
  115. # deny publish all; # Nobody can stream to the server except those that you've set in previous lines.
  116.  
  117. ## PLAYING ##
  118. allow play 127.0.0.1; # Localhost can play the stream from this application. Must be set if you decide to use push later on!
  119. allow play 192.168.2.0/24 # Anybody in this IP range can play the RTMP directly.
  120. allow play all; # Anybody can play the RTMP livestream directly. Comment this out if you decide to use the next option.
  121. # deny play all; # Nobody can play the RTMP stream except those that we've set in lines above it.
  122.  
  123. ## RECORDING ##
  124. record off; # off|all|audio|video|keyframes|manual. These options speak for themselves.
  125. record_path /tmp/rec; # Folder location that will be used to store the recordings.
  126. record_suffix -%d-%b-%y-%T.flv; # Added to recorded filenames. Example uses 'strftime' format results: -24-Apr-13-18:23:38.flv. Default=.flv
  127. record_unique on; # on|off. Appends timestamp to recorded files. Otherwise the same file is re-written each time. Default=-off
  128. record_append off; # on|off. When turned on recorder appends new data to the old file with no gap. Default=off
  129. record_lock off; # on|off. When turned on recorded file gets locked with 'fcntl' call. Default=off.
  130. record_notify off; # on|off. Toggles sending "NetStream.Record.Start" and "NetStream.Record.Stop" status messages. Default=off.
  131. # record_max_size 128K; # Set maximum file size of the recorded files. No default.
  132. # record_max_frames 200; # Sets maximum number of video frames per recorded file. No default.
  133. # record_interval 15m; # Restart recording after this number of (milli)seconds/minutes. Zero means no delay. Default=off.
  134. # recorder name {} # Create recorder{} block. Multiple recorders can be created withing single application. Example:
  135. recorder audio {
  136. record audio;
  137. record_suffix .audio.flv;
  138. record_path /tmp/rec/audio;
  139. }
  140.  
  141. ## STREAM RELAY ##
  142. ## For more info please visit: https://github.com/arut/nginx-rtmp-module/wiki/Directives#relay ##
  143. ## pull url [key=value]* # Creates pull relay. A stream is pulled from remote machine and becomes available locally. ##
  144. ## URL Syntax: [rtmp://]host[:port][/app[/playpath]] ##
  145. # pull rtmp://cdn.example.com/live/ch?id=1234 name=channel_a; # This is an example. Visit above url for more info.
  146. # session_relay off; # on|off. On=relay is destroyed when connection is closed. Off=relay is destroyed when stream is closed. Default=off.
  147.  
  148. ## Push has the same syntax as pull. Unlike pull, push directive publishes stream to remote server. ##
  149. ## This will push the stream from current application to these two applications to create a hls and/or dash stream automatically. ##
  150.  
  151. push rtmp://localhost/dash; # Remember to create the app dash! It is safe to disable this if you're not interested in using Dash.
  152. push rtmp://localhost/hls; # Remember to create the app hls! Disable it if you use transcoding using variable bitrates.
  153.  
  154. ## Push can also be used to re-stream your stream to other platforms. You can do this directly or use an additional application. ##
  155.  
  156. ## YOUTUBE ##
  157. # push rmtp://localhost/youtube; # Use local application OR use the following line:
  158. # push rtmp://a.rtmp.youtube.com/live2/YOUR-LIVE-STREAM-KEY;
  159.  
  160. # TWITCH ##
  161. # push rmtp://localhost/twitch; # Use local application OR use the following line:
  162. # push rtmp://live-ams.twitch.tv/app/live_YOUR-LIVE-STREAM-KEY;
  163. }
  164.  
  165. ## HLS ##
  166. application hls { # Since we've enabled pushing stream from the 'live' application to 'hls' we need to define it.
  167. live on; # on|off. Enables this application and allowing live streaming to it. Default=on.
  168. hls on; # on|off. Toggles HLS on or off for this application.
  169. hls_type live; # live|event. Live plays from the current live position. Event plays from the start of the playlist. Default=live.
  170. hls_path /tmp/hls; # Location to store the video fragment files. Will be created if it doesn't exist.
  171. hls_fragment 5s; # Sets HLS fragment length in seconds or minutes. Default=5s.
  172. hls_playlist_length 30s; # Sets HLS playlist length in seconds or minutes. Default=30s.
  173. hls_sync 2ms; # Timestamp sync threshold. Prevents crackling noise after conversion from low-res (1KHz) to high-res(90KHz). Default=2ms.
  174. hls_continuous off; # on|off. In this mode HLS sequence number is started from where it stopped last time. Old fragments are kept. Default=off.
  175. hls_nested on; # on|off. In this mode a subdirectory of hls_path is created for each stream. Default=off.
  176. hls_cleanup on; # on|off. Nginx cache manager process removes old HLS fragments and playlist files from hls_path. Default=on.
  177. hls_fragment_naming system; # system = use system time. sequential = use increasing integers. timestamp = use stream timestamp. Default=sequential.
  178. hls_fragment_slicing plain; # plain|aligned. Plain: switch fragment when target duration is reached. Aligned: switch fragment when incoming timestamp is a
  179. # multiple of fragment duration. Makes it possible to generate identical fragments on different nginx instances. Default=plain.
  180. ## ENCRYPTING YOUR LIVE-STREAM - Only works if you have certificates setup in your http server block (example shown earlier in this config file) ##
  181. hls_keys on; # on|off. Enables HLS encryption. AES-128 method is used to encrypt the HLS fragments. Requires ssl module. Default=off.
  182. hls_key_path /tmp/keys; # Sets the directory where auto-generated HLS keys are saved. Default= hls_path.
  183. hls_fragments_per_key 100; # Sets the number of HLS fragments encrypted with the same key. 0 means only one key is created at the publish start and
  184. # all fragments within the session are encrypted with this key. Default=0.
  185. hls_key_url https://example.com/keys/; # Sets url for HLS key file entries. When empty it assumes hls_path. Default= empty.
  186.  
  187. ## HLS_VARIANT - Used for variable bitrate streaming. Please read: https://github.com/arut/nginx-rtmp-module/wiki/Directives#hls_variant ##
  188. ## When hls_variant suffix is matched on stream name then variant playlist is created for the current stream with all entries specified by hls_variant
  189. ## directives in current application. Stripped name without suffix is used as variant stream name. The original stream is processed as usual.
  190. ## Optional parameters following the suffix are appended to EXT-X-STREAM-INF in m3u8 playlist. See HLS spec 3.3.10. EXT-X-STREAM-INF for full list.
  191.  
  192. hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution _low
  193. hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution _mid
  194. hls_variant _high BANDWIDTH=1152000; # Higher-than-SD resolution _high
  195. hls_variant _higher BANDWIDTH=2048000; # High bitrate, HD 720p resolution _higher
  196. hls_variant _src BANDWIDTH=4096000; # Source bitrate, source resolution _src
  197. }
  198.  
  199. ## MPEG-DASH ##
  200. application dash { # These variables will be used if we enable to push the stream to this application.
  201. live on; # on|off. Enables this application and allowing live streaming to it. Default=on.
  202. dash on; # on|off. Toggles MPEG-DASH on the current application.
  203. dash_path /tmp/dash; # Location to store the video fragment files. Will be created if it doesn't exist.
  204. dash_fragment 5s; # Sets DASH fragment length in seconds or minutes. Default= 5s.
  205. dash_playlist_length 30s; # Sets MPEG-DASH playlist length. Defaults= 30s.
  206. dash_nested on; # on|off. In this mode a subdirectory of dash_path is created for each stream. Default=off.
  207. dash_cleanup on; # on|off. Nginx cache manager process removes old DASH fragments and playlist files from dash_path. Default=on.
  208. }
  209.  
  210. ## TWITCH - Only required if you decide to re-stream using an application ##
  211. application twitch {
  212. live on;
  213. record off;
  214. allow publish 127.0.0.1;
  215. deny publish all;
  216. push rtmp://live-ams.twitch.tv/app/live_YOUR-LIVE-STREAM-KEY;
  217. }
  218.  
  219. ## YOUTUBE - Only required if you decide to re-stream using an application ##
  220. application youtube {
  221. live on;
  222. record off;
  223. allow publish 127.0.0.1;
  224. deny publish all;
  225. push rtmp://a.rtmp.youtube.com/live2/YOUR-LIVE-STREAM-KEY;
  226. }
  227.  
  228. application test {
  229. live on;
  230. record off;
  231. allow publish all;
  232. allow play all;
  233. }
  234.  
  235. ## ACCESS LOG - This section has too many options to include. Please see https://github.com/arut/nginx-rtmp-module/wiki/Directives#access-log ##
  236. access_log /var/log/ninx/rtmp_access.log; # off|path [format_name]. Default logging is done to same file as HTTP logger.
  237.  
  238. ## NOTIFY ##
  239. ## Notifications use HTTP callback to inform subscribers that stream has started. You will need a website that can handle these. ##
  240. ## These option go beyond the scope of this configuration file as it contains lots of info. Please visit this url for more info: ##
  241. ## https://github.com/arut/nginx-rtmp-module/wiki/Directives#notify ##
  242.  
  243. # publish_notify on; # on|off. Send "NetStream.Play.PublishNotify" and "NetStream.Play.UnpublishNotify" to subscribers. Default=off.
  244. # on_connect http://www.example.com/plugin/Live/on_publish.php;
  245. # on_play http://www.example.com/plugin/Live/on_play.php;
  246. # on_record_done http://www.example.com/plugin/Live/on_record_done.php';
  247. ## These are some examples to use if you would have a website running https://github.com/WWBN/AVideo ##
  248.  
  249. } # CLOSING BRACKET FOR SERVER BLOCK
  250. } # CLOSING BRACKET FOR RTMP BLOCK
Advertisement
Add Comment
Please, Sign In to add comment