Creating an NGINX Video Relay Server with Live Player

Based on these links:

https://www.youtube.com/watch?v=VHUPGfgkIgw

https://www.youtube.com/watch?v=eD88NZN6N6Q

https://www.youtube.com/watch?v=Ap_ZxQ0voqE&list=PL10Ao–Mok8MgVMkUqsqnnYRoFtL9P81m&index=5


For this I started with creating a Debian 10 droplet on Digital Ocean…

Once the droplet has been created, SSH and WinSCP into the machine as root…

  1. Remember to update then reboot.
apt-get update
apt-get upgrade
apt-get dist-upgrade
reboot

2. Download the NGINX package (at time of writing the version was 1.18.0), expand and delete the download.

http://nginx.org/en/download.html

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -xvf nginx-1.18.0.tar.gz
rm nginx-1.18.0.tar.gz

3. Install git and clone the RTMP module:

https://github.com/arut/nginx-rtmp-module

apt-get install git
git clone https://github.com/arut/nginx-rtmp-module

4. Change to the NGINX folder and compile.

You will also need a few other installations to make this work…

cd nginx-1.18.0
apt-get install build-essential
apt-get install libpcre3 libpcre3-dev libssl-dev
apt-get install zlib1g zlib1g-dev

5. Configure:

http://nginx.org/en/docs/configure.html

./configure --with-http_ssl_module \
--add-module=../nginx-rtmp-module \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-pcre \
--pid-path=/run/nginx.pid

6. Make and Install:

make
make install

7. Set up to run as service:

I find easiest to use WinSCP. Go to /etc/systemd/system and create a new file nginx.service. Paste in the following:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
User=root
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8. Set up the config file:

I find easiest to use WinSCP. Go to your /etc/nginx folder and edit nginx.conf. Paste in the following:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live-<add random string here for stream link from obs> {
            record off;
            live on;

            push rtmp://a.rtmp.youtube.com/live2/<your youtube stream key>;

            # Turn on HLS
            hls on;
            hls_path /usr/local/nginx/html/hls/;
            hls_fragment 3;
            hls_playlist_length 60;
            ## disable consuming the stream from nginx as rtmp
            #deny play all;
        }
    }
}

9. Add Video.js and the hsl folders:

https://github.com/videojs/video.js/releases

Current version at time of writing is v7.11.0

cd /usr/local/nginx/html
mkdir hls
mkdir video.js
cd video.js
apt-get install unzip
wget https://github.com/videojs/video.js/releases/download/v7.11.0/video-js-7.11.0.zip
rm video-js-7.11.0.zip

10. Create a player.html file.

Using WinSCP, create stream.html in /usr/local/nginx/html and add the following:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Live Streaming</title>
    <link href="/video.js/video-js.min.css" rel="stylesheet">
    <script src="/video.js/video.min.js"></script>
</head>
<body>
<video id="player" class="video-js vjs-default-skin" height="360" width="640" controls preload="none">
    <source src="/hls/stream.m3u8" type="application/x-mpegURL" />
</video>
<script>
    var player = videojs('#player');
</script>
</body>
</html>

11. Run the service and enable it:

systemctl daemon-reload
systemctl start nginx
systemctl enable nginx

Browse to your IP and you should see the NGINX welcome page:

You can stop the service with:

systemctl stop nginx

And it should restart automatically when you start the machine.