Your own Restream Server (Nginx RTMP | Ubuntu 20.04 VPS | Windows Desktop) – Mikulski
Site Overlay

Your own Restream Server (Nginx RTMP | Ubuntu 20.04 VPS | Windows Desktop)

Well, the free streaming services I wrote about earlier do a great job with their function. But in the context of 24/7 broadcasts with a large number of platforms (for example, radio, as in my case), this is still not the best option: random disconnections from the server, dropping the broadcasting script (on average, once every two weeks); a small number of one-time connections or lack of support for the desired site, due tofor which you have to take the “millipede” out of the services, or switch to paid tariffs (which you still need to be able to pay).
Being a person who has just joined the relative wilds of web technologies, I did not immediately think of a logical step: to look for solutions to launch my own restream server on a VPS. And when I thought about it, I decided that the simplest configuration of my machine (1 CPU, 1 GB RAM) would not handle such functions, especially when paired with a broadcasting script.
In general, my prejudices significantly delayed the moment when I finally came to the Nginx server and its RTMP module, which turned out to be an excellent solution for my 24/7 radio stream:

  • Stable operation and no connection interruptions (well, except when the problem is on the side of the VPS hoster).
  • Easy and fast setup without any problems.
  • Minimum CPU load (literally 1% in my case).
  • The ability to add any sites by inserting rtmp addresses with a key and in the amount that your Internet channel will allow (for example, your speed is 100mb/sec, the stream bitrate is 3mb/sec (3000kb/s) -> you can retransmit to 30 channels).
  • Of the minuses, it can be noted that it is not possible to disable/restart individual channels without restarting conditional OBS (but I do not exclude that I simply did not find a way).

In fact, there are quite a lot of guides and various information on Nginx-RTMP, because this is a fairly popular way of relaying among streamers (yes, Nginx can be configured on Windows – I’ll tell you about this at the end of the post). For example, when I first set up my server, I used these source:

Therefore, I did not see the point in retelling what I had learned and did not take up this post. But after thinking about it for a while, I changed my mind: nevertheless, this is an essential part of my experience in creating a 24/7 stream, which I decided to share from the very beginning, trying to give information to those who, like me, are dealing with VPS, Linux and other things for the first time..

In the guide that goes on, I will talk about installing and configuring Nginx on Ubuntu 20.04 and briefly about Windows.

NGINX

The first step is to install the Nginx server itself:

sudo apt update
sudo apt install nginx

After installation in the directory: /etc/nginx it will start automatically. This can be checked with the command:

systemctl status nginx

The log will display information where, among other things, there will be the line “Active”:

Active: active (running) since ...

More Nginx management commands that will come in handy:

Stop server

sudo systemctl stop nginx

Start server

sudo systemctl start nginx

Restart the server (stop, then start)

sudo systemctl restart nginx

Reload the server (in fact, apply the changes after editing the config file without disconnecting connections. Streams will not fall! But the individual channels that you removed from the config file will not stop either).

sudo systemctl reload nginx

NGINX – RTMP

Now you can install the RTMP module:

sudo apt install libnginx-mod-rtmp

Ready! Next, you need to edit the nginx.conf, configuration file, which will indicate where to receive the stream from and to which recipients to duplicate it.
Any text editor will do, for example nano:

sudo nano /etc/nginx/nginx.conf

Without touching the already existing contents of the file, you need to go down to the very bottom and add such a block there:

rtmp {
        server {
                listen 1935;
                chunk_size 4096;
                allow publish 127.0.0.1;
                allow publish your__local_IP_address;
                deny publish all;

                application live {
                        live on;
                        record off;
#TWITCH
       push rtmp://hel03.contribute.live-video.net/app/stream_key;
#YOUTUBE
       push rtmp://a.rtmp.youtube.com/live2/stream_key;
          }
     }
}

What is it all about:

  • listen 1935 – the port that will “listen” to rtmp for connection. 1935 is the standard port.
  • chunk_size 4096 – the size of the block of data that rtmp will send. By default, the standard is also specified.
  • allow publish – from which address it is allowed to publish streams. Here you need to leave the line 127.0.0.1 and add your own with the IP address of your VPS (from which the stream will flow). You can specify other addresses, if necessary. For example, home, to start the stream from OBS at home, and Nginx, hosted on the VPS, distributed the signal further.
  • deny publish all – prohibit the publication of the stream from all other addresses, except those listed above.
  • application where live can be replaced with any other “name”. Simply put, this is the name/part of the address of the “receiver” of the stream.
    In other words, if you stream via OBS, then the rtmp string with the address where to send the stream will look like: rtmp://localhost/live
  • live on – enabling the mode in which multiple users can connect to the stream at the same time.
  • record off – disabling the recording mode so that streams are not saved to disk.
  • #SERVICE_NAME – the “#” symbol is used for commenting and does not affect the code. Therefore, it is very convenient to name services so as not to get confused in rtmp addresses.: which address belongs to which site.
    The block closes and the next one opens:
  • push rtmp:// – here you need to insert the URL of the server and the broadcast key that the platform issues.

