Build a Live Streaming Server with Nginx, FFmpeg, and OpenResty
This guide walks through selecting streaming protocols, preparing a high‑performance server, installing OpenResty, compiling the nginx‑ts module, configuring Nginx for RTMP/HLS/DASH, pushing streams with FFmpeg or OBS, and applying security and stability best practices.
Introduction
Live streaming can use the low‑latency RTMP protocol or the widely compatible HTTP‑based HLS protocol; the choice depends on real‑time requirements and device compatibility.
Server Environment Setup
Select a performant cloud server and install required tools such as Nginx, FFmpeg, and OpenResty.
OpenResty and Module Installation
Download OpenResty:
wget https://openresty.org/download/openresty-1.21.4.3.tar.gzClone the nginx‑ts module:
git clone https://github.com/arut/nginx-ts-module.gitInstall FFmpeg (refer to external tutorial).
Dynamic Compilation and Configuration
apt-get install libreadline-dev libncurses5-dev libpcre3-dev \
libssl-dev perl make build-essential ./configure --prefix=/opt/openresty --with-luajit --without-http_redis2_module \
--with-http_iconv_module --add-dynamic-module=/root/nginx-ts-module
make -j4
sudo make installKey configuration file ( nginx.conf) loads the TS module and defines HTTP and RTMP locations, e.g.:
# vim /opt/openresty/nginx/conf/nginx.conf
error_log logs/error.log;
pid logs/nginx.pid;
load_module "/opt/openresty/nginx/modules/ngx_http_ts_module.so";
http {
server {
listen 8777;
location /publish/ {
ts;
ts_hls path=/var/media/hls segment=10s;
ts_dash path=/var/media/dash segment=10s;
client_max_body_size 0;
}
location /play/ {
add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*';
types { application/x-mpegURL m3u8; application/dash+xml mpd; video/MP2T ts; video/mp4 mp4; }
alias /var/media/;
}
}
}Create media directories:
cd /var && mkdir media
cd media && mkdir hls dashFFmpeg Stream Push
Push a stream from a source to the server:
ffmpeg -re -i rtmp://liteavapp.qcloud.com/live/liteavdemoplayerstreamid \
-bsf:v h264_mp4toannexb -c copy -f mpegts http://127.0.0.1:8777/publish/sintelIf port 8777 is occupied, use an alternative port.
Public RTMP Test Addresses
rtmp://liteavapp.qcloud.com/live/liteavdemoplayerstreamid (smooth)
rtmp://ns8.indexforce.com/home/mystream (some lag)
rtmp://mobliestream.c3tv.com:554/live/goodtv.sdp (slow)Client Playback Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Live Streaming Demo</title>
</head>
<body>
<video data-dashjs-player autoplay src="http://127.0.0.1:8777/play/dash/sintel/index.mpd" controls></video>
</body>
</html>Playback Screenshots
OBS Streaming
When FFmpeg is not used directly, OBS Studio can push the stream to http://127.0.0.1:8777/publish/sintel .
OBS Studio is a free, open‑source Windows application that captures video, audio, and graphics, encodes with H.264/X264 and AAC, and supports GPU‑accelerated game capture.
OBS Solution Details
Download nginx‑rtmp‑module:
git clone https://github.com/arut/nginx-rtmp-module.gitCompile and load the module similarly to the TS module, adding
load_module "/opt/openresty/nginx/modules/ngx_rtmp_module.so";to nginx.conf.
Streaming flow: OBS → nginx‑rtmp → nginx‑ts.
This allows Windows users to stream without invoking FFmpeg directly.
Security and Stability Optimizations
Protect the live system by configuring access controls, enabling SSL encryption, and setting up load balancing.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
