vi

RTMP Server Tự Host Trên VPS Offshore

RTMP (Real-Time Messaging Protocol) là giao thức tiêu chuẩn cho livestream từ encoder (OBS, vMix) tới streaming server. Tự dựng RTMP server trên VPS offshore Iceland của AnubizHost cho phép bạn livestream trực tiếp tới audience, tạo backup stream, restream tới nhiều platform cùng lúc, hoặc xây dựng platform streaming riêng không phụ thuộc YouTube/Twitch. Phù hợp cho content creator quan tâm bảo mật, doanh nghiệp livestream sự kiện nội bộ, hoặc bất kỳ ai cần kiểm soát hoàn toàn hạ tầng streaming.

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

Khi Nào Cần Tự Host RTMP Server

Có bốn use case chính cho RTMP server tự host. Thứ nhất, restream/multistream - bạn livestream một lần lên RTMP server riêng, server đó push stream tới YouTube, Twitch, Facebook, X cùng lúc. Tiết kiệm bandwidth upload từ máy bạn, đặc biệt quan trọng khi internet upload yếu.

Thứ hai, livestream riêng tư - sự kiện công ty, training nội bộ, hội thảo không công khai. Push lên RTMP server riêng và share link HLS chỉ cho người được mời. Không bị bóp băng thông hay quảng cáo như platform công cộng.

Thứ ba, xây dựng platform streaming riêng - như Twitch nhưng cho cộng đồng cụ thể (gaming clan, esports, edu, religious). RTMP server kết hợp với một frontend web (HTML5 player playing HLS) tạo nên platform livestream hoàn chỉnh.

Thứ tư, backup streaming - lo lắng YouTube hay Twitch ban đột ngột mất stream giữa chừng. Push song song lên RTMP server riêng làm backup, có thể switch over instantly.

Yêu cầu hệ thống: bandwidth là yếu tố quan trọng nhất. Stream 1080p60 ~6Mbps, 4K60 ~25Mbps. Khi restream tới 4 platform, upload bandwidth từ VPS = 4× input bitrate. Trên VPS AnubizHost không giới hạn bandwidth, có thể restream comfortable. CPU cần đủ để transcode nếu muốn nhiều resolution (1080p, 720p, 480p) - VPS Pro hoặc Premium phù hợp.

Cài Đặt Nginx-RTMP Module

Nginx với module rtmp là giải pháp RTMP server phổ biến và đáng tin cậy nhất. Trên Ubuntu 22.04:

apt update && apt install -y nginx libnginx-mod-rtmp ffmpeg

Sửa /etc/nginx/nginx.conf thêm block rtmp:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;

            # HLS output
            hls on;
            hls_path /var/www/hls;
            hls_fragment 3;
            hls_playlist_length 60;

            # Push tới các platform khác (restream)
            push rtmp://a.rtmp.youtube.com/live2/STREAM_KEY_YT;
            push rtmp://live.twitch.tv/app/STREAM_KEY_TWITCH;
        }
    }
}

Tạo thư mục HLS:

mkdir -p /var/www/hls
chown www-data:www-data /var/www/hls

Cấu hình HTTP block để serve HLS qua HTTPS:

server {
    listen 443 ssl http2;
    server_name stream.tenmien.com;

    ssl_certificate /etc/letsencrypt/live/stream.tenmien.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/stream.tenmien.com/privkey.pem;

    location /hls {
        types {
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }
        root /var/www;
        add_header Cache-Control no-cache;
        add_header Access-Control-Allow-Origin *;
    }
}

Reload Nginx. Mở firewall: 1935/tcp (RTMP), 443/tcp (HTTPS).

Trong OBS Studio: Settings > Stream > Custom, URL rtmp://stream.tenmien.com/live, Stream Key tùy chọn (ví dụ mystream). Click Start Streaming. Sau vài giây, HLS playlist xuất hiện tại https://stream.tenmien.com/hls/mystream.m3u8 - phát được trên VLC, web player, mobile.

Bảo Mật, Adaptive Bitrate và Phát Hiện Stream

Mặc định Nginx-RTMP cho phép ai cũng push stream tới server. Cần thêm authentication:

on_publish authentication: Mỗi khi có stream được push, Nginx gọi HTTP endpoint xác minh. Trong rtmp block:

application live {
    on_publish http://localhost:8080/auth;
}

Endpoint backend (Node.js/Python) nhận stream key, kiểm tra database, return 200 OK nếu hợp lệ hoặc 403 nếu không. Cách này tách biệt authentication logic khỏi Nginx config, dễ scale.

HLS authentication: Đặt HLS trong thư mục có protect bằng nginx_auth hoặc generate signed URL (HMAC) cho từng viewer.

Adaptive Bitrate (ABR): Để stream xem được trên cả 4G và fiber, cần multiple bitrate. Dùng FFmpeg transcode:

application live {
    live on;
    exec ffmpeg -i rtmp://localhost/live/$name
        -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -s 1280x720 -c:a aac -b:a 128k -f flv rtmp://localhost/hls/720p_$name
        -c:v libx264 -preset veryfast -b:v 1500k -maxrate 1500k -s 854x480 -c:a aac -b:a 96k -f flv rtmp://localhost/hls/480p_$name;
}

application hls {
    live on;
    hls on;
    hls_path /var/www/hls;
    hls_nested on;
    hls_variant _720p BANDWIDTH=3128000;
    hls_variant _480p BANDWIDTH=1596000;
}

Player tự động chọn variant phù hợp dựa trên bandwidth viewer. Transcode tiêu thụ nhiều CPU - VPS Pro hoặc Premium AnubizHost với 4-8 core là phù hợp.

Recording: Bật record all trong application để lưu mỗi stream thành file FLV. Convert sang MP4 sau khi stream kết thúc bằng on_publish_done hook.

DVR (rewind live stream): Đặt hls_playlist_length cao (600s = 10 phút) để viewer có thể rewind. Hoặc lưu full recording và phát lại như VOD sau livestream.

Stream key rotation: Xoay stream key định kỳ qua API. Khi stream key cũ hết hạn, viewer không xem được - đảm bảo content chỉ accessible trong thời gian quy định.

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

Ready to get started?

Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.

Anubiz Chat AI

Online