Live Video Streaming Server with Nginx

Having an RTMP live streaming server is useful for accepting one or more incoming video streams. These streams can be rebroadcast to multiple streaming services and you could show a standby video when there is a momentary loss of connectivity. IRL (In Real Life) streamers often use these standby videos so they don’t lose viewers when they hit a cellular dead zone.

There are commercial platforms like Wowza that are easy to setup but cost hundreds of dollars per year per server in addition to server rental and bandwidth fees. There is a little known open source solution that you can use for free called Nginx and Nginx-RTMP-Module.

Ubuntu 18.04 LTS has a precompiled package for NGINX RTMP, so you can avoid having to compile NGINX and Nginx-RTMP-Module manually.

Install nginx and rtmp module

sudo apt-get install libnginx-mod-rtmp nginx -y

After installing Nginx you’ll need to change the config to activate the RTMP model. See the following sample config file. This configuration is designed to be used in combination with OBS so that OBS does a scene change with the bitrate is too low. This configuration isn’t fully secure and needs to have IP restrictions or other access controls added, otherwise anyone could stream to this server.

worker_processes 1;

events {
        worker_connections 4096;
}

http {
    include mime.types;
    default_type application/octet-stream;
    ignore_invalid_headers on;

    server {
        listen      80;
        server_name localhost;

        # This URL provides RTMP statistics in XML
        location /stat {
            if ($request_method = "GET") {
                add_header "Access-Control-Allow-Origin"  *;
            }

            rtmp_stat all;

            # Use this stylesheet to view XML as web page
            # in browser
            rtmp_stat_stylesheet /stat.xsl;
        }

        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            # root /path/to/stat.xsl/;
            root "/";

        }

        location /control {
            rtmp_control all;
        }
    }
}

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;
        chunk_size 8192;
        ack_window 8192;

        # Stream to "rtmp://HOSTNAME/publish/live".
        application publish {
            live on;
            wait_video on;
            wait_key on;
            exec_options on;
            publish_notify on;
            play_restart on;
            drop_idle_publisher 6s;
            idle_streams off;
        }
    }
}

Restart nginx then hit http://ip-address-of-server/stat and you should see something like:

This interface shows your active RTMP connections and bandwidth.

Leave a Reply

Close Menu
×
×

Cart