How to Build an HLS Streaming Server with Nginx

Prerequisites

Before building your HLS streaming server, ensure you have the following:

  • A server running Ubuntu 20.04+ or Debian 11+ with root access
  • A domain name pointing to your server (recommended for production)
  • Basic familiarity with the Linux command line
  • At least 2GB RAM and 20GB free disk space

HLS (HTTP Live Streaming) enables you to serve video content over standard HTTP, with adaptive bitrate support for smooth playback across devices and network conditions.

Install Nginx with RTMP Module

The RTMP module allows Nginx to receive live video streams via the RTMP protocol and convert them to HLS on the fly. You can either compile from source or use a prebuilt package.

Option 1: Install via package (Ubuntu/Debian)

sudo apt update

                        sudo apt install nginx libnginx-mod-rtmp -y

Option 2: Compile from source

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

                        git clone https://github.com/arut/nginx-rtmp-module.git

                        wget http://nginx.org/download/nginx-1.24.0.tar.gz

                        tar -xzf nginx-1.24.0.tar.gz

                        cd nginx-1.24.0

                        ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module

                        make && sudo make install

After installation, verify Nginx is running with systemctl status nginx.

Configure Nginx for HLS

Edit your /etc/nginx/nginx.conf file and add the RTMP and HLS configuration blocks. This setup enables both live streaming and VOD capabilities with proper CORS headers for cross-origin access.

rtmp {

                            server {

                                listen 1935;

                                chunk_size 4096;

                                application live {

                                    live on;

                                    record off;

                                    hls on;

                                    hls_path /var/www/html/hls;

                                    hls_fragment 3;

                                    hls_playlist_length 60;

                                }

                            }

                        }

                        http {

                            server {

                                listen 80;

                                server_name your-domain.com;

                                location /hls {

                                    types {

                                        application/vnd.apple.mpegurl m3u8;

                                        video/mp2t ts;

                                    }

                                    root /var/www/html;

                                    add_header Cache-Control no-cache;

                                    add_header 'Access-Control-Allow-Origin' '*';

                                    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

                                }

                            }

                        }

Restart Nginx after making changes: sudo systemctl restart nginx. The CORS headers are critical for web players like ours to access your HLS streams.

Stream with FFmpeg

FFmpeg is the industry-standard tool for encoding and streaming video. Use the following commands to push live streams or convert existing videos to HLS.

Push a live RTMP stream to your server:

ffmpeg -re -i input.mp4 \

                          -c:v libx264 -preset veryfast -b:v 3000k \

                          -c:a aac -b:a 128k -f flv \

                          rtmp://your-server-ip/live/stream

Convert a VOD file to HLS segments directly:

ffmpeg -i video.mp4 \

                          -c:v libx264 -c:a aac \

                          -hls_time 6 -hls_list_size 0 \

                          -hls_segment_filename 'segment_%03d.ts' \

                          output.m3u8

For adaptive bitrate streaming, encode multiple quality levels and create a master playlist referencing each variant playlist.

Test Your Stream

Once your server is configured and streaming, test it using our free M3U8 Player:

  1. Go to M3U8 Player
  2. Enter your HLS URL (e.g., https://your-domain.com/hls/stream.m3u8)
  3. Click the Play button or press Enter
  4. The player will load your stream with adaptive bitrate support

You can also test with VLC, ffplay, or any HLS-compatible player. If the stream fails, check your firewall (ports 80 and 1935 must be open), Nginx error logs, and CORS headers.

For production use, consider adding SSL/TLS (Let's Encrypt) and securing your RTMP endpoint with authentication. For more details on troubleshooting, see our Troubleshooting Guide.

Related Articles

📚 About M3U8 & HLS

Learn the fundamentals of M3U8 playlists and HLS streaming. Master adaptive bitrate, variant playlists, .ts segments, and understand the M3U8 vs MP4 differences.

🔧 Troubleshooting Guide

Fix common HLS server issues including CORS misconfiguration, buffering problems, 404 errors for .ts segments, and permission/ownership problems.

❓ Frequently Asked Questions

Get answers to 14 common M3U8 and HLS questions covering CORS fixes, adaptive bitrate, DRM support, latency reduction, and embedding the player.

🎬 M3U8 vs MPEG-DASH

Compare HLS and MPEG-DASH streaming protocols. Understand codec support, DRM ecosystems, latency performance, and which protocol fits your use case.