How to set up your own private RTMP server using nginx

All Versions How to set up your own private RTMP server using nginx

GillyMoMo

Member
Nginx-RTMP is an open-source extension module for the Nginx web server that can be used as a media streaming server for both live streams and video on demand using RTMP. This doesn’t come pre-packaged for the operating system, so we’ll need to build Nginx with this module from source code. The first thing we need to do is get some bits. To do so, enter the following:
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev unzip
This will take some time. Once these are installed we then need to grab the nginx source code. Version 1.8.1 is the latest stable version so we will use that one:
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.8.1.tar.gz
tar -zxvf nginx-1.8.1.tar.gz
The next thing we will need is the source for Nginx-RTMP:
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
unzip master.zip
At this point, we should have a directory named nginx-1.8.1 which contains the Nginx source code, and one named nginx-rtmp-module-master which contains the Nginx-RTMP source code. The next step is to reconfigure the Nginx source to compile with the Nginx-RTMP module:
cd nginx-1.8.1
./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master
You should see a scroll of text while it is configured, after which you can make and install nginx:
make
sudo make install
At this point Nginx will be installed into the /usr/local/nginx directory. To test everything is working let’s fire Nginx up:
sudo /usr/local/nginx/sbin/nginx
If everything is working as expected, you should now get the Nginx test page if you navigate to your server’s IP address in a web browser. To stop Nginx you need to call the program again and give it the stop command:
sudo /usr/local/nginx/sbin/nginx -s stop
Now you will need to add the code to configure the RTMP module. This is done in the default config file which is stored with the other files. I’m going to use nano here, but other text editors are available:
sudo nano /usr/local/nginx/conf/nginx.conf
Go to the end of the file and paste in the following configuration:
rtmp {
server {
listen 1935;
chunk_size 8192;
application vod {
play /usr/local/nginx/rtmp;
}
}
}
Save and exit the file. In this file, we’ve told Nginx to listen on port 1935 for RTMP, which is the default port. We’ve also set it to use a chunk size in transfers of 8192 bits. Next, we’ve created an “application” called vod for video on demand. You can have as many of these as you wish, and name them anything you like. We’ve then told it that the vod application will play files from /usr/local/nginx/rtmp directory. This directory doesn’t actually exist yet, so you will need to create it and place some media into it. Note that Nginx-RTMP can only serve flash flv video and mp4 video.
sudo mkdir /usr/local/nginx/rtmp
The next thing to do is start Nginx again, at which point everything is configured and ready for use:
sudo /usr/local/nginx/sbin/nginx
To test you just need to open a stream from your server. The easiest method is to use VLC media player. To open this, go to the “Media” menu and then select “Open Network Stream”. A window will open for you where you can enter the URL for your media.
The URL will start with rtmp:// to tell VLC the protocol to use, and then the domain name or IP address of your server. Next will be a slash, then your application name, in our case “vod”, another slash and finally the filename of the file. So as an example:
rtmp://your-domain.com/vod/test.mp4
rtmp://your-domain.com/vod/test.flv
This will have more control over your live streaming platform like twitch on iOS, Windows, Linux etc.
So there you have it – a simple streaming server using RTMP based on open source components. Happy streaming!

Just as an FYI the rtmp module for nginx is in the repos now. You don't have to compile from source anymore.You just have to look ;). That reminds me I should get back on my distro that I have abandoned over the years.
 

Pepito-06

New Member
Hi guys, thanks for the procedure. I've tried in under wsl (Ubuntu running on Windows 10). I managed to run the procedure without error, but i'm not able to stream it from VLC.
Is there an up-to-date procedure ?Thanks
 

ven000m

New Member
Could you update your Tutorial. We need "unzip" and "zlib1g" "zlib1g-dev" as well in the apt-get install step.

Code:
apt-get install unzip build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev
 

vansontv

Member
I use obs to stream youtube using obs-multi to play FB but obs-multi broadcast to FB only 48 minutes, please guide me how to adjust
 

Mark Weiss

