系统环境: CentOS 8.1.1911 最小化+开发工具
更换yum源为阿里云
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
yum makecache
关闭firewalld和selinux
systemctl disable firewalld.service
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
reboot
安装环境依赖
yum groupinstall 'Development Tools' -y
yum install epel-release -y
yum install -y git unzip perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 gd-devel GeoIP GeoIP-devel GeoIP-data
下载组件文件
wget http://nginx.org/download/nginx-1.18.0.tar.gz
wget https://ftp.pcre.org/pub/pcre/pcre-8.42.zip
wget https://www.openssl.org/source/openssl-1.1.0h.tar.gz
tar -xzvf openssl-1.1.0h.tar.gz
unzip pcre-8.42.zip
tar -xvzf nginx-1.18.0.tar.gz
git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git
nginx安装
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx \
--with-select_module \
--with-poll_module \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_geoip_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--http-client-body-temp-path=/usr/local/nginx/client_temp \
--http-proxy-temp-path=/usr/local/nginx/proxy_temp \
--http-fastcgi-temp-path=/usr/local/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp \
--http-scgi-temp-path=/usr/local/nginx/scgi_temp \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module \
--with-compat \
--with-pcre-jit \
--with-pcre=../pcre-8.42 \
--with-openssl=../openssl-1.1.0h \
--with-openssl-opt=no-nextprotoneg \
--add-module=../nginx-rtmp-module \
--with-debug
make
make install
测试启动
/usr/local/nginx/sbin/nginx
ss -tnlp | grep nginx
配置RTMP
vi /usr/local/nginx/conf/nginx.conf
worker_processes auto;
events {
worker_connections 1024;
}
# RTMP configuration
rtmp {
server {
listen 1935; #推流监听端口
chunk_size 4000;
application yuchen {
live on;
hls on;
hls_path /usr/local/nginx/html/yuchen;
hls_fragment 10s;
}
}
}
http {
sendfile off;
tcp_nopush on;
aio on;
directio 512;
default_type application/octet-stream;
server {
listen 1936; #拉流监听端口
server_name localhost;
location / {
add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header 'Access-Control-Allow-Headers' 'Range';
root /usr/local/nginx/html/;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
pkill -9 nginx
/usr/local/nginx/sbin/nginx
ss -tnlp | grep nginx
推流地址
rtmp://ip:1935/yuchen/
串流密钥yuchen
(这里使用工具OBS-Studio-26.1.1-Full-Installer-x64.exe)
在potplayer中播放http的m3u8地址。或者是使用支持rtmp协议的播放器拉流
拉流地址
http://ip:1936/yuchen/yuchen.m3u8
完事!
注意:
- rtmp{}的内容和http{}为同级,位置不要放错。
- 苹果系统原生H5支持m3u8,直接浏览器播放即可。
- 会存在3-5秒的延时。
参考文献
1.https://www.atlantic.net/vps-hosting/how-to-install-nginx-with-rtmp-module-on-centos-8/
2.通过nginx扩展nginx-rtmp-module简单做了一个流媒体直播-博客园