Nginx based streaming server on Raspberry Pi

The Raspberry Pi is a credit-card sized computer that plugs into your TV and a keyboard. Several web server softwares are available for Linux-based operating systems including Raspibian, a free operating system based on Debian optimized for the Raspberry Pi hardware. Its very easy to use any of these softwares to turn the Pi to a 24/7 portable server. But due to the hardware limitations, its always a good idea to opt for a lightweight solution when possible and available. Apache is a very popular software, but nginx(pronounced engine-x) is a lightweight alternative. In addition to this, using the RTMP module, we can easily turn the pi to a streaming server.

Unlike Apache, all modules, including the 3rd party modules, should be compiled into nginx. We will download the source for nginx, rtmp module and then compile and install it.

Lets install nginx from apt-get first and then remove it. This way, we will get the startup scripts and dependencies in place.

sudo bash
apt-get update
apt-get -y install nginx   
apt-get -y remove nginx   
apt-get clean

Go to src folder and create an nginx folder, where we will compile the source.

cd /usr/src/
mkdir nginx
cd nginx
#clone the latest version of rtmp module for nginx  
git clone git://  
#dowload nginx source  
tar xzf nginx-1.7.4.tar.gz 
cd nginx-1.7.4

Now, install some dependencies and configure the installer.

apt-get install -y curl build-essential libpcre3-dev libpcre++-dev zlib1g-dev libcurl4-openssl-dev libssl-dev  
./configure --prefix=/var/www \  
            --sbin-path=/usr/sbin/nginx \  
            --conf-path=/etc/nginx/nginx.conf \  
            --pid-path=/var/run/ \  
            --error-log-path=/var/log/nginx/error.log \  
            --http-log-path=/var/log/nginx/access.log \  
            --with-http_ssl_module \  
            --without-http_proxy_module \  

Create the www folder

mkdir -p /var/www

Install nginx

make install

Check the version

nginx -v

Start the service

service nginx start

Check http://localhost/ and you should get the Welcome to nginx! page.

Now, edit nginx.conf to set the RTMP configuration. My config file looks as below.

user www-data;
worker_processes 1; 
pid /var/run/;

events {
	worker_connections 768;

http {

	# Basic Settings

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	# Logging Settings

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	# Gzip Settings

	gzip on;
	gzip_disable "msie6";

	# Virtual Host Configs

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
rtmp {
   server {
     listen 12345;
     chunk_size 8192;
     ping 30s;
     notify_method get;
     allow play all;

     # You should send x.264/aac RTMP Stream via ffmpeg to this application
     application hls {
       allow play all;
       live on;
       hls on;
       hls_path /tmp/hls;

The highlighted portion at the end of the file defines the RTMP settings. It essentially add a new application named hls.

Open the virtual server file /etc/nginx/sites-enabled/default for the rest of the configuration. My file looks as below:

# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.

server {
	root /usr/share/nginx/www;
	index index.html index.htm index.php;

	# Make site accessible from http://localhost/
	server_name localhost;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ /index.html;

	location /doc/ {
		alias /usr/share/doc/;
		autoindex on;
		deny all;

     # rtmp stat
     location /stat {
       rtmp_stat all;
       rtmp_stat_stylesheet stat.xsl;

     location /stat.xsl {
       # you can move stat.xsl to a different location
       root /etc/nginx;

     # rtmp control
     location /control {
       rtmp_control all;
     error_page  500 502 503 504 /50x.html;
     location = /50x.html {
       root  html;
     # Client (VLC etc.) can access HLS here.
     location /hls {
       # Serve HLS fragments
       types {
         application/ m3u8;
         video/mp2t ts;
       root /tmp;
       add_header Cache-Control no-cache;

The highlighted portion towards the end shows the configuration to add an RTMP statistics page and types served by hls application.

Check http://localhost/stat to see the RTMP statistics page.

You can publish your stream to rtmp://localhost:12345/hls/mystream and clients like VLC player can consume it using the url http://localhost/hls/mystream.m3u8

You can use ffmpeg to publish/transcode streams to the RTMP url. That will be explained in the next post.

One thought on “Nginx based streaming server on Raspberry Pi

  1. fwiw:
    Had trouble with getting version 1.10.2 running on rpi2B+ due to “nginx: [emerg] the INET6 sockets are not supported on this platform in “[::]:80” of the “listen” directive” error.

    Adding the –with-ipv6 switch in ./configure worked for me.

Leave a Reply

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