Member
It's been three years since I unsuccessfully tried streaming through nginx. I spent hundreds of hours trying to get it to work in 2018 and finally had to move on to other things.
However, yesterday, I found a video by Doug Johnson on Youtube "Setup Your Own RTMP + Web Streaming Video Server", where he provides step by step instructions and a custom config for nginx to set it up for RTMP streaming. So I thought, great, a turnkey solution that will work.
Only it didn't. At first, I could not get nginx to run on Windows 10. It kept complaining it couldn't open this log or that config file, even though all were in exactly the places that the Doug Johnson video demonstrated. Eventually, I somehow hacked my way pas that and got nginx running (though it's invisible in task manager) because I could stream to it and get a 'green' status indicator in OBS. And.. the /hls folder was seeing Stream1.ts files appearing every few seconds, so the fragments are being produced. Oddly the thumbnails are all black and don't represent what is in the streamed video.
But the main problem is that when I open the Viewer.html file, all I get is the js player, but no video. I've checked again and again to make sure all paths were matching the config file and they are. It just won't play the video.

hls on;
hls_path /nginx/srv/hls/;
hls_fragment 3;
hls_playlist_length 60;


I'm so close, but still stuck on the last step. It all went without a hitch in Doug's video and I followed his instructions to the tee, but it's not working here. I don't know what the problem is.
 

Mark Weiss

Member
It looks like nginx is running, but I cannot find it in Task manager.

1638484781786.png


OBS is streaming to it on "rtmp://192.168.0.102/live" with a healthy status.
1638484838856.png


The Viewer.html produces this result:
1638484959335.png
 

Mark Weiss

Member
If I run the Start.bat as administrator, I see that Windows cannot find nginx.exe. This works on Doug's system, but not my vanilla 'out of the box' Win 10 system.

Batch contents:

@Echo Off
nginx.exe -s quit
start nginx.exe

Result when started as admin:

1638486011640.png
 

Mark Weiss

Member
Finally got nginx to run (as verified by Task Manager),

I have OBS streaming, the HLS folder on the server is filling up with 3.35MB video "fragments", and the ports are open on the router, and config of nginx is setup to run 8080 (due to IIS running). Still, the player, both locally, and online do not show the video I am streaming.

Here is my player application embedded in a test website.

The video fragments in the HLS folder all have black thumbnails?
The player doesn't show video. Neither does the locally opened player on the server.
Everything is set up to the letter of Doug Johnson's instructions in his youtube video on how to do this.
The only thing I can think of is some incompatibility between Windows 21H2 and nginx or the java app.
 

vansontv

Member
I wrote a little program that will help ease the creation of the config file for Windows. You will need to provide your own copy of Nginx with RTMP for it to work properly.
Main Window
9QYKJpQ.png


Config Window (Example)
0LsNKQH.png


nginx.conf with settings from previous window.
ELwmlm8.png

The ingest will allow for up to 5 applications for other people to stream TO so they can be mixed with OBS on the RTMP server and then rebroadcast out from the Stream application. You can use this with another person to produce multicam streams.

I think its operation is fairly straight forward.
Any feedback is appreciated.

https://1drv.ms/u/s!Ar4s89ZajRLcocVjj_y0D_NZVYLmew?e=pM1yRB
I tried using your Nginx but it gives me an error please help
Ảnh chụp màn hình 2021-12-23 072407.png
 
  • Like
Reactions: fei

MaikAntonioLima

New Member
I wrote a little program that will help ease the creation of the config file for Windows. You will need to provide your own copy of Nginx with RTMP for it to work properly.
Main Window
9QYKJpQ.png


Config Window (Example)
0LsNKQH.png


nginx.conf with settings from previous window.
ELwmlm8.png

The ingest will allow for up to 5 applications for other people to stream TO so they can be mixed with OBS on the RTMP server and then rebroadcast out from the Stream application. You can use this with another person to produce multicam streams.

I think its operation is fairly straight forward.
Any feedback is appreciated.

https://1drv.ms/u/s!Ar4s89ZajRLcocVjj_y0D_NZVYLmew?e=pM1yRB

Great! Would you have a step by step of the settings you made?
 

Mark Weiss

Member
I'm wondering if there is something wrong with my code to embed the player. nginx is streaming and creating transport stream files in its designated folder, but the player is blank on my web page. Here's the code I have in the web page:



Code:
<tr>
        <td style="text-align: justify; margin-left: 12; margin-right: 12">&nbsp;
            <video width="640" height="360" controls>
        <source src="/hls/stream1.m3u8"  type="video/mp4; codecs=avc1.42E01E,mp4a.40.2">
        </video></td>
    </tr>
    <head>
     <title>HLS Live Streaming</title>
    <link href="/js/video-js.css" rel="stylesheet" />
</head>
<body>
  <video
    id="my-video"
    class="video-js"
    controls
    preload="auto"
    width="1280"
    height="720"
    data-setup="{}">
    <source src="/hls/stream1.m3u8" type="application/vnd.apple.mpegurl" />
  </video>
  <script src="/js/video.js"></script>
</body>
 

Mark Weiss

Member
Apparently, I can't get Doug Johnson's pre configured nginx rtmp to work even locally. No wonder the player in the web page doesn't work. The local player isn't working.

How do I get this to work? It's been almost four years since I started working on this project. With my streaming being blocked in 249 locations by the corporate servers, I need to set up my own streaming.


1647562639395.png
 

Mark Weiss

Member
Still hacking away at Doug Johnson's configuration for streaming with nginx.
This is a pre-packaged, supposed to be close to "turn key" setup.

Here's a screen recording of what's going on with the setup. You can see that OBS is streaming and and giving a green indicator in the status line.

In the browser, showing port 8080, we can see nginx Gryphon is up and running.

In the Windows 10 server, we see .ts files in the HLS folder.

Task man shows the required 3 nginx tasks.

Here's the screen recording.

OBS Streaming to nginx not Working


I don't know what else could be the problem. I even tried shutting off the firewall for a short time, but the problem remains.

I've looked into alternatives, but there are none currently available. Microsoft discontinues their streaming software.

Funny thing is, in 2005, I could stream live video with Windows XP Home on a laptop with Windows Media Encoder 9. That is no longer available. Every alternative that I followed lead to a dead end or a 404 error or the software download is no longer available.

So I'm back at trying to get nginx to stream. Since April 2018, I've got over 200 hours devoted to troubleshooting this. I need help!
 
This should be straightforward ! I hope someday, people would create an RTMP server for Windows that could be easily installed, even by the layman. Things don´t have to be difficult this way...
 

Tomasz Góral

Active Member
I'm wondering if there is something wrong with my code to embed the player. nginx is streaming and creating transport stream files in its designated folder, but the player is blank on my web page. Here's the code I have in the web page:



Code:
<tr>
        <td style="text-align: justify; margin-left: 12; margin-right: 12">&nbsp;
            <video width="640" height="360" controls>
        <source src="/hls/stream1.m3u8"  type="video/mp4; codecs=avc1.42E01E,mp4a.40.2">
        </video></td>
    </tr>
    <head>
     <title>HLS Live Streaming</title>
    <link href="/js/video-js.css" rel="stylesheet" />
</head>
<body>
  <video
    id="my-video"
    class="video-js"
    controls
    preload="auto"
    width="1280"
    height="720"
    data-setup="{}">
    <source src="/hls/stream1.m3u8" type="application/vnd.apple.mpegurl" />
  </video>
  <script src="/js/video.js"></script>
</body>
Player hls working only on ssl, your nginx must be running as https (default port 443).
on this sample hls file must exists in hls folder.
If you have any problems watch logs file.
 

Mark Weiss

Member
I have a huge update to this problem I've been working on for four years!

As many of you know, I tried Doug Johnson's nginx rtmp server solution for streaming from OBS on my server. I had run into the problem of OBS happily streaming to nginx, but the viewer html file would play a frame or two and freeze. Today, I noticed that on high motion hand held footage shot on a speedboat, the video would actually play for several seconds. More static scenes would freeze.
I played with the bitrate, B frames, constant quality vs variable and nothing had any effect.
Then, I tried changing from the NVENC encoder to NVENC (new) as the encoder. Since making that change, video is playing fluidly from my streamed page. I even checked it on my 4G cellular phone. Took a while to buffer, because of weak cell signal, but it plays on my phone as well.

Test link: Video Stream Test

I need to figure out how to embed the player in my website properly and make it scaleable to the web page. Any hints on coding that would be appreciated!
 

Mark Weiss

Member
Streaming is basically working, but nginx seems to be unreliable at handling a 2mbit/sec stream from OBS.

On someone's suggestion, I tried monaserver and could have OBS stream to that with no dropped frames. I could even set OBS to 92mbit/sec as an extreme test and no dropped frames. So network is robust.

Even IPERF shows a 940mbit/sec throughput between OBS machine and nginx server.

So the problem is the Griffon build of nginx or its config file isn't quite setup right.
 

Tomasz Góral

Active Member
Streaming is basically working, but nginx seems to be unreliable at handling a 2mbit/sec stream from OBS.

On someone's suggestion, I tried monaserver and could have OBS stream to that with no dropped frames. I could even set OBS to 92mbit/sec as an extreme test and no dropped frames. So network is robust.

Even IPERF shows a 940mbit/sec throughput between OBS machine and nginx server.

So the problem is the Griffon build of nginx or its config file isn't quite setup right.
soo your server run on 8080 port, but you html site run on default port 80. That not working.
Once again i run nginx-rtmp for win32 for local computer or linux, my code use player from videojs.com.
Now i use 20 servers.
 

Mark Weiss

Member
The problem with nginx was solved by changing worker threads to 1. Any higher number and OBS disconnects at random times.

As for getting the stream to be SSL, I'm still working on that because some folks are afraid to go to a non secure site and the browser warns them as if they are entering a malware site.

And finally, I have a lag of a bout 40-50 seconds between activity on camera and when it appears in the web player. nginx has a huge playlist with over 20 fragment files. I wonder if that's why? I'd like to get this delay down to 5 seconds or so, but not sure how the fragment and other commands in nginx.conf work.
 
Top