Configuring RTMP Streaming with Nginx: A Comprehensive Guide

Core Configuration

The RTMP server instance declaration is the foundation of your configuration.

rtmp {
    server {
        listen 1935;
    }
}

Server Listening Configuration

Define the socket where the server will accept connections.

server {
    listen 1935;
}

Application Setup

Create an application instance within the server context.

server {
    listen 1935;
    application video_stream {
    }
}

Connection Parameters

Configure connection timeouts and behavior.

timeout 60s;
ping 3m;
ping_timeout 30s;

Stream Management

Control stream handling parameters.

max_streams 32;
ack_window 5000000;
chunk_size 4096;
max_queue 1000;
max_message 1M;
buflen 5s;

Access Control

Restrict access to specific resources.

allow publish 127.0.0.1;
deny publish all;
allow play 192.168.0.0/24;
deny play all;

External Process Execution

Stream Publishing Handling

Execute external commands when streams start publishing.

application source {
    live on;
    exec_push ffmpeg -i rtmp://localhost/source/$stream_id
        -vcodec libx264 -vprofile baseline -g 10 -s 300x200
        -acodec libfaac -ar 44100 -ac 1 -f flv
        rtmp://localhost/hls/$stream_id;
}

Stream Pulling

Fetch streams from remote sources.

application remote {
    live on;
    exec_pull ffmpeg -i http://example.com/video_$stream_id.ts
        -c copy -f flv rtmp://localhost/$app/$stream_id;
}

Live Streaming Features

Stream Behavior

Configure how streams behave during transmission.

live on;
meta copy;
interleave on;
wait_key on;
wait_video on;
publish_notify on;

Stream Management

Control stream handling during connection states.

drop_idle_publisher 10s;
sync 10ms;
play_restart off;
idle_streams off;

Recording Configuration

Recording Parameters

Define recording behavior and storage.

record all;
record_path /var/rec;
record_suffix _rec.flv;
record_unique on;
record_append off;
record_lock on;

Segmented Recording

Create multiple recording segments with specific entervals.

recorder audio_rec {
    record audio;
    record_suffix .audio.flv;
}

recorder chunked {
    record all;
    record_interval 15s;
    record_path /var/rec/chunked;
}

Video On Demand

Content Playback

Specify locations for playback content.

application vod {
    play /var/video_content;
}

Remote Content Handling

Configure playback from remote locations with caching.

play_temp_path /tmp/vod_cache;
play_local_path /var/video_cache;
play /var/video_cache http://remote.server.com/video;

Stream Relay

Remote Stream Pulling

Fetch streams from remote servers.

application relay {
    live on;
    pull rtmp://remote.example.com/app/stream_name name=main_stream;
}

Remote Stream Pushing

Forward streams to remote servers.

application relay {
    live on;
    push rtmp://remote.example.com/app/stream_name;
    push_reconnect 1s;
}

Notification Callbacks

Connection Events

Configure HTTP callbacks for connection events.

on_connect http://auth.example.com/connect;
on_play http://analytics.example.com/play;
on_publish http://analytics.example.com/publish;

Recording Events

Handle recording completion events.

on_record_done http://analytics.example.com/recording_complete;

HLS Streaming Configuration

Enable and configure HTTP Live Streaming.

hls on;
hls_path /var/hls;
hls_fragment 15s;
hls_playlist_length 10m;
hls_continuous on;
hls_nested on;

MPEG-DASH Configuration

Enable and configure MPEG-DASH streaming.

dash on;
dash_path /var/dash;
dash_fragment 15s;
dash_playlist_length 10m;
dash_nested on;

Access Logging

Configure logging for connection and stream events.

access_log logs/stream_access.log;
log_format custom '$remote_addr [$time_local] $command "$app" "$name" $bytes_received $bytes_sent';

System Limits

Set system-level constraints.

max_connections 100;

Statistics and Monitoring

Enable and configure statistics reporting.

rtmp_stat all;
rtmp_stat_stylesheet stats.xsl;

Multi-Worker Streaming

Configure for multiple worker processes.

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /var/rtmp_sock;

Control Module

Enable remote control via HTTP.

location /control {
    rtmp_control all;
}

Example Configurations

Basic Video On Demand

rtmp {
    server {
        listen 1935;
        application vod {
            play /var/video_library;
        }
    }
}

Live Streaming with HLS

rtmp {
    server {
        listen 1935;
        application live {
            live on;
            hls on;
            hls_path /var/hls;
        }
    }
}

http {
    server {
        location /hls {
            alias /var/hls;
            add_header Cache-Control no-cache;
        }
    }
}

Remote Stream Relay with HLS

rtmp {
    server {
        listen 1935;
        application relay {
            live on;
            hls on;
            hls_path /var/hls/relay;
            hls_fragment 15s;
            pull rtmp://cdn.example.com/live/stream name=relay_stream;
        }
    }
}

Stream Publishing

Use FFmpeg to publish streams with various encoding options.

ffmpeg -re -i /video/input.mp4 -c:v libx264 -c:a libfaac -f flv rtmp://localhost/live/stream1
ffmpeg -re -i /video/input.mp4 -c:v libx264 -c:a libmp3lame -f flv rtmp://localhost/live/stream2

Stream Playback

Use FFplay to view streams.

ffplay rtmp://localhost/live/stream1

Tags: nginx-rtmp streaming HLS MPEG-DASH rtmp

Posted on Mon, 06 Jul 2026 17:23:22 +0000 by gotry