A simple guide to start rtmp streaming with nginx 1


I was once using nginx, nginx-rtmp-module and EasyRTMP(http://rg4.net/easyrtmp) to streaming live video with rtmp mode.

It was just for fun, and I didn’t find a chance to utilize it in work. I suggested it to my boss year ago, but it was turned down, comparing to the works on schedule it was always a minor factor to the products. So one day when I found my disk out of usage, I re-installed my OS without backup any data of the previous working environment.

Days ago, a formal colleague asked me some questions about rtmp streaming by using nginx. Happened to have the time, so I tryied to redo the work, and here are the detail steps to start streaming video by rtmp with nginx and nginx rtmp module.

BTW: I’m using /opt/softwares folder as my software download directory, if you are using another directory as your base directory, you should change your configuration for your script or configuration files.

Here we go:

1. Download nginx
The latest release of nginx is 1.5.13, we’ll use it as a example.

wget http://nginx.org/download/nginx-1.5.13.tar.gz

2. Extract the downloaded nginx source code

tar -xvf nginx-1.5.13.tar.gz

3. Download nginx-rtmp-module source code
This is only a test, so we’ll use the latest source to run the test. In formal version, we should choose a stable release instead.

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

4. configure nginx with rtmp suppported, and compile it.

cd nginx-1.5.13
./configure --add-module=/opt/softwares/nginx-rtmp-module --add-module=/opt/softwares/nginx-rtmp-module/hls --without-http_rewrite_module
make
make install

If you don’t need HLS support, you can remove the option string: –add-module=/opt/softwares/nginx-rtmp-module/hls

5. nginx and rtmp module configurations

use /home/jacky/rtmp as the root directory of nginx.

cp -r /opt/softwares/nginx-rtmp-module/test/* /home/jacky/rtmp
cp /opt/softwares/nginx-rtmp-module/stat.xsl /home/jacky/rtmp

6.re-config the nginx.conf

Open the nginx.conf file and modify the path setting to the right path of your environment, you need to modify at least the following 3 settings:

        location /stat.xsl {
            root /path/to/nginx-rtmp-module/;
        }

        location /rtmp-publisher {
            root /path/to/nginx-rtmp-module/test;
        }

        location / {
            root /path/to/nginx-rtmp-module/test/www;
        }

An example of my configuration. Here is the working directory for me:

	location /stat.xsl {
            root /home/jacky/rtmp/;
        }

        location /rtmp-publisher {
            root /home/jacky/rtmp;
        }

        location / {
            root /home/jacky/rtmp/www;
        }

7. startup the nginx

/usr/local/nginx/sbin/nginx -c /home/jacky/rtmp/nginx.conf

8. publish stream to nginx
The easiest way to publish live video stream is using ffmpeg.
So as an example, we use ffmpeg to feed the rtmp stream to nginx:

ffmpeg -re -i ~/Videos/ted.mp4 -f flv rtmp://live.rg4.net/myapp/mystream

If you dont have a proper video file, you can also publish your webcam to nginx by following command:

ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an -f flv rtmp://localhost/myapp/mystream

Something you may need to know:
RTMP supports only a limited number of codecs. The most popular RTMP codecs pair is H264 + AAC, traditional FLV format, Sorenson-H263 (aka flv) + MP3 or Nellymoser, is also supported.
So if you are publishing streams by yourself, you must be confirmed that your audio/video codec is the ones RTMP supports. Otherwise you need to do the transcode yourself before feed to nginx.

9. Play the video
Open a browser, and navigate to http://live.rg4.net

Getting started with nginx rtmp

10. Get your nginx ready for HLS

If you want your nginx ready for HLS support, you should first make sure you’ve compiled the nginx with HLS module, and modify the default nginx.conf to have HLS enabled, then push your streams to the server with URL like:

rtmp://your-server-ip-address/hls/<stream_name>

Once done with these, you can have both RTMP and HLS supported for your single push of stream. nginx will do the rest job of conversion and segmenting of RTMP to HLS.

But as you may know, HLS supports less formats than RTMP, if you want archive that, support RTMP and HLS both with a single push, you need specify the audio/video encoding type to AAC/H.264 for your stream.

When everything is ready, you can watch the video by:

  • RTMP: rtmp://your-server-ip-address/hls/<stream_name>
  • HLS: http://your-server-ip-address/hls/<stream_name>.m3u8

Here is a sample configuration which worked for me:

#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 /stat {
		      	rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {            
             root tmp;
             index stat.xsl;
        }

        # rtmp control
        location /control {
            rtmp_control all;
        }
	
    	  location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
            }
            alias ./tmp/hls;
            expires -1;
        }
        
        location / {
            root ./test/www;
            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;
        }
	}
}

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;

        application hls {
            live on;
            # sample HLS
            hls on;
            hls_path ./tmp/hls;
            hls_sync 100ms;
        }
        
        application live {
        		live on;
        }
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “A simple guide to start rtmp streaming with nginx

  • jacky

    There is one more step to follow before you start the nginx, that is modify the nginx.conf and change all the “/path/to” content to the real path for your system.