借助 Nginx 搭建带简单认证的直播推流服务器,实现多平台同步直播
原理
使用 nginx-rtmp-module 接收 rtmp 流,然后同时推送到多个直播平台,同时使用 lua-nginx-module 来做简单的身份验证。
安装必须的 nginx 模块
debian 9 用户可以直接使用 apt 安装,需启用 stretch-backports 源:
sudo apt install libnginx-mod-rtmp libnginx-mod-http-lua -t stretch-backports
nginx 配置
Gist: nginx.conf
# file: nginx.conf
# date: 2018-10-13
# license: GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
# author: nanpuyue <[email protected]> https://blog.nanpuyue.com
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
}
rtmp {
server{
listen 1935;
chunk_size 10240;
application live {
live on;
record off;
on_publish http://127.0.0.1:2080/auth;
push rtmp://live.twitch.tv/app/xxxxxxxxxx;
push rtmp://a.rtmp.youtube.com/xxxxxxxxxx;
}
}
}
http {
server {
listen 127.0.0.1:2080;
location /auth {
rewrite_by_lua '
ngx.req.read_body()
local name = ngx.req.get_post_args().name
if (name == "xxxxxxxxxx") then
ngx.status = 200
ngx.say("OK")
ngx.exit(200)
else
ngx.status = 403
ngx.say("Forbidden")
ngx.exit(403)
end
';
}
}
}