title: Rails, Devise and Nginx RTMP Authentication
body: <p>I am working on a live streaming service that will be used by a pretty small community, and I am currently unable to figure out how to secure the nginx server that takes in rtmp from OBS/xSplit and puts it out as HLS. The HLS is played using VideoJS. User accounts are done using the Devise module, the usernames are used to create the path for the client and the streamer. I need to add stream keys to protect the user accounts from being streamed to by anyone, this can be done with on_publish and PHP in nginx.conf, but that wouldn't work with my RoR/Devise accounts system. What can I do to do this within Devise and Rails?</p>
  
  <p><strong>Edit:</strong> I was asked to add some code, though I don't know of any method to do this in Rails, here is a PHP and Python example <a href="https://github.com/Nesseref/nginx-rtmp-auth" rel="nofollow noreferrer">https://github.com/Nesseref/nginx-rtmp-auth</a></p>
  
  <p>The NGINX http server is running on port 8080, but is behind an apache2 proxy on port 80, as this machine hosts multiple websites.</p>
  
  <p>Here's my NGINX config</p>
  
  <pre><code>worker_processes  1;
  
  
  events {
      worker_connections  1024;
  }
  
  
  http {
      include       mime.types;
      default_type  application/octet-stream;
  
  
      sendfile        on;
      keepalive_timeout  65;
  
      #gzip  on;
  
      server {
          listen       8080;
          server_name  localhost;
           location /hls {
              types {
                  application/vnd.apple.mpegurl m3u8;
              }
              root /tmp;
              add_header Cache-Control no-cache;
  
              # To avoid issues with cross-domain HTTP requests (e.g. during development)
              add_header Access-Control-Allow-Origin *;
          }
  
          location / {
              root   html;
              index  index.html index.htm;
          }
          location /stat {
              rtmp_stat all;
              rtmp_stat_stylesheet stat.xsl;
          }
          location /stat.xsl {
              root /var/www/html/;
          }
  
  
  
  
          #error_page  404              /404.html;
  
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   html;
          }
      }
  
  }
  
  
  rtmp {
          server {
                  listen 1935;
                  chunk_size 4096;
  
                  application live {
                          live on;
                          deny play all;
                          hls on;
                          hls_path /tmp/hls;
                          hls_fragment 15s;
                  }
          }
  }
  </code></pre>