Nginx动态代理到Docker容器

问题描述

很抱歉,我不是Nginx专家。

我正在尝试找到一种动态代理到运行Nginx RTMP的Docker容器的方法

我将拥有主要的Nginx映像来执行“路由”,并且每个RTMP实例都将具有自己的配置,从而可以在不干扰其他实例的情况下重新启动每个实例。

我的问题是:如何?

我假设我可以基于“秘密密钥”动态路由RTMP请求,然后将其代理到适当的端口。

类似吗?

server {
    listen 1935;
    chunk_size 4096;
    notify_method get;

    application live {
        on_publish http://localhost/auth;
        live on;
        record all;
        record_path /var/www/html/recordings;
        record_unique on;
        push rtmp://localhost:$dynport;
    }
}

我只是不确定如何获取身份验证脚本以将端口号传递给Nginx

解决方法

我认为这对于nginx和RTMP是不可能的。使用HTTP时,可以通过将所需的目标设置为cookie(不好的主意),但是使用RTMP可以将其入侵。但是在Google中,我没有发现任何迹象表明RTMP可以做任何事情。

在这里,我将为您提供一个示例,说明如何使用HTTP进行操作,也许您会找到一种针对RTMP进行欺骗的方法,因为我对协议并不十分熟悉。 在下面的示例中,我使用curl设置cookie port,并使nginx在proxy指令中使用它。 Nginx可以使用$cookie_{name}变量访问cookie。

version: "3.7"

x-generator-common:
  &common
  image: curlimages/curl:latest
  depends_on:
    - nginx
  entrypoint:
    - /bin/ash
    - -c
  command:
    - while sleep 3; do curl -s -b "port=$$PORT" nginx >/dev/null; done

services:
  generator9000:
    << : *common
    environment:
      PORT: 9000

  generator3000:
    << : *common
    environment:
      PORT: 3000

  nginx:
    image: nginx:alpine
    environment:
      CONFIG: |-
        log_format test "Got request on port $$cookie_port";
        server {
          listen 80;
          access_log /var/log/nginx/access.log test;
          location / { proxy_pass http://127.0.0.1:$$cookie_port; }
        }
        server {
          listen 3000;
          listen 9000;
          access_log off;
          location / { return 200; }
        }
    entrypoint:
      - /bin/ash
      - -c
    command:
      - echo "$$CONFIG" > /etc/nginx/conf.d/default.conf && /docker-entrypoint.sh nginx -g "daemon off;"

UPD: 这是一个示例,您可以从URI中提取内容:

map $uri $port {
    ~^/live/(?<port>.*) $port; 
}
,

您可以使用我创建的这个项目:https://github.com/spartanz51/rtmp-interceptor

根据用户的rtmp密钥将用户重定向到其他RTMP服务器

const RTMPInterceptor = require('rtmp-interceptor')

const params = {
  listenPort: '1936'
}

RTMPInterceptor.listen(params,(client,tcUrl,SKey) => {
  console.log('tcUrl: '+tcUrl)      /* Do something with the data ... */
  console.log('StreamKey: '+SKey)

  return {                          /* Return false to block client and close stream */
    host: 'localhost',port: '1935'
  }
})

这会将用户重定向到localhost:1935