I think this is clearly shown in the code example and there will be no difficulties.
The main thing is to monitor the opening and closing of “{}” and as a guideline remember that at the very bottom of the file, after the text there should be three closing curly brackets.
In any case, you should not be afraid: the syntax (after saving the file) can always be checked with the command. If it returns an error, it means that somewhere you missed “;” or “{}”.

sudo nginx -t

Also, you can add a second “application” with a different set of platforms for the restream. This is convenient if there is a need to conduct one stream, for example, only on youtube and twitch, and the second stream is only on vkplay and trovo:

rtmp {
        server {
                listen 1935;
                chunk_size 4096;
                allow publish 127.0.0.1;
                allow publish your_local_IP_address;
                deny publish all;
#First setup:
                application live {
                        live on;
                        record off;
#TWITCH
       push rtmp://hel03.contribute.live-video.net/app/stream_key;
#YOUTUBE
       push rtmp://a.rtmp.youtube.com/live2/stream_key;
          }
#Second setup:
                application live2 {
                        live on;
                        record off;
#VKPLAY
        push rtmp://vsu.mycdn.me/app/stream_key;
#TROVO
        push rtmp://livepush.trovo.live/live/stream_key;
          }
     }
}

That’s it, the file can be saved and closed (for the nano editor – Ctrl+X, confirm – Y, then enter).

If you are using a Firewall, then you need to open 1935 port:

sudo ufw allow 1935/tcp

After that, you should restart Nginx so that the config file changes take effect:

sudo systemctl reload nginx.service

Great! Now it remains to drive the rtmp address into the conditional OBS: rtmp://your_vps_ip/live (remember that where live is the name that we set in the first line of application) and start the stream.
The field with the key can be left empty, or you can enter anything, for example, test – this, in fact, does not affect anything. You can simply connect to the stream directly, for example, through a VLC player, where the URL will be: rtmp://your_vps_ip/live/test.

Statistics

Also, within Nginx_RTMP, it is possible to output statistics to an http page of a similar type:

In my opinion, there is no benefit from this: only numbers that do not affect anything. Just an extra hassle with setting up Firewall rules so that no outsider can access this page…


At first, in the /etc/nginx/sites-available directory, you need to create file named rtmp :

sudo nano /etc/nginx/sites-available/rtmp

Fill this file with the following contents:

server {
    listen 8080;
    server_name  localhost;

    # rtmp stat
    location /stat {
        rtmp_stat all;
        rtmp_stat_stylesheet stat.xsl;
    }
    location /stat.xsl {
        root /var/www/html/rtmp;
    }

    # rtmp control
    location /control {
        rtmp_control all;
    }
}

Now you need to extract stat.xsl file to a specific directory from the archive that was downloaded along with the Rtmp-module when it was installed:

sudo mkdir /var/www/html/rtmp
sudo gunzip -c /usr/share/doc/libnginx-mod-rtmp/examples/stat.xsl.gz > /var/www/html/rtmp/stat.xsl`

If after entering the second line, an empty line is displayed: “>”, press the cursor up to call the previous command again and press Enter.

Next, you need to create a Firewall rule so that you can access the statistics page from a specific IP:

sudo ufw allow from your_ip_address to any port 8080

Ready! By clicking on the link: http://your_vps_ip:8080/stat you can formally track what happens to the streams.

Here it only remains to add that in Ubuntu, by default, Firewall is disabled. And I strongly recommend studying its configuration and management before activating it, and only then connecting RTMP statistics.

Windows

To activate the server on Windows, you need to download the package from github, which already includes Nginx and Rtmp module: https://github.com/illuspas/nginx-rtmp-win32
The archive is unpacked to any convenient location, then the nginx.conf file is edited in Conf folder with “Notepad” according to the same principle as describred above. Unless in allowpublish, you can add nothing except “127.0.0.1”, and in OBS to output the stream, specify rtmp://localhost/live/.
To start the server, you need to open a file in the root folder nginx.exe (it will seem that nothing will happen, but in fact, the server will start) and you can start streaming.
The Stop.bat executable file is used to stop the server.
It is important to understand that after restarting the PC, the server stops working and it needs to be started again via nginx.exe.

0 comments
Inline Feedbacks
View all comments