CGJU
CGJU Tool Net
June 8, 2026 Β· 8 min read

Nginx-RTMP Module Introduction

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.

Core Features of Nginx-RTMP

  • RTMP Push and Pull: Supports RTMP live streaming and playback
  • HLS Support: Real-time conversion of RTMP streams to HLS format
  • DASH Support: Supports MPEG-DASH streaming format
  • Live Recording: Record live content as video files
  • Real-time Transcoding: Real-time transcoding with FFmpeg
  • Statistics Dashboard: Built-in statistics page
  • Live Control: Supports recording, stopping, dropping, and other control commands

Common Application Scenarios

  • Personal Streaming: Build your own live streaming platform
  • Corporate Training: Internal video live streaming system
  • Educational Streaming: Online classrooms, remote teaching
  • Gaming Streaming: Real-time game screen broadcasting
  • Surveillance Systems: Camera streaming forwarding

Ubuntu Compilation Installation

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.

Step 1: Install Compilation Dependencies

First, install the dependency packages required for compilation:

sudo apt update
sudo apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev -y

Step 2: Download Source Code

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

Step 3: Compile and Install

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

Step 4: Create System Service

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

Windows Installation

On Windows systems, you can use precompiled versions to avoid the hassle of compilation.

Method 1: Use Precompiled Version

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

Method 2: Use Docker

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

Configuration File Details

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.

Basic Configuration

This is the most basic RTMP configuration:

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        application live {
            live on;
            record off;
        }
    }
}
  • listen 1935: RTMP default port, firewall must be open
  • chunk_size: Data block size, affects transmission efficiency
  • application live: Define an application named live
  • live on: Enable live mode
  • record off: Disable recording function

HLS Configuration

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;
        }
    }
}
  • hls on: Enable HLS function
  • hls_path: HLS segment and playlist storage path
  • hls_fragment: Duration of each segment (seconds)
  • hls_playlist_length: Total playlist duration
  • hls_cleanup: Automatically clean up expired segments
  • hls_sync: Audio-video sync time difference

DASH Configuration

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;
}

Multi-Bitrate Transcoding Configuration

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;
}

Streaming Test

Streaming with FFmpeg

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

Streaming with OBS

OBS Studio is the most popular live streaming software. Configuration method is as follows:

  1. Open OBS, click "Settings"
  2. Select the "Stream" tab
  3. Service select "Custom..."
  4. Server fill in: rtmp://SERVER_IP/live
  5. Stream key fill in: stream1 (can be customized)
  6. Click OK, then click "Start Streaming"

Playback Test

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 Configuration Details

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.

HLS Key Parameter Optimization

  • hls_fragment: Segment duration, recommended 2-4 seconds, smaller means lower latency but more files
  • hls_playlist_length: Playlist length, recommended 5-6 times the segment duration
  • hls_sync: Audio-video sync, 100ms is usually sufficient
  • hls_start_number: Playlist starting sequence number

Low Latency HLS Configuration

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.

Recording Feature

Nginx-RTMP provides a powerful live recording function that can save live content as video files.

Recording Configuration Options

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;
}
  • record: Recording mode, off|all|audio|video
  • record_path: Recording file save path
  • record_suffix: File name suffix, supports time format
  • record_unique: Generate unique file names to avoid overwriting
  • record_max_size: Maximum size of a single recording file
  • record_max_files: Maximum number of files to keep
  • record_interval: How often to slice a new file

Recording Mode Explanation

  • off: Disable recording
  • all: Record audio and video
  • audio: Record only audio
  • video: Record only video

Statistics Dashboard

Nginx-RTMP has built-in statistics functionality, allowing you to view current server status and connection information through an HTTP page.

Configure Statistics Dashboard

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;
        }
    }
}

Access Statistics Dashboard

After configuration, access through the following address:

  • Statistics Page: http://SERVER_IP:8080/stat
  • Control Interface: http://SERVER_IP:8080/control

Information displayed on the statistics dashboard includes:

  • Current number of active streams
  • Streaming information for each stream
  • Number of playback clients for each stream
  • Upload and download bandwidth
  • Detailed information such as connection duration

Control Interface

Remote control of live streaming through the control interface:

  • record/start: Start recording
  • record/stop: Stop recording
  • drop/publisher: Disconnect streamer
  • drop/subscriber: Disconnect a viewer

Common Issues and Optimization

1. Firewall Settings

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

2. Permission Issues

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

3. Concurrency Performance Optimization

Adjust Nginx configuration to improve concurrency:

worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 65535;
    use epoll;
    multi_accept on;
}

4. Streaming Authentication

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;
}

Summary

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.

Related Recommendations

More great articles and practical tools

Want to Test Your Live Stream?

Use our free M3U8 online player to play HLS live streams directly in your browser

Try Now ➑