Nginx RTMP流媒体服务器搭建

RTMP(Real Time Messaging Protocol)实时消息传送协议是 Adobe Systems公司为Flash播放器和服务器之间音频、视频和数据传输开发的开放协议。

1. 源码编译安装nginx

1.1 下载

nginx下载:
wget http://nginx.org/download/nginx-1.24.0.tar.gz

nginx-http-flv-module下载:
https://github.com/winshining/nginx-http-flv-module

1.2 解压

tar -xvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
unzip nginx-http-flv-module-master.zip
mv nginx-http-flv-module-master /usr/local/nginx/nginx-http-flv-module-master

1.3 编译和安装

mkdir /usr/local/nginx
./configure --prefix=/usr/local/nginx  --add-module=/usr/local/nginx/nginx-http-flv-module-master
make -j8 && make install

1.4 配置 nginx.conf

cd /usr/local/nginx/conf
vim nginx.conf

可以参照nginx-http-flv-module-master/test/nginx.conf添加以下内容:

rtmp {
    server {
        listen  1935;
        chunk_size  4096;
        application  myapp {
            live on;
        }
    }   
}

1.5 启动运行

cd /usr/local/nginx
./nginx

1.6 测试

通过ffmpeg工具推送test.MP4视频为rtmp://10.201.53.1:1935/myapp/test视频流。

ffmpeg -re -stream_loop -1 -i test.MP4 -f flv  -b:v 1000k -vcodec h264 rtmp://10.201.53.1:1935/myapp/test

2. 开机自启动

2.1 安装在物理机时采用 service 自启动方式

cd  /usr/lib/systemd/system
vim nginx.service

nginx.service内添加如下内容

# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target


配置service自启动

systemctl reload
service nginx start
service nginx status

2.2 安装在docker容器时的自启动方式

若在docker内编译的nginx,可将该容器重新生成镜像nginx-rtmp:v1.0,后续用此镜像来部署环境。

docker run \
	--name nginx-rtmp \
	--user root \
	--network host \
	--privileged=true \
	-v /etc/localtime:/etc/localtime \
	-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
	-w /usr/local/nginx/sbin \
	--ipc host \
	--restart always \
	-dit nginx-rtmp:v1.0 \
	./nginx -g 'daemon off;'
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值