Advertisement
ustoopia

nginx.conf

Aug 1st, 2023 (edited)
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.72 KB | Source Code | 0 0
  1. #### ======================================================================================================== ####
  2. #### NGINX.CONF ####
  3. #### Go to this page for a list of all variables: https://github.com/arut/nginx-rtmp-module/wiki/Directives ####
  4. #### Go to this site for many more configuration examples: https://github.com/arut/nginx-rtmp-module ####
  5. #### This Nginx config file was put together by "ustoopia" for a setup tutorial at https://www.ustoopia.nl ####
  6. #### ======================================================================================================== ####
  7.  
  8. user www-data; # The server will run as this user & group.
  9. worker_processes 1; # Using 1 will results in a reliable /stat page. Set to auto or a specific number.
  10. pid /run/nginx.pid;
  11. include /etc/nginx/modules-enabled/*.conf; # Include all the .conf files located in this folder.
  12.  
  13. events {
  14. worker_connections 768; #
  15. # multi_accept on;
  16. }
  17. http {
  18. sendfile off; # Default=on. Toggles the use of sendfile. For optimal HLS delivery disable this.
  19. tcp_nopush on; # Default=off. Sends the response header and beginning of a file in one packet.
  20. tcp_nodelay on; # Default=on. Forces a socket to send data in its buffer, whatever packet size.
  21. server_tokens off; # Default=on. Enables/disables the server signature.
  22. keepalive_timeout 65;
  23. types_hash_max_size 2048;
  24. server_name_in_redirect off;
  25. server_names_hash_bucket_size 64;
  26. default_type application/octet-stream;
  27.  
  28. ssl_protocols TLSv1.2 TLSv1.3;
  29. ssl_prefer_server_ciphers on;
  30.  
  31. #### This logging format is optimal for Amplify monitoring (https://amplify.nginx.com) ####
  32. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  33. '$status $body_bytes_sent "$http_referer" '
  34. '"$http_user_agent" "$http_x_forwarded_for" $request_time';
  35.  
  36. access_log /var/log/nginx/access.log main;
  37. error_log /var/log/nginx/error.log warn;
  38.  
  39. gzip off; # Compresses responses using gzip method. Helps to reduce size of transmitted data by half or more.
  40. # gzip_vary on;
  41. # gzip_proxied any;
  42. # gzip_comp_level 6;
  43. # gzip_buffers 16 8k;
  44. # gzip_http_version 1.1;
  45. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  46.  
  47. include /etc/nginx/mime.types; # This will include the mime.types. Do not remove this.
  48. include /etc/nginx/conf.d/*.conf; # This line will include all the config files in this folder to our configuration.
  49. include /etc/nginx/sites-enabled/*; # This line will include all the virtual hosts file to our configuration.
  50.  
  51. } # HTTP closing tag
  52.  
  53. #### RTMP SERVER ####
  54. rtmp {
  55. server {
  56. listen 1935; # The port where RTMP will listen on. Default = 1935.
  57. chunk_size 4096;
  58. timeout 30s;
  59. buflen 1s;
  60.  
  61. #### THE /LIVE APPLICATION WILL BE USED TO SEND OUR LIVESTREAM TO, INCLUDING A STREAMKEY. ####
  62. application live { # The /live application will receive the incoming live-stream.
  63. live on; # Enable/disable the live application.
  64. interleave off; # Audio and video data is transmitted on the same RTMP chunk stream. Default=off.
  65. wait_key off; # Makes video stream start with a key frame. Default=off.
  66. wait_video off; # Disable audio until first video frame is sent (can cause delay). Default=off.
  67. idle_streams off; # If disabled prevents viewers from connecting to idle/nonexistent streams and disconnects all.
  68. drop_idle_publisher off; # Drop publisher that has been idle for this time.
  69. allow publish all; # Default=all. Anybody can stream to our server. Change this to restrict this behaviour.
  70. # deny publish all; # Enable this in case you set an IP address for previous option allow publish.
  71. # deny publish 10.0.0.9 # Example of how to deny access from a specific IP. Uncomment to use.
  72. allow play 127.0.0.1; # Diables direct playback and restricts it to localhost. To allow all playback change to: all
  73. # deny playback all; # Uncomment to truly allow only playback on localhost (this includes ffmpeg)
  74.  
  75. #### We aren't using these next few lines here. Serves as an example of how to implement potential authentification ####
  76. # on_publish http://127.0.0.1/streamauth/auth.php; # Event is triggered when livestreaming starts.
  77. # on_publish_done http://127.0.0.1/streamauth/deauth.php; # Event is triggered when livestreaming stops
  78. # on_play http://127.0.0.1/streamauth/play.php; # Event is triggered when a vierwer starts watching
  79.  
  80. #### OPTIONS TO PUSH OR FORWARD THE INCOMING VIDEO STREAM TO ####
  81. # push rtmp://localhost/hls; # Push the source stream as-is to /hls to create a single HLS stream. Low CPU usage.
  82. # push rtmp://localhost/dash; # Push the source stream to the /dash app to create a DASH outgoing stream. Low CPU usage.
  83. # push rtmp://localhost/rec; # Enable this to record the source streams. Also see /rec application below!
  84. # exec_push /etc/nginx/ffmpeg/transcode.sh; # Alternative to the lines below, placing ffmpeg command in a seperate file.
  85.  
  86. #### FFMPEG WILL GRAB THE SOURCE STREAM AT /live, CREATE THE VARIANTS, AND SENDS THEM TO /hls | HIGH CPU! ####
  87. #### IF YOU DON'T NEED THIS MANY ADAPTIVE STREAMS, OR CPU USAGE IS TOO HIGH, DISABLE ONE OR MORE OF THESE LINES. ####
  88.  
  89. exec_push ffmpeg -i rtmp://localhost/live/$name -async 1 -vsync -1
  90. -c:v libx264 -c:a aac -b:v 256k -b:a 32k -vf "scale=480:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_low
  91. -c:v libx264 -c:a aac -b:v 768k -b:a 96k -vf "scale=720:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_mid
  92. -c:v libx264 -c:a aac -b:v 1024k -b:a 128k -vf "scale=960:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_high
  93. -c:v libx264 -c:a aac -b:v 1920k -b:a 128k -vf "scale=1280:trunc(ow/a/2)*2" -tune zerolatency -preset veryfast -crf 23 -f flv rtmp://localhost/hls/$name_higher
  94. -c copy -f flv rtmp://localhost/hls/$name_src;
  95.  
  96. #### AN ALTERNATIVE METHOD TO CREATE THE ADAPTIVE BITRATE STREAMS. DISABLE THE PREVIOUS ONE BEFORE TESTING | HIGH CPU USAGE! ####
  97. #exec_push ffmpeg -i rtmp://localhost/live/$name -async 1 -vsync -1
  98. #-c:a aac -strict -2 -b:a 128k -c:v libx264 -vf scale=-2:240 -g 48 -keyint_min 48 -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 400k -maxrate 700k -bufsize 1200k -b:a 96k -r 20 -f flv rtmp://localhost/hls/$name_low
  99. #-c:a aac -strict -2 -b:a 128k -c:v libx264 -vf scale=-2:480 -g 48 -keyint_min 48 -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 1200k -maxrate 2100k -bufsize 2400k -b:a 128k -f flv rtmp://localhost/hls/$name_med
  100. #-c:a aac -strict -2 -b:a 128k -c:v libx264 -vf scale=-2:720 -g 48 -keyint_min 48 -sc_threshold 0 -bf 3 -b_strategy 2 -b:v 2400k -maxrate 4200k -bufsize 100000k -b:a 128k -f flv rtmp://localhost/hls/$name_high
  101. #-c copy -f flv rtmp://localhost/hls/$name_src;
  102. }
  103.  
  104. #### THE /HLS APPLICATION AUTOMATICALLY CREATES THE FILES FOR THE OUTGOING HLS STREAM. ####
  105. application hls { # The /hls app will reads the input and create a useable outgoing HLS stream.
  106. hls_path /mnt/livestream/hls; # Location where the temporary video fragment files will be stored.
  107. hls on; # This makes sure that a m3u8 index file will be created to play our .ts video files.
  108. live on; # This enables or disables this application.
  109. hls_fragment 10; # Sets HLS fragment length in seconds or minutes. Default = 5s.
  110. hls_playlist_length 50; # Sets HLS playlist length in seconds or minutes. Default = 30s.
  111. # hls_sync 100ms; # Timestamp threshold. Prevents crackling noise after conversion from low (1KHz) to highres(90KHz)
  112. hls_nested on; # In this mode a subdirectory is created for each stream under hls_path.
  113. hls_type live; # Options: live|event. Live plays from the current position. Event plays from start of playlist.
  114. hls_continuous off; # HLS sequence number is started from where it stopped last time. Old fragments are kept.
  115. hls_cleanup on; # Automatically clean up temp files. When turned on, it automatically negates hls_continues
  116. allow publish 127.0.0.1; # Only localhost (this includes /live feed) is allowed to publish to the /hls application
  117. # deny publish all; # Uncomment this to truly restrict publish to localhost.
  118. allow play all; # Allow everybody to play the HLS streams.
  119.  
  120. #### HLS VARIANTS ####
  121. # When hls_variant suffix is matched on stream-name, a variant playlist is created for current stream with all entries specified
  122. # Stripped name without suffix is used as variant stream name. The original stream is processed as usual.
  123. # Make sure you use the same naming convention as used in the output names of the ffmpeg commands above.
  124.  
  125. hls_variant _low BANDWIDTH=288000; # Low bitrate, sub-SD resolution
  126. hls_variant _mid BANDWIDTH=448000; # Medium bitrate, SD resolution
  127. hls_variant _high BANDWIDTH=1152000; # Higher-than-SD resolution
  128. hls_variant _higher BANDWIDTH=2048000; # High bitrate, HD 720p resolution
  129. hls_variant _src BANDWIDTH=4096000; # Copy of source stream. Source bitrate, source resolution.
  130. }
  131.  
  132. #### DASH APPLICATION CREATES THE FILES THAT WILL FORM THE OUTGOING DASH STREAM ####
  133. application dash {
  134. dash on; # on|off. Toggles MPEG-DASH on the current application.
  135. live on; # Enables or disables the application.
  136. dash_path /mnt/livestream/dash; # Location to store the video fragment files. Will be created if it doesn't exist.
  137. dash_fragment 5s; # Sets DASH fragment length in seconds or minutes. Default= 5s.
  138. dash_playlist_length 30s; # Sets MPEG-DASH playlist length. Defaults= 30s.
  139. dash_nested on; # In this mode a subdirectory of dash_path is created for each stream. Default=off.
  140. dash_cleanup on; # Cache manager process removes old DASH fragments and playlist files from dash_path.
  141. allow publish 127.0.0.1; # Allow only localhost to publish to /dash
  142. allow play all; # Allow everybody to play these streams.
  143. }
  144.  
  145. ##### RECORDING /rec #####
  146. application rec {
  147. live on; # on|off. Enables this application and allowing live streaming to it. Default=on.
  148. record all; # Records everything sent to /rec. Options are: off|all|audio|video|keyframes|manual
  149. record_unique on; # Appends timestamp to files. Otherwise the same file is over-written
  150. record_path /mnt/livestream/rec; # Location where the recordings will be stored. Adjust this according to your needs.
  151. record_suffix -%d-%b-%y-%T.flv; # Added to filenames. Example uses 'strftime' results in: -24-Apr-13-18:23:38.flv.
  152. # record_max_size 128K; # Set maximum file size of the recorded files.
  153. # record_max_frames 200; # Sets maximum number of video frames per recorded file.
  154.  
  155. ##### OPTIONAL! WHEN RECORDING FINISHES, CONVERTS .FLV FILES TO .MP4 | HIGH CPU USEAGE WHILE IT RUNS ####
  156. exec_record_done ffmpeg -i $path -c copy $dirname/$basename.mp4;
  157. }
  158.  
  159. } # SERVER closing tag
  160. } # RTMP closing tag
Tags: nginx.conf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement