Docker 个容器的 Nginx 动态代理
Nginx Dynamic Proxy to Docker Containers
我正在尝试找出解决此问题的方法,不幸的是我不是 Nginx 专家。
我正在尝试找到一种动态代理到 Docker 容器的方法,这些容器是 运行 Nginx RTMP。
我将拥有执行“路由”的主要 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;
}
}
我只是不确定如何让 auth 脚本将端口号传递给 nginx。
我认为这对于 nginx 和 RTMP 是不可能的。使用 HTTP 可以通过将所需的目标设置到 cookie 中来破解它(坏主意)但是使用 RTMP ......我没有在 Google 中找到任何迹象表明 RTMP 可以做任何事情。
我将在此处为您提供如何使用 HTTP 完成此操作的示例,也许您会找到一种针对 RTMP 进行欺骗的方法,因为我对该协议并不十分熟悉。
在下面的示例中,我使用 curl
设置 cookie port
并让 nginx 在代理指令中使用它。 Nginx 可以使用 $cookie_{name}
变量访问 cookies。
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;"
更新:
这是一个如何从 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
我正在尝试找出解决此问题的方法,不幸的是我不是 Nginx 专家。
我正在尝试找到一种动态代理到 Docker 容器的方法,这些容器是 运行 Nginx RTMP。
我将拥有执行“路由”的主要 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;
}
}
我只是不确定如何让 auth 脚本将端口号传递给 nginx。
我认为这对于 nginx 和 RTMP 是不可能的。使用 HTTP 可以通过将所需的目标设置到 cookie 中来破解它(坏主意)但是使用 RTMP ......我没有在 Google 中找到任何迹象表明 RTMP 可以做任何事情。
我将在此处为您提供如何使用 HTTP 完成此操作的示例,也许您会找到一种针对 RTMP 进行欺骗的方法,因为我对该协议并不十分熟悉。
在下面的示例中,我使用 curl
设置 cookie port
并让 nginx 在代理指令中使用它。 Nginx 可以使用 $cookie_{name}
变量访问 cookies。
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;"
更新: 这是一个如何从 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