restreamer icon indicating copy to clipboard operation
restreamer copied to clipboard

NGINX reverse proxy config for RTMP?

Open wallopthecat opened this issue 5 years ago • 3 comments

Goals

  1. Multiple instances of restreamer on same host behind nginx with https
  2. Must be able to listen on RTMP for each instance (prerequisite was getting env variable custom RTMP ports done)
  3. ~View counts with --with-http_stub_status_module~ Done here

While trying to add custom RTMP ports so I can run multiple streams from the same host, I've run into some issues that stem from my lack of understanding of nginx. These two RTMP ports (1935, 1936) are punched through NAT but I am missing something fundamental, because I cannot stream to mysite.com/live - I have to stream to the internal host IP directly - rtmp://192.168.192.2/live works but rtmp://mysite.com/live does not.

Can any nginx wizards help me understand what is missing to get a solid reverse proxy conf that works with multiple instances and RTMP?

using linuxserver/letsencrypt in front of restreamer. The server block for restreamer instance 1 is below - instance 2 is a dupe but on a subdomain. There are definitely optimizations to be had, which is why I am here

## Version 2019/08/01 - Changelog: https://github.com/linuxserver/docker-letsencrypt/commits/master/root/defaults/default

# redirect all traffic to https
server {
	listen 80;
	listen [::]:80;
	server_name mysite.com;
	return 301 https://$host$request_uri;
}

# main server block
server {
	listen 443 ssl http2;
	listen [::]:443 ssl http2;
	root /config/www/mysite.com;
	index index.html;
	server_name mysite.com;
	include /config/nginx/ssl.conf;
	client_max_body_size 0;

	location / {
		try_files $uri $uri/ /index.html =404;
	}

	location /restreamer {
		return 301 $scheme://$host/restreamer/;
	}

	location ^~ /restreamer/ {
	    rewrite ^/restreamer(/.*)$ $1 break; # remove "restreamer" from the redirect so the app server is just raw http://app:port
		include /config/nginx/proxy.conf;
		resolver 127.0.0.11 valid=30s;
		proxy_pass http://restreamer:8080;
	}
        # does this need to be a TCP proxy? can only stream to rtmp://LOCALIP/live
	location ^~ /live {
		include /config/nginx/proxy.conf;
		resolver 127.0.0.11 valid=30s;
		proxy_pass http://restreamer:8080;
	}
        # --with-http_stub_status_module for hacky view count
	location ^~ /status {
		include /config/nginx/proxy.conf;
		resolver 127.0.0.11 valid=30s;
		proxy_pass http://restreamer:8080/status;
	}
}

Docker compose:

 restreamer:
    image: restreamer-custom-rtmp-port
    container_name: restreamer
    ports:
      - 1935:1935
    volumes:
      - "${DOCKERCONFDIR}/restreamer/db:/restreamer/db"
    environment:
    ...
      - RS_RTMP_PORT=1935
 restreamer2:
    image: restreamer-custom-rtmp-port
    container_name: restreamer2
    ports:
      - 1936:1936
    volumes:
      - "${DOCKERCONFDIR}/restreamer2/db:/restreamer/db"
    environment:
    ...
      - RS_RTMP_PORT=1936

EDIT: will try

server {
 listen 1935;
    proxy_pass restreamer:1935;
          proxy_protocol on;
}

and in the other subdomain
server {
 listen 1936;
    proxy_pass restreamer2:1936;
          proxy_protocol on;
}

wallopthecat avatar Jun 08 '20 17:06 wallopthecat

@wallopthecat I didn't try it out, but I think you could use the stream functionality of nginx to achieve this (http://nginx.org/en/docs/stream/ngx_stream_core_module.html).

It's basically accepting TCP connection on a defined port and proxying it to somewhere else. It might work with this:

stream {
   server {
      listen 1935;
      proxy_pass 192.168.192.2:1935
   }
}

I also found this page, where it is described in the context of a TCP/UDP load balancer (and nginx PLUS), but if you leave out the load balancing part and the PLUS features, then you should get what you want.

ioppermann avatar Jun 09 '20 07:06 ioppermann

Thanks, that was helpful - but I still seem to be missing a key piece. I can now stream to rtmp://192.168.168.2:1936/live and rtmp://192.168.168.2:1935/live but still not the external domain rtmp://mysite.com:1935/live

This location block earlier in my https server section is my naive attempt at proxying earlier. I suspect that in order for this to work, I need to be able to proxy the live application of rtmp as well? If/when we get this working I'll post the full config

	location ^~ /live {
		include /config/nginx/proxy.conf;
		resolver 127.0.0.11 valid=30s;
		proxy_pass http://restreamer:8080;
	}

wallopthecat avatar Jun 10 '20 01:06 wallopthecat

The location block will only be respected for HTTP requests. In order to proxy the RTMP requests, you either have to find a dedicated RTMP proxy or use the nginx stream proxy capabilities on your HTTPS server (the one that serves the requests for mysite.com. This one will accept the RTMP requests and proxies them to the Restreamers in the local net behind it.

ioppermann avatar Jun 14 '20 11:06 ioppermann

Hello

We are closing your ticket https://github.com/datarhei/restreamer/issues/176.

This may be due to the following reasons:

  • Problem/inquiry has been solved
  • Ticket remained unanswered by you for a more extended period
  • Problem was explained and handled in another ticket

You can reopen this ticket at any time!

Please only open related tickets once! Always answer/ask in the original ticket with the same issue!

With kind regards, Your datarhei team

svenerbeck avatar Nov 23 '22 21:11 svenerbeck