Nginx+RTMP 直播推流服务器
如果已安装Nginx,不论是编译安装还是Yum安装,检查Nginx的版本以及编译时选项:
bash
nginx -V
会出现类似下面的输出:
bash
nginx version: nginx/1.16.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
可以看出,我们的nginx版本是1.16.1,编译选项是从--prefix开始的很长一串的东西。
yum升级nginx
检查centos版本
bash
lsb_release -a
编辑repo文件
bash
$ vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/{version}/$basearch/
gpgcheck=0
enabled=1
#{version}选择大版本号,centos7填7,6填6。
升级更新nginx
bash
yum update
yum update nginx
重启nginx服务
bash
service nginx restart (centos6) | systemctl nginx restart (centos7)
nginx -s reload
如果出现类似这样的错误
bash
nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1014002 in /usr/share/nginx/modules/mod-http-geoip.conf:1
删除原来版本nginx的mod,再重新安装。
bash
yum remove nginx-mod*
yum install nginx-module-*
Nginx重新编译加入RTMP模块
原来已经有nginx的就按照版本下载nginx,没有的就选最新稳定版nginx下载:
bash
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
下载nginx-rtmp模块:
bash
git clone https://github.com/arut/nginx-rtmp-module.git
在解压后的nginx目录中编译安装,编译选项就是已经安装好的nginx的选项:
bash
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/path/to/nginx-rtmp-module
没安装过nginx的(其实使用上面那个也可以):
bash
./configure --prefix=/etc/nginx --add-module=/path/to/nginx-rtmp-module
编译完成以后:
bash
make && make install
修改Nginx配置文件
如果--prefix=/etc/nginx,那么nginx就会被安装到/etc/nginx目录下,编辑该目录下的/etc/nginx/nginx.conf文件,加入rtmp模块:
bash
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
... ...
rtmp {
server {
listen 1935;
chunk_size 4096;
application appname {
live on;
hls on;
wait_key on;
hls_path /var/www/live/hls;
hls_fragment 5s;
hls_playlist_length 30s;
hls_continuous on;
hls_cleanup on;
hls_nested on;
}
}
}
http {
... ...
}
在/etc/nginx/conf.d/中创建新文件live.conf:
bash
server
{
listen 80;
server_name 域名;
index index.html index.htm index.php;
root /var/www/live;
location / {
index index.html index.htm;
}
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /var/www/live/hls;
add_header Cache-Control no-cache;
}
}
在相应的位置创建对应文件夹:
bash
mkdir /var/www/live
mkdir /var/www/live/hls
在/var/www/live中创建对应的index.html文件:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Astronomy</title>
<link class="dplayer-css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.css">
<style>
h1 {
text-align: center;
background: linear-gradient(to right, lightblue, dodgerblue, darkblue);
-webkit-background-clip: text;
color: transparent;
font-size: 2.7em; /* 放大标题 */
margin-bottom: 50px; /* 增加标题和播放器之间的距离 */
}
#player {
width: 50%;
height: 50%;
margin: 0 auto;
}
</style>
</head>
<body>
<h1>直播间</h1>
<div id="player"></div>
<script src="https://cdn.jsdelivr.net/npm/hls.js/dist/hls.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dplayer/dist/DPlayer.min.js"></script>
<script type="application/javascript">
function createApiBackend (url, onMessage) {
var ws;
var connect = function () {
ws = new WebSocket(url);
ws.onmessage = function (event) {
onMessage(JSON.parse(event.data));
};
ws.onclose = function () {
// Try to reconnect in 5 seconds
setTimeout(connect, 5000);
};
};
var connected = false;
window.addEventListener('beforeunload', function () {
ws.onclose = null;
ws.close();
});
return {
read: function (options) {
if (connected) {
return;
}
connected = true;
connect();
options.success();
},
send: function (options) {
ws.send(JSON.stringify(options.data));
options.success();
}
};
}
var dp = new DPlayer({
container: document.getElementById('player'),
live: true,
autoplay: true,
danmaku: true,
apiBackend: createApiBackend('danmaku_url', function (dan) {
dp.danmaku.draw(dan);
}),
video: {
url: 'hls/test/index.m3u8',
type: 'hls'
}
});
</script>
</body>
</html>
重启nginx服务。
FFMPEG推流测试
FFMPEG 本地视频推流
bash
ffmpeg -re -i /path/to/file.mp4 -c copy -f flv rtmp://ip:port/appname/streamname
FFMPEG Mac桌面推流
bash
ffmpeg -f avfoundation -i "1" -vcodec libx264 -preset ultrafast -acodec libfaac -f flv rtmp://ip:port/appname/streamname
打开http://server_name就可以看到直播推流了。如果看不到,可以试着把nginx.conf中的user改成root。
其他
连通问题
移动或者联通的网不稳定,可能需要给域名分发证书。使用certbot。
html
https://certbot.eff.org/
bash
/usr/local/bin/certbot-auto --nginx
推流统计
在live.conf中加入nginx-rtmp-module中的/stat
网页播放器
目前使用的dplayer,如果要加弹幕,去安装dplayer的danmuku api。
其余问题
如果有再补上。