On this page
- How does the RTMP to HLS pipeline work?
- How do you install Nginx with the RTMP module?
- How do you configure the RTMP application?
- How do you stop strangers from publishing?
- How do you serve the HLS stream over HTTPS?
- Which firewall rules does streaming need?
- How do you set up OBS for this server?
- How do you test playback and watch the stream?
- What else can the RTMP module do?
- Questions
How does the RTMP to HLS pipeline work?
OBS on your computer encodes video and pushes it over RTMP to the server. The Nginx RTMP module receives that stream and, without re-encoding, cuts it into short MPEG-TS segments plus an .m3u8 playlist. Regular Nginx serves those files over HTTPS, and browsers play them with native HLS support or a small JavaScript player.
Because nothing is transcoded on the server, CPU use stays low even on a small plan. The trade-off is that every viewer receives the single quality you chose in OBS. Producing several renditions (1080p, 720p, 480p) would mean running FFmpeg encoders on shared vCPUs with no GPU, which a VPS handles poorly beyond one extra rendition. Outgoing transfer is the real budget: a 3 Mbit/s stream costs about 1.35 GB per viewer per hour. Stream only content you have the rights to broadcast.
How do you install Nginx with the RTMP module?
Ubuntu and Debian package the module separately, and the package enables it automatically.
sudo apt update
sudo apt install -y nginx libnginx-mod-rtmp
nginx -v
dpkg -l libnginx-mod-rtmp | tail -n 1
ls /etc/nginx/modules-enabled/ | grep rtmpThe last command should list a file such as 50-mod-rtmp.conf, which loads the module before the rest of the configuration. Nginx basics are covered in the Nginx guide.
How do you configure the RTMP application?
The rtmp block lives at the top level of nginx.conf, beside the http block, not inside it. Keep it in its own file and include it.
sudo mkdir -p /var/www/hls
sudo chown www-data:www-data /var/www/hls
sudo tee /etc/nginx/rtmp.conf >/dev/null <<'EOF'
rtmp {
server {
listen 1935;
chunk_size 4096;
application live {
live on;
record off;
notify_method get;
on_publish http://127.0.0.1:8080/rtmp-auth;
deny play all;
hls on;
hls_path /var/www/hls;
hls_fragment 2s;
hls_playlist_length 20s;
hls_cleanup on;
}
}
}
EOF
echo 'include /etc/nginx/rtmp.conf;' | sudo tee -a /etc/nginx/nginx.confdeny play all stops anyone from pulling the raw RTMP feed, so viewers use HLS only. Short fragments lower latency to roughly 6 to 10 seconds; longer ones are more forgiving on weak connections.
How do you stop strangers from publishing?
Without a check, anyone who guesses the URL can broadcast on your server. The on_publish line above sends each new stream to a local HTTP endpoint and only accepts it on a 2xx answer. OBS passes anything after a question mark in the stream key as query arguments, so a key of stream?pwd=SECRET reaches the endpoint as $arg_pwd.
KEY=$(openssl rand -hex 16); echo "$KEY"
sudo tee /etc/nginx/conf.d/rtmp-auth.conf >/dev/null <<EOF
server {
listen 127.0.0.1:8080;
location = /rtmp-auth {
if (\$arg_pwd = "$KEY") { return 200; }
return 403;
}
location = /stat {
rtmp_stat all;
}
}
EOF
sudo nginx -t && sudo systemctl reload nginx
sudo ss -tlnp | grep -E ':1935|:8080'The shell fills in your random key, while the escaped \$arg_pwd stays literal for Nginx. Note the printed key; you will paste it into OBS. Port 8080 binds to localhost only, so the auth and statistics endpoints are not public.
How do you serve the HLS stream over HTTPS?
Create a DNS record for live.example.net (see DNS records) and a site that serves the segment folder with the right MIME types and no caching of the playlist.
sudo mkdir -p /var/www/live-site
sudo tee /etc/nginx/sites-available/live >/dev/null <<'EOF'
server {
listen 80;
listen [::]:80;
server_name live.example.net;
root /var/www/live-site;
location /hls/ {
alias /var/www/hls/;
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
}
EOF
sudo ln -s /etc/nginx/sites-available/live /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d live.example.netCertbot rewrites the site for port 443 and sets up renewals; see the Certbot guide for details.
Which firewall rules does streaming need?
Open RTMP for the encoder and the web ports for viewers.
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw allow 1935/tcp
sudo ufw enableIf you always stream from the same connection, narrow the RTMP rule to it: sudo ufw allow from 203.0.113.50 to any port 1935 proto tcp. RTMP is unencrypted, so the publish key travels in clear text; a source IP rule adds a second layer.
How do you set up OBS for this server?
In OBS, open Settings, Stream and choose Custom as the service.
- Server:
rtmp://live.example.net/live - Stream key:
stream?pwd=followed by the key you generated
Under Output, switch to Advanced mode and set the encoder to x264 or your local hardware encoder, rate control CBR, a bitrate between 2,500 and 4,500 kbit/s for 720p or 1080p, and a keyframe interval of 2 seconds so it lines up with hls_fragment. Audio at AAC 128 or 160 kbit/s is plenty. Press Start Streaming; the encoder status in the bottom bar turns green when Nginx accepts the stream.
How do you test playback and watch the stream?
Confirm that segments appear, then open the playlist in a player.
ls -l /var/www/hls/
curl -s https://live.example.net/hls/stream.m3u8
curl -s http://127.0.0.1:8080/stat | grep -E '<name>|<nclients>'Safari and most mobile browsers play the .m3u8 address directly, and desktop media players such as VLC open it as a network stream. For an embedded player in Chrome or Firefox, a page in /var/www/live-site can load the open-source hls.js library and attach it to a video element. With no OBS at hand, you can publish a short test pattern from the server with FFmpeg:
ffmpeg -re -f lavfi -i testsrc2=size=1280x720:rate=30 -f lavfi -i sine=frequency=440 \
-t 60 -c:v libx264 -preset veryfast -b:v 2500k -g 60 -c:a aac -b:a 128k \
-f flv "rtmp://127.0.0.1/live/stream?pwd=$KEY"What else can the RTMP module do?
A few directives extend the setup without extra software:
- Recording:
record all; record_path /var/rec; record_unique on;saves each broadcast as FLV. Keep an eye on disk space with disk usage checks. - Restreaming: a
push rtmp://destination/app/key;line forwards the incoming stream to another RTMP service you are allowed to stream to, so OBS uploads once. - Logs: failed publish attempts show in
/var/log/nginx/error.log, which is the first place to look if OBS cannot connect.
Updates arrive with sudo apt upgrade; reload Nginx after them and run nginx -t to confirm the module still loads.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Live streaming with Nginx RTMP and OBS
Why does OBS say it failed to connect?
Check that 1935 is open in the firewall, the server URL ends in /live, and the key includes ?pwd= with the exact secret. A 403 from the auth endpoint appears in the Nginx error log.
How much delay should viewers expect?
With 2 second fragments and a 20 second playlist, around 6 to 10 seconds behind the encoder. Shorter fragments reduce it a little but make playback less tolerant of slow connections.
Can the server offer several quality levels?
Only by transcoding with FFmpeg through the exec directive, which runs on CPU here because there is no GPU. One extra low rendition may fit on a larger plan; for more, encode multiple outputs locally instead.
Does this work on Rocky Linux or AlmaLinux?
The RTMP module is not in their base repositories, so you would build Nginx with the module from source. This guide sticks to Ubuntu and Debian, where it is a single package.
How many viewers can a plan serve?
CPU is not the limit for HLS files. Divide the monthly transfer of the plan by your stream bitrate times viewer hours; at 3 Mbit/s each viewer hour is about 1.35 GB.
Can I make the stream private?
Yes. Add auth_basic to the /hls/ location, or limit it with allow and deny rules for known addresses.