19 Setting up an HLS Broadcast server with NGINX
rainbow napkin edited this page 2025-03-19 02:32:01 +00:00

Step-by-step procedure for Setting up an HLS Broadcast server with NGINX

Installation & Prep

  1. Spin up a new Debian 12 box w/ a properly configured UFW firewall. This is out of scope for this guide...
  2. Install nginx and it's associated RTMP module sudo apt install nginx libnginx-mod-rtmp
  3. Create a location to store your hls stream, as such mkdir /srv/live && chown www-data:www-data /srv/live

RTMP Server Configuration

The first part of this server config handles ingesting the raw RTMP datastream and converting to a series of files which can be fetched via web request, as the HLS protocol perscribes.

  1. Edit open /etc/nginx/nginx.conf
  2. Add the following block to your nginx.conf:
rtmp {
	server {
		listen 1935;
		chunk_size 4096;
		max_message 1M;
		#allow publish from specific ip
		allow publish 192.168.69.69;
		#deny publish to everyone else
		deny publish all;

		application stream{
			live on;
			hls on;
			hls_nested on;
			hls_path /srv/live;

                        #This changes how nginx splits the streams into files
                        #Increasing fragment length will increase delay
                        #Make sure your keyframe interval in OBS matches this number!
                        #Split Stream into 2 second fragments
   			hls_fragment 2s;
                        #Provide 10 seconds worth of fragments (5 fragments)
			hls_playlist_length 10s;
		}
	}
}

Web Server Configuration

This part of the configuration handles setting up the web server which serves the files that make up the HLS stream.

  1. Edit /etc/nginx/sites-available/default or create a new site config
    Remember to create a symlink and move/delete the default if you create a new file!
  2. Add the following to your site config:
server {
	listen 80;
	listen [::]:80 ipv6only=on;

	root /srv/live;

	server_name your.domain.name;

	location / {
                #You'll probably want to use this from another site :P
		add_header Access-Control-Allow-Origin https://domain.to.allow;
		add_header Cache-Control no-cache;

                #Default to playlist file to allow users to just paste the bare domain
		try_files $uri $uri/index.m3u8;

		types{
			application/vnd.apple.mpegurl m3u8;
			video/mp2t ts;
		}
	}
}

SSL Configuration

For SSL configuration, we recommend following the guide provided by the eff.

Streaming:

You should now be able to stream to your server from OBS to rtmp://your.domain.name/stream. The stream can be tested by entering https://your.domain.name into a modern copy of VLC. Make sure the keyframe interval setting in OBS matches the supplied hls_fragment property in /etc/nginx/nginx.conf for minimum latency. The ourfore.st streaming server seems to have about 10s round trip according to testing.