Post by

cloud fodder and 121 others

How to setup a streaming server for Nostr with NGINX

Published on

yakihonne.com

Aug 2, 2023

Here is an nginx config for setting up a streaming server that serves HLS. This is what you need if you want to stream from your own streaming server and be viewed on zap.stream , amethyst or other nostr clients that support streaming .

You can connect to it using RTMP -or RMTPS from OBS.

This is an 'advanced guide' it assumes all you need is the nginx.conf and that you can install nginx and use certbot to create the certificates.

I've tested this config on a t3.micro (very small) amazon server and it works great for streaming to my friens.

All you have to do is install nginx, point a DNS record to the server, and use certbot to get some SSL certificates.

nginx.conf

worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
	# multi_accept on;
}


http {

  map $sent_http_content_type $expires {
    default 1d;
    application/vnd.apple.mpegurl epoch;
  }

  server {
    allow all;
    sendfile off;
    tcp_nopush on;
#  aio on;
    directio 512;
  
    include mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;

index test.html;

    location / {
  
                expires $expires;
	   	 # Disable cache
		add_header Cache-Control no-cache;

		# CORS setup
		add_header 'Access-Control-Allow-Origin' '*' always;
		add_header 'Access-Control-Expose-Headers' 'Content-Length';

		# allow CORS preflight requests
		if ($request_method = 'OPTIONS') {
		    add_header 'Access-Control-Allow-Origin' '*';
		    add_header 'Access-Control-Max-Age' 1728000;
		    add_header 'Content-Type' 'text/plain charset=UTF-8';
		    add_header 'Content-Length' 0;
		    return 204;
		}

	types {
                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
		text/html html;
            }

		root /tmp/hls;
	}


        listen 443 ssl;
        server_name mysite.com;
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;

     # managed by Certbot
}
include /etc/nginx/sites-enabled/*.*; 


}

stream {
    upstream backend {
        server 127.0.0.1:1935;
    }

    server {
        listen 1936 ssl;
        proxy_pass backend;
        proxy_protocol on;

        ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem;
    }
}

rtmp {
    server {
        listen 1935 proxy_protocol;
	chunk_size 4000;

	application live {
		live on;
		hls on;
		hls_path /tmp/hls;

		# Use HLS encryption
		hls_keys on;

		# Store auto-generated keys in this location rather than hls_path
		hls_key_path /tmp/hls/keys;

		# Prepend key url with this value
		hls_key_url /keys/;

		# Change HLS key every 2 fragments
		hls_fragments_per_key 100;
	        #hls_playlist_length 5s; 
		#hls_fragment 2s;
	    }
	}
}

here is an index.html page that displays a video player embedded.

<!DOCTYPE html>
<html>
	<meta http-equiv="Content-Type" content="text/html">
<head>
  <link href="https://vjs.zencdn.net/7.8.4/video-js.css" rel="stylesheet" />
</head>
<body>
  <video
    id="my-video"
    class="video-js"
    controls
    preload="auto"
    width="1920"
    height="1080"
    data-setup='{"liveui": true, "liveTracker": true}'>
    <source src="/test2.m3u8" type="application/vnd.apple.mpegurl" />
    <p class="vjs-no-js">
      To view this video please enable JavaScript, and consider upgrading to a
      web browser that
      <a href="https://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
    </p>
  </video>
  <script src="https://vjs.zencdn.net/7.8.4/video.js"></script>
</body>
</html>

As you can see, the URL you would give to zap.stream to setup the stream is https://yoursite/test2.m3u8

You can test the stream is online by visiting mysite/index.html and hitting play.

Hope this helps! At the very least, now I won't lose these precious working configs. PV.

2

2
0
3.2k