• 59154This month:
  • 424Today:
  • 15Currently online:

LeaseWeb CDN

Streaming Video on Demand with nginx and RTMP Module

In this tutorial we will show a simple way to stream different types of video files using the Nginx server and RTMP modules.

Before starting the work, we will try to offer a small overview over the advantages of using RTMP versus HTTP video delivery.

Real Time Messaging Protocol (RTMP) was initially developed by Macromedia, for streaming on-demand and live media to Adobe Flash applications. It is a TCP-based protocol which maintains persistent connections and is defined as a “stateful” protocol. This means that from the first time a client connects until the time it disconnects, the streaming server keeps track of the client’s actions. On the other side, HTTP is a “stateless” application protocol used to deliver hypermedia information across the Internet worldwide.

Advantages of HTTP over RTMP:

  • Less likely to be blocked by firewalls at different levels in the network; RTMP uses default port 1935, which can be blocked sometimes, especially within corporate firewalls
  • Supported by more CDNs (it supports easier mirroring and edge caching)
  • More expertise in customizing HTTP

Advantages of RTMP over HTTP:

  • Ability to provide multicast support
  • Security and IP protection, using TLS/SSL or RTMPE
  • Seeking: particularly advantageous for long-duration content because the viewer doesn’t have to wait for the video file to load before jumping ahead, as is the case for HTTP-delivered video
  • Reconnect: if there is a network disruption, the client can re-establish a connection, while the video continues to play from the buffer; when the client re-connects, the buffer will begin filling to avoid any disruption in video or audio flow

And now, let’s start to build our streaming server.

To achieve this, we will compile the Nginx source code together with RTMP modules on a Ubuntu 12.04 box.

Logged in as root:

cd ~
mkdir nginx
cd nginx

Install the necessary software and dependencies:

# for compiler and git
apt-get install git gcc make

#for the HTTP rewrite module which requires the PCRE library
apt-get install libpcre3-dev

# for SSL modules
apt-get install libssl-dev

Clone Nginx RTMP module source code from Github:

git clone

Download the Nginx source code and extract it from the archive. At the time when we are writing this blog post, the current version is 1.4.3:

tar zxpvf nginx-1.4.3.tar.gz
cd nginx-1.4.3

Let’s compile Nginx:

./configure --add-module=/root/nginx/nginx-rtmp-module/ --with-http_ssl_module --prefix=/usr/local/nginx-streaming/
make install

Now it is the time to create a configuration file for Nginx. Go to:

cd /usr/local/nginx-streaming/conf
mv nginx.conf nginx.cong.bkp
nano nginx.conf

and paste the configuration below. This configuration is suggested by the documentation of the RTMP module.

worker_processes  1;

