Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
nginx-rtmp at LAN sample configuration
##################
## LANSTREAM ##
##################
# This is a sample configuration file
# Please don't copy and paste this exactly, read through it and understand what you're doing
# nginx-rtmp can be a little strange when working multithreaded, so we turn it off
worker_processes 1;
events {
# defines the number of connections we can take at once, 1024 should be plenty
worker_connections 1024;
}
# this block is for standard HTTP requests over port 80
http {
# define some basic ground rules
include mime.types;
default_type application/octet-stream;
types_hash_bucket_size 64;
server_names_hash_bucket_size 128;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" args "$args"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
# note: my rooting isn't exceptional, please feel free to fix this
root /etc/nginx/html;
# This URL provides RTMP statistics in XML, extremely useful
location /stat {
rtmp_stat all;
# Use this stylesheet to view XML as web page
# in browser
rtmp_stat_stylesheet stat.xsl;
}
# Control interface (extremely useful, but can also boot people from streams so we put basic auth in front of it - see https://github.com/arut/nginx-rtmp-module/wiki/Control-module for more information)
location /control {
auth_basic "LanStream";
# you'll need a htpasswd auth file, that's outside the scope of this doc but any apache one will work
auth_basic_user_file /etc/nginx/htpasswd;
# tell nginx-rtmp that this is the control interface
rtmp_control all;
}
# stat.xsl for the statistics module, copy this from the nginx-rtmp-module source and pop it somewhere sane
# otherwise stats won't format properly
location /stat.xsl {
# and put the full directory path here
root /srv/nginx/;
}
# this URL will return HTTP 201 if it's sent the correct key in the GET request, otherwise it'll return HTTP 500.
# for our uses, the on_publish hook will call this and drop the incoming stream if it gets a 50x back
location /auth {
if ($arg_psk = 'CHANGEMELOL') {
return 201;
}
return 500;
}
# HLS streaming for apple devices, HTTP side
# nginx will store the HLS fragments for your stream here, so make sure it's writable by nginx
location /hls {
root /srv/nginx/hls;
autoindex on;
autoindex_localtime on;
set $sent_http_accept_ranges bytes;
add_header Cache-Control no-cache;
types {
video/MP2T ts;
audio/x-mpegurl m3u8;
}
}
# mpeg-dash for HTML5, HTTP side
# again, nginx stores DASH fragments here, so make sure it's writable by nginx
location /dash {
root /srv/nginx/dash;
add_header Cache-Control no-cache;
autoindex on;
autoindex_localtime on;
add_header Access-Control-Allow-Origin *;
}
# all other HTML
# you could pop some fancy players and stuff in here, up to you
location / {
index index.html;
}
}
}
# RTMP section - this is where it gets interesting
rtmp {
server {
# some more basic ground rules
listen 1935;
ping 30s;
chunk_size 4000;
notify_method get;
# this is our restricted area, anything pushed here will get sent up to the internets to our channel (but also available internally)
application out {
# enable live streaming and set some ground rules, some of these are set for twitch.tv compatibility
live on;
interleave on;
meta off;
publish_notify on;
wait_key on;
wait_video on;
drop_idle_publisher 10s;
# HLS settings
hls on;
hls_path /srv/nginx/hls/;
hls_fragment 10s;
hls_playlist_length 30s;
# MPEG-DASH settings
dash on;
dash_path /srv/nginx/dash/;
# access control (see the /auth section in http further up)
on_publish "http://localhost/auth";
# if you'd like to execute a script when the stream goes up or down, here's some examples
# exec_publish /srv/nginx/flagstream.sh up;
# exec_publish_done /srv/nginx/flagstream.sh down;
# push to:
# youtube live - be sure to register an event first
# push "rtmp://a.rtmp.youtube.com/live2/STREAMKEY";
# twitch.tv - this is the london heathrow server, for a full list see http://bashtech.net/twitch/ingest.php
# push "rtmp://live-lhr.twitch.tv/app/STREAMKEY";
# if you want to restrict publishing or playback to a certain subnet or even a certain IP, do so here
# by default anyone can publish and play
# allow publish 192.168.1.1;
allow publish all;
# allow play 192.168.0.0/24;
allow play all;
}
# anyone can push to this application, and it's only available internally - doesn't produce HLS or DASH segments, but you can easily add if it you want it
application interna {
# enable live streaming & set some ground rules
live on;
interleave on;
meta off;
publish_notify on;
wait_key on;
wait_video on;
drop_idle_publisher 10s;
# publish rules
allow publish all;
allow play all;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment