Set Up Nginx RTMP Streaming Server on VPS

If you want to broadcast live video without relying on platforms like YouTube or Twitch, Nginx RTMP (Real-Time Messaging Protocol Module) provides a powerful, lightweight alternative. With Nginx RTMP, you can stream live video with remarkably low latency — typically just 1-3 seconds from capture to viewer — directly from your own VPS. Whether you're broadcasting gaming sessions, live events, conferences, or creating educational content, Nginx RTMP keeps all your data on your server while maintaining complete control over your content and audience.

Understanding RTMP and Why Nginx is Perfect for Streaming

RTMP (Real-Time Messaging Protocol) is a specialized protocol engineered for live audio and video transmission over the internet. Unlike HTTP-based streaming solutions that introduce significant latency (10-60 seconds), RTMP maintains a persistent connection that keeps the audio and video stream flowing continuously in real-time. Here are the key advantages that make RTMP exceptional for live streaming:

Nginx, known for its speed and efficiency, is the perfect foundation for adding RTMP functionality. Rather than deploying a dedicated streaming server, you compile Nginx with the RTMP module, turning your existing web server into a powerful streaming platform that consumes minimal CPU and memory.

Prerequisites and System Requirements

Before starting, ensure your VPS meets these minimum requirements:

AsiaGB's VPS offerings in Thailand and Singapore are specifically configured for streaming workloads, with SSD storage, 99% uptime guarantees, and generous bandwidth allocations at competitive prices starting from just 500 THB per month.

Step 1: Update System and Install Build Dependencies

Start by updating your system package manager and installing the essential development tools required for compiling Nginx with RTMP support:

sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev \
  libgd-dev libgeoip-dev git curl wget

These packages include GCC compiler, Make build system, and various libraries that Nginx requires during compilation. The process typically takes 5-10 minutes depending on your VPS's download speed and existing cached packages.

Step 2: Download Nginx Source and RTMP Module

Navigate to a temporary working directory and download both Nginx source code and the RTMP module from official repositories:

cd /tmp
wget http://nginx.org/download/nginx-1.24.0.tar.gz
git clone https://github.com/arut/nginx-rtmp-module.git
tar -zxf nginx-1.24.0.tar.gz
cd nginx-1.24.0

Before proceeding, verify that nginx-1.24.0 is indeed the latest stable version. Visit nginx.org to confirm you're not using outdated software. Using a fresh version ensures you benefit from the latest security patches and performance optimizations.

Step 3: Configure and Compile Nginx with RTMP Module

Run the configure script with appropriate flags to include the RTMP module and other essential features:

./configure \
  --prefix=/etc/nginx \
  --sbin-path=/usr/sbin/nginx \
  --modules-path=/usr/lib64/nginx/modules \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx.pid \
  --lock-path=/var/run/nginx.lock \
  --with-http_gzip_static_module \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_geoip_module \
  --add-module=../nginx-rtmp-module

This configuration enables critical modules: SSL/TLS for HTTPS support, gzip compression for bandwidth efficiency, HTTP/2 for faster page loads, and most importantly, the RTMP module for streaming functionality. After configuration completes successfully, compile and install:

make && sudo make install

The compilation process can take 10-20 minutes depending on CPU speed. A 2-core VPS typically completes in 15 minutes. You'll see compilation progress messages; wait for the process to complete fully before proceeding.

Step 4: Configure Nginx for RTMP Streaming

Edit the main Nginx configuration file to add RTMP application definitions:

sudo nano /etc/nginx/nginx.conf

Scroll to the end of the file (before the final closing bracket) and add the RTMP server block:

rtmp {
    server {
        listen 1935;

        application live {
            live on;
            record off;

            # Streaming restrictions — modify based on your security needs
            allow publish all;
            allow play all;
        }

        application vod {
            play /mnt/vod/videos;
        }
    }
}

This configuration creates two RTMP applications: 'live' for real-time broadcasting and 'vod' for serving archived video-on-demand content. The 'record off' setting means streams won't be automatically saved (we'll handle recording separately). To restrict streaming to specific IPs, replace 'allow publish all' with specific IP ranges.

Step 5: Prepare Video Storage Directory for VOD Recording

Create dedicated directories for storing recorded videos:

sudo mkdir -p /mnt/vod/videos
sudo chown -R www-data:www-data /mnt/vod
sudo chmod 755 /mnt/vod/videos

These directories will store FLV files captured from live streams, allowing you to maintain a complete archive of all broadcasts. The www-data user and group ensure proper file ownership for the Nginx process. You can symlink this directory to a larger mount point or separate storage if needed.

Step 6: Start Nginx and Verify RTMP Port

Test the Nginx configuration syntax, then start the service and enable it to auto-start on reboot:

sudo nginx -t    # Syntax validation — should show "test is successful"
sudo systemctl start nginx
sudo systemctl enable nginx

Verify that the RTMP server is listening on port 1935:

sudo netstat -tlnp | grep 1935
# Or alternatively:
ss -tlnp | grep 1935

You should see output showing that Nginx is listening (LISTEN state) on port 1935. If the port doesn't appear, check Nginx error logs with 'sudo tail -f /var/log/nginx/error.log' to diagnose the issue.

⚠️ Firewall Configuration: Your VPS firewall must allow traffic on port 1935. If you're using ufw (Uncomplicated Firewall), run: sudo ufw allow 1935/tcp. For cloud provider firewalls (AWS, DigitalOcean, etc.), add an inbound rule for TCP port 1935.

Step 7: Connect OBS Studio and Start Streaming

Open OBS Studio on your streaming computer and navigate to Settings → Stream. Configure these values:

Click "Start Streaming" and OBS will connect to your Nginx server. Within seconds, your video stream begins flowing to your VPS. The playback URL for viewers is: rtmp://your-vps-ip:1935/live/mystream123

Performance Tuning for High-Concurrency Streaming

For streams exceeding 500 concurrent viewers, optimize Nginx configuration:

worker_processes auto;
worker_connections 10000;
events {
    use epoll;
}
http {
    client_max_body_size 100m;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
}

And enhance RTMP server settings:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        idle_streams off;

        application live {
            live on;
            record off;
        }
    }
}

These settings increase worker processes, boost connection limits, and optimize TCP behavior for streaming workloads. The 'epoll' event engine provides superior performance on Linux systems with high concurrency.

Implementing Security: Restricting Stream Publishers

To prevent unauthorized users from broadcasting, implement webhook-based authentication. Add this to your RTMP application block:

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

Then create a PHP script at localhost:8080/auth that validates stream keys against a database. This ensures only authorized users can broadcast, while anonymous viewers remain unrestricted.

Monitoring and Maintenance

Monitor Nginx performance and logs regularly:

# Watch real-time error logs
sudo tail -f /var/log/nginx/error.log

# Monitor access (connection) logs
sudo tail -f /var/log/nginx/access.log

# Check CPU and memory usage during streaming
top

Keep Nginx updated with security patches. Check periodically for new versions and rebuild with the same configure flags. Document your compilation steps for future updates.

Ready to Launch Your Streaming Server?

AsiaGB VPS provides the ideal platform for self-hosted streaming. Thailand and Singapore locations offer optimal latency for Asian audiences. SSD storage ensures fast VOD recording, 99% uptime guarantee protects your broadcast schedule, and unlimited bandwidth per stream removes scaling concerns. Start streaming today from just 500 THB/month.

Get Started with AsiaGB VPS

View all affordable VPS Thailand plans →