events {
    worker_connections  1024;

rtmp {
    server {
        listen 1935;

        chunk_size 4000;

        # video on demand for flv files
        application vod {
            play /var/flvs;

        # video on demand for mp4 files
        application vod2 {
            play /var/mp4s;

# HTTP can be used for accessing RTMP stats
http {
    access_log /var/log/nginx/access-streaming.log;
    error_log /var/log/nginx/error-streaming.log;

    server {
        # in case we have another web server on port 80
        listen      8080;

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            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 /var/www/;

        location /hls {
            # Serve HLS fragments
            types {
                application/ m3u8;
                video/mp2t ts;
            alias /tmp/app;
            expires -1;

To be able to see some nice statistics about the streaming, we must to copy stats.xml in the folder specified in the configuration above.

mkdir /var/www
cp /root/nginx/nginx-rtmp-module/stat.xsl /var/www/

And (re)start the Nginx server with:


It’s time to play!

As we defined in the Nginx configuration, the server can find some mp4 files in /var/mp4s or flv files in /var/flvs.

You can download an mp4 file here, right click and save it in /var/mp4s with the name sample.mp4.

Now, if you have a video player which supports RTMP protocol (eg: VLC), you can play it directly there.

From the VLC menu, Media->Open Network Stream.

In the URL field we input:


Where <IP_OF_THE_SERVER> is the IP or the hostname of the server where we installed Nginx.


And click play.


If you want to show this video on a website, you can use a flash player as Flowplayer or JW Player.

Below we will show how to use Flowplayer to achieve this. First we need to download flowplayer.rtmp-3.2.12.swf (see the download link at the bottom of the page).

The html code below shows how to implement RTMP using Flowplayer. You should just replace the /path/to/background.png, the <IP_OF_THE_SERVER> and the /path/to/flowplayer.rtmp-3.2.12.swf.

    <!-- flowplayer javascript component -->
    <script src=""></script>

<div id="player" style="width:644px;height:276px;margin:0 auto;text-align:center">
    <img src="/path/to/background.png" height="260" width="489" /></div>

$f("player", "", {
    clip: {
        url: 'sample.mp4',
        scaling: 'fit',
        provider: 'hddn'

    plugins: {
        hddn: {
            url: "/path/to/flowplayer.rtmp-3.2.12.swf",

            // netConnectionUrl defines where the streams are found
            netConnectionUrl: 'rtmp://<IP_OF_THE_SERVER>:1935/vod2'
    canvas: {
        backgroundGradient: 'none'

That’s all.

Of course the solution presented in the post above is minimalist and if you need to scale globally with your Video on Demand product, probably you need a CDN solution.

26 Responses to “Streaming Video on Demand with nginx and RTMP Module”

  • […] You can read my last post on LeaseWeb Labs – Streaming Video on Demand with nginx and RTMP Module […]

  • Yoga Darma:

    hi, i’m trying to make streaming VOD that running on web. i’m using your HTML code but it doesn’t want to running the video. but when i using vlc it’s working. can you please help me? sorry i can’t speak English well. thank you

  • Maurits van der Schee (Innovation Engineer):

    @Yoga: Please share some more information about you Nginx configuration, without that it is hard to help you.

  • […] are two main solutions for streaming Adobe RTMP streaming, about which we wrote in the previous blog post and Apple HLS streaming. Anyway, both technologies allow you to play your video as you record it, […]

  • Yoga Darma:

    @Maurits i only follow this tutorial. so my configuration will be the same as this tutorial’s configuration. am i doing something wrong ??if yes can you tell me what it is??
    thank you so much

  • Yoga Darma:

    @Razvan Tudorica : maybe i should try the HLS streaming. can you tell me what is the difference? and why my HTML code is not working? i doing all your instruction in this tutorial. that’s make me curious. thank you so much

  • inan:


    nice tutorial – great job. It’s possible to secure the streaming? Maybe with htaccess or whatever…

  • inan:

    Hey me again

    When i’m typing the command make i get a error like this: make: *** No rule to make target `build’, needed by `default’. Stop

    How can i solve this problem?

  • inan:


    i solved the Problem. I was in the wrong directory :(

    It’s possible to restream from my Live stream from my VLC Server?

  • razvan:

    Hi inan,
    You can secure your stream as described in this blog post written by the author of the rtmp modules.

  • When I stream from OBS to my nginx rtmp VPS server occasionally it’ll lose contact with nginx. The only way to get it to connect back to nginx is to “sudo killall -9 nginx” then “sudo /usr/local/nginx/sbin/nginx” and tada, OBS reconnects automatically and starts streaming again like a champ.

    Can you let me know any specific way to test and see what’s going on with nginx? :(

    -Jamie M.

  • david:

    Hi, Great tutorial thanks, only problem I have is when I try to start the NGINX server I get the following error..

    nginx: [emerg] unknown directive “worker_processes ” in /usr/local/nginx-streaming//conf/nginx.conf:1

    Any ideas why..?

  • Mazen:

    Hi , how to make nginx start on startup? without making it manually.

  • […] Streaming Video on Demand with nginx and RTMP Module | LeaseWeb Labs […]

  • […] You can read my last post on LeaseWeb Labs – Streaming Video on Demand with nginx and RTMP Module […]

  • […] there are two main solutions for streaming Adobe RTMP streaming, that we covered in the previous blog post and Apple HLS streaming. However, both technologies allow you to play your video as you record it, […]

  • WowzaAdmirer:

    Thanks for a post. I’ve been using WMSPanel which is a reporting panel for media servers and now I see that they support reporting for nginx-rtmp-module. I hope this will be useful for you guys:

  • Tim Beyer:

    Hey there,

    I have setup nginx with the rtmp-module on an Ubuntu machine. Right now my rtmp section looks like this and works perfect:

    rtmp {
    server {
    listen 1935;
    chunk_size 4096;

    application live {
    live on;
    record off;
    push rtmp://server/app/key;


    The thing is I don’t want it to drop the stream when someone disconnects with OBS. Instead I want to hold it at least with a black screen, image or better, to play randomly local files from the server. Now I have for testing purposes a .mp4 uploaded to var/mp4s, but haven’t been able yet to make it working. Could you maybe help me out on this one?
    In general I want to make it stream 24/7 – if someone connects with a livestream it stops the videofile playing and let’s the live-stream through. If the streamer disconnects it plays again randomly files from the above folder.

    Hopefully you’ve an idea how to do this.

    Thanks in advance!


  • Maurits van der Schee (Innovation Engineer):

    @Tim: Thanks for your comment. It seems like you have a good setup already. What player are you using? Can’t you implement a smart player that detects the stream (un)availability and gets another playlist on failure? That is probably the way that I would go.

  • Tim Beyer:

    Hi, Maurits – I’m streaming the content to Twitch. So unfortunately I have no access to the player… :/

  • Robin:

    How can I configure the rtmp to rtmps?

  • Maurits van der Schee (Innovation Engineer):

    @Robin: Thank you for your comment. I know FMS and Red5 support RTMPS, but I am not aware of a solution for Nginx. I suppose just terminating the SSL in a reverse proxy setup won’t work or did you not try that (yet)? Sorry that I can’t help you better. Keep us posted on the solution!

  • Arjun:

    Do anyone know how to get the published stream name in config file?

  • Hi,
    Try my project for this
    Easier, a single command and you are done :)

  • Maurits van der Schee (Innovation Engineer):

    @Ioannis: Thank you for this link. I’m going to have a look at it!

Leave a Reply