HLS.js Usage Guide and Best Practices
In-depth explanation of HLS.js working principles, application scenarios, advantages and disadvantages, with complete code examples and common problem solutions.
Read More βBuild your own live streaming server from scratch
Nginx-RTMP is an open-source third-party Nginx module developed by Russian developer Roman Arutyunyan. It adds RTMP (Real-Time Messaging Protocol) support to Nginx, turning it into a powerful streaming media server.
The RTMP protocol was originally developed by Macromedia (later acquired by Adobe) for audio and video transmission between Flash players and servers. Although Flash has exited the historical stage, the RTMP protocol remains the de facto standard for live streaming due to its low latency, maturity, and stability.
Installing Nginx-RTMP on Ubuntu requires compiling from source, as the RTMP module is not an official Nginx module. Below are the detailed compilation and installation steps.
First, install the dependency packages required for compilation:
sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev -y
Download the Nginx and Nginx-RTMP module source code:
cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.24.0.tar.gz
sudo wget https://github.com/arut/nginx-rtmp-module/archive/refs/heads/master.zip -O nginx-rtmp-module.zip
sudo tar zxvf nginx-1.24.0.tar.gz
sudo unzip nginx-rtmp-module.zip
Configure compilation parameters and add the RTMP module:
cd nginx-1.24.0
sudo ./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--add-module=../nginx-rtmp-module-master
sudo make
sudo make install
Create a systemd service file for easier Nginx management:
sudo tee /etc/systemd/system/nginx.service << EOF
[Unit]
Description=NGINX with RTMP module
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
On Windows systems, you can use precompiled versions to avoid the hassle of compilation.
You can download Windows versions compiled by kind people from GitHub, or use the following method:
# 1. Download Nginx for Windows
# from http://nginx.org/en/download.html
# 2. Download nginx-rtmp-module
# from https://github.com/arut/nginx-rtmp-module
# Note: Windows version requires specific compilation, it's recommended to search for ready-made nginx-rtmp-win32 versions
Using Docker on Windows is a simpler approach:
docker run -d \
--name nginx-rtmp \
-p 1935:1935 \
-p 8080:8080 \
-v /path/to/nginx.conf:/etc/nginx/nginx.conf \
tiangolo/nginx-rtmp
The Nginx-RTMP configuration file is nginx.conf, usually located in the /usr/local/nginx/conf/ directory. Below is a detailed introduction to various configurations.
This is the most basic RTMP configuration:
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
}
}
}
Configure HLS output to allow live streams to be played in web pages:
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
hls on;
hls_path /tmp/hls;
hls_fragment 3;
hls_playlist_length 15;
hls_cleanup on;
hls_sync 100ms;
}
}
}
http {
server {
listen 8080;
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
}
}
Configure MPEG-DASH support:
application live {
live on;
record off;
dash on;
dash_path /tmp/dash;
dash_fragment 2;
dash_playlist_length 10;
dash_format mp4;
}
Real-time multi-bitrate transcoding with FFmpeg:
application live {
live on;
record off;
exec ffmpeg -i rtmp://localhost/live/$name
-c:v libx264 -crf 23 -preset veryfast -maxrate 2000k -bufsize 4000k -s 1280x720 -c:a aac -b:a 128k -f flv rtmp://localhost/live720/$name
-c:v libx264 -crf 23 -preset veryfast -maxrate 1000k -bufsize 2000k -s 854x480 -c:a aac -b:a 96k -f flv rtmp://localhost/live480/$name
-c:v libx264 -crf 23 -preset veryfast -maxrate 500k -bufsize 1000k -s 640x360 -c:a aac -b:a 64k -f flv rtmp://localhost/live360/$name;
}
application live720 {
live on;
record off;
hls on;
hls_path /tmp/hls/720p;
}
application live480 {
live on;
record off;
hls on;
hls_path /tmp/hls/480p;
}
application live360 {
live on;
record off;
hls on;
hls_path /tmp/hls/360p;
}
The simplest way to stream is using FFmpeg:
# Stream local file
ffmpeg -re -i test.mp4 -c copy -f flv rtmp://SERVER_IP/live/stream1
# Stream screen
ffmpeg -f gdigrab -framerate 30 -i desktop -c:v libx264 -preset fast -pix_fmt yuv420p -f flv rtmp://SERVER_IP/live/stream1
OBS Studio is the most popular live streaming software. Configuration method is as follows:
rtmp://SERVER_IP/livestream1 (can be customized)Use ffplay or VLC to play RTMP streams:
# FFplay playback
ffplay rtmp://SERVER_IP/live/stream1
# VLC playback
vlc rtmp://SERVER_IP/live/stream1
If HLS is configured, you can also play in the browser:
http://SERVER_IP:8080/hls/stream1.m3u8
HLS (HTTP Live Streaming) is a streaming protocol developed by Apple. Due to its HTTP-based nature and good compatibility, it is widely used for web live streaming and mobile playback.
If you pursue lower latency, you can configure it like this:
hls on;
hls_path /tmp/hls;
hls_fragment 1;
hls_playlist_length 3;
hls_sync 50ms;
hls_cleanup on;
hls_delete_threshold 3;
This configuration can reduce latency to around 3-5 seconds, but will increase server load and network overhead.
Nginx-RTMP provides a powerful live recording function that can save live content as video files.
application live {
live on;
record all;
record_path /tmp/records;
record_suffix -%Y%m%d-%H%M%S.flv;
record_unique on;
record_max_size 1000M;
record_max_files 5;
record_interval 30m;
# Script executed after recording is complete
exec_record_done ffmpeg -i $path -c copy /path/to/$basename.mp4;
}
Nginx-RTMP has built-in statistics functionality, allowing you to view current server status and connection information through an HTTP page.
http {
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/src/nginx-rtmp-module-master/;
}
location /control {
rtmp_control all;
}
}
}
After configuration, access through the following address:
Information displayed on the statistics dashboard includes:
Remote control of live streaming through the control interface:
Ensure the firewall has opened the necessary ports:
# UFW firewall
sudo ufw allow 1935/tcp
sudo ufw allow 8080/tcp
# iptables
sudo iptables -A INPUT -p tcp --dport 1935 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
Ensure Nginx has write permission to the HLS segment directory:
sudo mkdir -p /tmp/hls /tmp/records
sudo chown -R www-data:www-data /tmp/hls /tmp/records
Adjust Nginx configuration to improve concurrency:
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
multi_accept on;
}
To prevent others from streaming freely, add authentication:
application live {
live on;
record off;
# Simple URL authentication
on_publish http://localhost/auth;
}
# HTTP authentication service (example)
location /auth {
if ($arg_key = "your-secret-key") {
return 200;
}
return 403;
}
Nginx-RTMP is a powerful open-source streaming server module. With it, you can easily build your own live streaming system. This article comprehensively introduces the usage of Nginx-RTMP from module introduction, installation and deployment, configuration details to feature usage.
Whether for personal streaming, corporate training, or online education, Nginx-RTMP can provide stable and reliable streaming services. Combined with FFmpeg, more advanced features can be achieved, such as real-time transcoding, multi-bitrate output, etc.
After setting up your streaming server, you can use our M3U8 Online Player to test the playback effect of HLS live streams, simply enter the HLS address in the browser to play.
More great articles and practical tools
In-depth explanation of HLS.js working principles, application scenarios, advantages and disadvantages, with complete code examples and common problem solutions.
Read More βBeginner's user guide, teaching you how to watch FLV and RTMP live streams through a web player, including live stream principles.
Read More βIn-depth analysis of browser video playback technology stack, comprehensive understanding of the technological evolution of web video playback.
Read More βUse our free M3U8 online player to play HLS live streams directly in your browser
Try Now β‘