CentOS8搭建RTMP直播服务器笔记 Nginx+RTMP
1. 下载Nginx-RTMP模块
1.1 安装git命令
yum -y install git
[root@VM-32-7-centos ~]# yum -y install git
Repository epel is listed more than once in the configuration
Extra Packages for Enterprise Linux Modular 8 - x86_64 5.1 kB/s | 7.3 kB 00:01
Extra Packages for Enterprise Linux Modular 8 - x86_64 195 kB/s | 465 kB 00:02
MySQL 8.0 Community Server 1.0 kB/s | 2.6 kB 00:02
MySQL 8.0 Community Server 577 kB/s | 2.3 MB 00:04
MySQL Connectors Community 1.3 kB/s | 2.6 kB 00:01
MySQL Tools Community 1.8 kB/s | 2.6 kB 00:01
nginx stable repo 1.4 kB/s | 2.9 kB 00:02
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Module yaml error: Unexpected key in data: static_context [line 9 col 3]
Dependencies resolved.
=================================================================================================================
Package Architecture Version Repository Size
=================================================================================================================
Installing:
git x86_64 2.27.0-1.el8 appstream 164 k
Installing dependencies:
git-core x86_64 2.27.0-1.el8 appstream 5.7 M
git-core-doc noarch 2.27.0-1.el8 appstream 2.5 M
perl-Error noarch 1:0.17025-2.el8 appstream 46 k
perl-Git noarch 2.27.0-1.el8 appstream 77 k
perl-TermReadKey x86_64 2.37-7.el8 appstream 40 k
Transaction Summary
=================================================================================================================
Install 6 Packages
Total download size: 8.5 M
Installed size: 45 M
Downloading Packages:
(1/6): git-2.27.0-1.el8.x86_64.rpm 2.1 MB/s | 164 kB 00:00
(2/6): perl-Error-0.17025-2.el8.noarch.rpm 1.5 MB/s | 46 kB 00:00
(3/6): perl-Git-2.27.0-1.el8.noarch.rpm 2.3 MB/s | 77 kB 00:00
(4/6): perl-TermReadKey-2.37-7.el8.x86_64.rpm 2.3 MB/s | 40 kB 00:00
(5/6): git-core-doc-2.27.0-1.el8.noarch.rpm 10 MB/s | 2.5 MB 00:00
(6/6): git-core-2.27.0-1.el8.x86_64.rpm 20 MB/s | 5.7 MB 00:00
-----------------------------------------------------------------------------------------------------------------
Total 29 MB/s | 8.5 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : git-core-2.27.0-1.el8.x86_64 1/6
Installing : git-core-doc-2.27.0-1.el8.noarch 2/6
Installing : perl-TermReadKey-2.37-7.el8.x86_64 3/6
Installing : perl-Error-1:0.17025-2.el8.noarch 4/6
Installing : perl-Git-2.27.0-1.el8.noarch 5/6
Installing : git-2.27.0-1.el8.x86_64 6/6
Running scriptlet: git-2.27.0-1.el8.x86_64 6/6
Verifying : git-2.27.0-1.el8.x86_64 1/6
Verifying : git-core-2.27.0-1.el8.x86_64 2/6
Verifying : git-core-doc-2.27.0-1.el8.noarch 3/6
Verifying : perl-Error-1:0.17025-2.el8.noarch 4/6
Verifying : perl-Git-2.27.0-1.el8.noarch 5/6
Verifying : perl-TermReadKey-2.37-7.el8.x86_64 6/6
Installed:
git-2.27.0-1.el8.x86_64 git-core-2.27.0-1.el8.x86_64 git-core-doc-2.27.0-1.el8.noarch
perl-Error-1:0.17025-2.el8.noarch perl-Git-2.27.0-1.el8.noarch perl-TermReadKey-2.37-7.el8.x86_64
Complete!
1.2 下载nginx-rtmp-module
- 使用git下载github站点的 nginx-rtmp-module 模块
git clone https://github.com/arut/nginx-rtmp-module.git
跳转目录到用户目录下载nginx-rtmp-module
[root@VM-32-7-centos /]# cd #
[root@VM-32-7-centos ~]# git clone https://github.com/arut/nginx-rtmp-module.git
Cloning into 'nginx-rtmp-module'...
remote: Enumerating objects: 4324, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 4324 (delta 1), reused 1 (delta 0), pack-reused 4317
Receiving objects: 100% (4324/4324), 3.11 MiB | 55.00 KiB/s, done.
Resolving deltas: 100% (2689/2689), done.
[root@VM-32-7-centos ~]# ll
total 8
drwxr-xr-x 4 root root 4096 Feb 26 04:42 download
drwxr-xr-x 7 root root 4096 Aug 14 02:18 nginx-rtmp-module
2. 下载Nginx源码包
2.1 已经安装Nginx
2.1.1 查看Nginx运行情况
[root@VM-32-7-centos ~]# ps -ef | grep nginx
root 1335 1 0 00:01 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 1336 1335 0 00:01 ? 00:00:00 nginx: worker process
nginx 1337 1335 0 00:01 ? 00:00:00 nginx: worker process
root 3890 3067 0 00:14 pts/0 00:00:00 grep --color=auto nginx
2.1.2 下载Nginx已安装版本源码
- 查看Nginx版本
[root@VM-32-7-centos ~]# nginx -v
nginx version: nginx/1.22.0
- 下载源码
wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@VM-32-7-centos ~]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
--2022-08-14 02:21:21-- http://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.0.tar.gz’
nginx-1.22.0.tar.gz 100%[======================================>] 1.02M 281KB/s in 3.7s
2022-08-14 02:21:25 (281 KB/s) - ‘nginx-1.22.0.tar.gz’ saved [1073322/1073322]
[root@VM-32-7-centos ~]# ll
total 1060
drwxr-xr-x 4 root root 4096 Feb 26 04:42 download
-rw-r--r-- 1 root root 1073322 May 24 22:29 nginx-1.22.0.tar.gz
drwxr-xr-x 7 root root 4096 Aug 14 02:18 nginx-rtmp-module
- 解压包
tar -zxvf nginx-1.22.0.tar.gz
[root@VM-32-7-centos ~]# ll
total 1064
drwxr-xr-x 4 root root 4096 Feb 26 04:42 download
drwxr-xr-x 8 live fpmuser 4096 May 24 07:59 nginx-1.22.0
-rw-r--r-- 1 root root 1073322 May 24 22:29 nginx-1.22.0.tar.gz
drwxr-xr-x 7 root root 4096 Aug 14 02:18 nginx-rtmp-module
- 停止运行的Nginx服务,这步很重要
nginx -s stop
[root@VM-32-7-centos ~]# nginx -s stop
[root@VM-32-7-centos ~]# ps -ef | grep nginx
root 171687 171437 0 02:23 pts/1 00:00:00 grep --color=auto nginx
2.2 未安装Nginx
2.2.1 下载Nginx源码
-
下载源码
wget http://nginx.org/download/nginx-1.22.0.tar.gz -
解压包
tar -zxvf nginx-1.22.0.tar.gz
3. 编译 Nginx和Module
3.1 查看已安装Nginx的Module
[root@VM-32-7-centos ~]# nginx -V
nginx version: nginx/1.22.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
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 -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
3.2 编译nginx-rtmp-module
3.2.1 进入nginx源码目录
[root@VM-32-7-centos ~]# cd nginx-1.22.0
[root@VM-32-7-centos nginx-1.22.0]# ll
total 824
drwxr-xr-x 6 live fpmuser 4096 Aug 14 02:22 auto
-rw-r--r-- 1 live fpmuser 317070 May 24 07:59 CHANGES
-rw-r--r-- 1 live fpmuser 484445 May 24 07:59 CHANGES.ru
drwxr-xr-x 2 live fpmuser 4096 Aug 14 02:22 conf
-rwxr-xr-x 1 live fpmuser 2590 May 24 07:59 configure
drwxr-xr-x 4 live fpmuser 4096 Aug 14 02:22 contrib
drwxr-xr-x 2 live fpmuser 4096 Aug 14 02:22 html
-rw-r--r-- 1 live fpmuser 1397 May 24 07:59 LICENSE
drwxr-xr-x 2 live fpmuser 4096 Aug 14 02:22 man
-rw-r--r-- 1 live fpmuser 49 May 24 07:59 README
drwxr-xr-x 9 live fpmuser 4096 Aug 14 02:22 src
3.2.2 配置编译
3.2.2.1 在已安装版本上增加RTMP模块配置
./configure --add-module=/path/to/nginx-rtmp-module
/path/to 替换rtmp源码包路径,相对路径或绝对路径都行:
./configure --add-module=../nginx-rtmp-module
或
我的是放在/root目录的
./configure --add-module=/root/nginx-rtmp-module
最重要的是把原来的已经安装模块和编译配置加上
[root@VM-32-7-centos nginx-1.20.2]# ./configure --add-module=../nginx-rtmp-module --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
checking for OS
+ Linux 4.18.0-305.25.1.el8_4.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... found
checking for eventfd() ... found
checking for O_PATH ... found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for kqueue AIO support ... not found
checking for Linux AIO support ... found
checking for int size ... 4 bytes
checking for long size ... 8 bytes
checking for long long size ... 8 bytes
checking for void * size ... 8 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 8 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 8 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for strerrordesc_np() ... not found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for ioctl(FIONREAD) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
configuring additional modules
adding module in ../nginx-rtmp-module
+ ngx_rtmp_module was configured
checking for PCRE2 library ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using threads
+ using system PCRE2 library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/etc/nginx"
nginx binary file: "/usr/sbin/nginx"
nginx modules path: "/usr/lib64/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
3.2.2.2 全新安装Nginx版本RTMP模块配置
./configure --add-module=../nginx-rtmp-module --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx
3.2.3 编译安装
3.2.3.1 已安装Nginx,只编译不自动安装
3.2.3.1.1 只编译生成nginx主程序
已有nginx情况,只编译不安装,安装会覆盖原有的配置,手动复制新编译的nginx主程序
make
3.2.3.1.2 查看编译的主程序的RTMP模块
[root@VM-32-7-centos nginx-1.22.0]# ll
total 832
drwxr-xr-x 6 live fpmuser 4096 Aug 14 04:15 auto
-rw-r--r-- 1 live fpmuser 317070 May 24 07:59 CHANGES
-rw-r--r-- 1 live fpmuser 484445 May 24 07:59 CHANGES.ru
drwxr-xr-x 2 live fpmuser 4096 Aug 14 04:15 conf
-rwxr-xr-x 1 live fpmuser 2590 May 24 07:59 configure
drwxr-xr-x 4 live fpmuser 4096 Aug 14 04:15 contrib
drwxr-xr-x 2 live fpmuser 4096 Aug 14 04:15 html
-rw-r--r-- 1 live fpmuser 1397 May 24 07:59 LICENSE
-rw-r--r-- 1 root root 387 Aug 14 04:17 Makefile
drwxr-xr-x 2 live fpmuser 4096 Aug 14 04:15 man
drwxr-xr-x 4 root root 4096 Aug 14 04:19 objs
-rw-r--r-- 1 live fpmuser 49 May 24 07:59 README
drwxr-xr-x 9 live fpmuser 4096 Aug 14 04:15 src
[root@VM-32-7-centos nginx-1.22.0]# cd objs
[root@VM-32-7-centos objs]# ll
total 10536
drwxr-xr-x 5 root root 4096 Aug 14 04:16 addon
-rw-r--r-- 1 root root 19384 Aug 14 04:16 autoconf.err
-rw-r--r-- 1 root root 76132 Aug 14 04:17 Makefile
-rwxr-xr-x 1 root root 10570032 Aug 14 04:19 nginx
-rw-r--r-- 1 root root 5500 Aug 14 04:19 nginx.8
-rw-r--r-- 1 root root 9862 Aug 14 04:17 ngx_auto_config.h
-rw-r--r-- 1 root root 657 Aug 14 04:16 ngx_auto_headers.h
-rw-r--r-- 1 root root 13346 Aug 14 04:16 ngx_modules.c
-rw-r--r-- 1 root root 66112 Aug 14 04:19 ngx_modules.o
drwxr-xr-x 9 root root 4096 Aug 14 04:16 src
[root@VM-32-7-centos objs]# ./nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --add-module=../nginx-rtmp-module --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
出现“–add-module=…/nginx-rtmp-module”模块信息,RTMP模块编译成功
3.2.3.1.3 手动复制更新Nginx主程序
从源码目录的objs中复制编译完成的主程序
cp /root/nginx-1.22.0/objs/nginx /usr/sbin/nginx
3.2.3.2 全新安装
3.2.3.2.1 编译并安装
make && make install
3.2.3.2.2 配置Nginx主程序的运行环境变量
3.2.3.2.2.1 方法1:自建环境变量文件
- 创建环境变量文件
vim /etc/profile.d/nginx.sh
export PATH="$PATH:/usr/local/nginx/sbin" #nginx实际安装目录
- 立即生效配置
source /etc/profile
3.2.3.2.2.2 方法2:添加至系统环境变量里
- 通过echo命令管道添加到系统变量里
echo export PATH=$PATH:/usr/local/nginx/sbin >>/etc/profile - 立即生效配置
source /etc/profile
3.2.3.2.2.3 方法3:创建 nginx 的软连接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
3.2.3.2.3 全新安装Nginx的RTMP模块情况
[root@VM-32-7-centos ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --add-module=../nginx-rtmp-module
4. 配置Nginx的RTMP协议
4.1 查找conf
find / -name nginx.conf
[root@VM-32-7-centos ~]# find / -name nginx.conf
/etc/nginx/nginx.conf
[root@VM-32-7-centos ~]# find /etc/nginx -name *.conf
/etc/nginx/nginx.conf
/etc/nginx/default.d/php.conf
/etc/nginx/conf.d/ups_laravelS.conf
/etc/nginx/conf.d/svr_www.conf
/etc/nginx/conf.d/ups_php-fpm.conf
/etc/nginx/conf.d/svr_live.conf
子域名站点分别命名放置 /etc/nginx/conf.d/ 目录下,方便独立配置管理各子站点
- svr_www.conf 网站www.domain.com配置文件
- svr_live.conf 网站live.domain.com配置文件
注意:conf.d 目录的配置是包含在nginx.conf的http{} 标签下的,rtmp{} 标签是与http{} 平级标签,后续配置rtmp时注意。
4.2 编辑conf
4.2.1 加入RTMP服务器配置
- 创建流媒体网站目录
mkdir -p /home/live - 进入nginx-rtmp-module目录,复制stat.xsl状态文件到站点目录
cp stat.xsl /home/live
[root@VM-32-7-centos ~]# ll
total 1064
drwxr-xr-x 4 root root 4096 Feb 26 04:42 download
drwxr-xr-x 9 live fpmuser 4096 Aug 14 04:16 nginx-1.22.0
-rw-r--r-- 1 root root 1073322 May 24 22:29 nginx-1.22.0.tar.gz
drwxr-xr-x 7 root root 4096 Aug 14 02:18 nginx-rtmp-module
[root@VM-32-7-centos ~]# cd nginx-rtmp-module
[root@VM-32-7-centos nginx-rtmp-module]# ll
total 832
-rw-r--r-- 1 root root 115 Aug 14 02:18 AUTHORS
-rw-r--r-- 1 root root 5882 Aug 14 02:18 config
drwxr-xr-x 2 root root 4096 Aug 14 02:18 dash
drwxr-xr-x 2 root root 4096 Aug 14 02:18 doc
drwxr-xr-x 2 root root 4096 Aug 14 02:18 hls
-rw-r--r-- 1 root root 1307 Aug 14 02:18 LICENSE
-rw-r--r-- 1 root root 11729 Aug 14 02:18 ngx_rtmp_access_module.c
-rw-r--r-- 1 root root 16504 Aug 14 02:18 ngx_rtmp_amf.c
-rw-r--r-- 1 root root 1933 Aug 14 02:18 ngx_rtmp_amf.h
-rw-r--r-- 1 root root 17802 Aug 14 02:18 ngx_rtmp_auto_push_module.c
-rw-r--r-- 1 root root 589 Aug 14 02:18 ngx_rtmp_bandwidth.c
-rw-r--r-- 1 root root 584 Aug 14 02:18 ngx_rtmp_bandwidth.h
-rw-r--r-- 1 root root 1062 Aug 14 02:18 ngx_rtmp_bitop.c
-rw-r--r-- 1 root root 1162 Aug 14 02:18 ngx_rtmp_bitop.h
-rw-r--r-- 1 root root 22251 Aug 14 02:18 ngx_rtmp.c
-rw-r--r-- 1 root root 22685 Aug 14 02:18 ngx_rtmp_cmd_module.c
-rw-r--r-- 1 root root 4817 Aug 14 02:18 ngx_rtmp_cmd_module.h
-rw-r--r-- 1 root root 26153 Aug 14 02:18 ngx_rtmp_codec_module.c
-rw-r--r-- 1 root root 2640 Aug 14 02:18 ngx_rtmp_codec_module.h
-rw-r--r-- 1 root root 18855 Aug 14 02:18 ngx_rtmp_control_module.c
-rw-r--r-- 1 root root 21001 Aug 14 02:18 ngx_rtmp_core_module.c
-rw-r--r-- 1 root root 6132 Aug 14 02:18 ngx_rtmp_eval.c
-rw-r--r-- 1 root root 843 Aug 14 02:18 ngx_rtmp_eval.h
-rw-r--r-- 1 root root 41739 Aug 14 02:18 ngx_rtmp_exec_module.c
-rw-r--r-- 1 root root 19445 Aug 14 02:18 ngx_rtmp_flv_module.c
-rw-r--r-- 1 root root 19517 Aug 14 02:18 ngx_rtmp.h
-rw-r--r-- 1 root root 24055 Aug 14 02:18 ngx_rtmp_handler.c
-rw-r--r-- 1 root root 16976 Aug 14 02:18 ngx_rtmp_handshake.c
-rw-r--r-- 1 root root 7412 Aug 14 02:18 ngx_rtmp_init.c
-rw-r--r-- 1 root root 5553 Aug 14 02:18 ngx_rtmp_limit_module.c
-rw-r--r-- 1 root root 32748 Aug 14 02:18 ngx_rtmp_live_module.c
-rw-r--r-- 1 root root 2709 Aug 14 02:18 ngx_rtmp_live_module.h
-rw-r--r-- 1 root root 25282 Aug 14 02:18 ngx_rtmp_log_module.c
-rw-r--r-- 1 root root 71083 Aug 14 02:18 ngx_rtmp_mp4_module.c
-rw-r--r-- 1 root root 19230 Aug 14 02:18 ngx_rtmp_netcall_module.c
-rw-r--r-- 1 root root 2138 Aug 14 02:18 ngx_rtmp_netcall_module.h
-rw-r--r-- 1 root root 47966 Aug 14 02:18 ngx_rtmp_notify_module.c
-rw-r--r-- 1 root root 32906 Aug 14 02:18 ngx_rtmp_play_module.c
-rw-r--r-- 1 root root 2536 Aug 14 02:18 ngx_rtmp_play_module.h
-rw-r--r-- 1 root root 3872 Aug 14 02:18 ngx_rtmp_proxy_protocol.c
-rw-r--r-- 1 root root 311 Aug 14 02:18 ngx_rtmp_proxy_protocol.h
-rw-r--r-- 1 root root 11691 Aug 14 02:18 ngx_rtmp_receive.c
-rw-r--r-- 1 root root 36046 Aug 14 02:18 ngx_rtmp_record_module.c
-rw-r--r-- 1 root root 2992 Aug 14 02:18 ngx_rtmp_record_module.h
-rw-r--r-- 1 root root 46437 Aug 14 02:18 ngx_rtmp_relay_module.c
-rw-r--r-- 1 root root 2281 Aug 14 02:18 ngx_rtmp_relay_module.h
-rw-r--r-- 1 root root 15709 Aug 14 02:18 ngx_rtmp_send.c
-rw-r--r-- 1 root root 2629 Aug 14 02:18 ngx_rtmp_shared.c
-rw-r--r-- 1 root root 25542 Aug 14 02:18 ngx_rtmp_stat_module.c
-rw-r--r-- 1 root root 378 Aug 14 02:18 ngx_rtmp_streams.h
-rw-r--r-- 1 root root 239 Aug 14 02:18 ngx_rtmp_version.h
-rw-r--r-- 1 root root 9087 Aug 14 02:18 README.md
-rw-r--r-- 1 root root 11765 Aug 14 02:18 stat.xsl
drwxr-xr-x 4 root root 4096 Aug 14 02:18 test
[root@VM-32-7-centos nginx-rtmp-module]# cp stat.xsl /home/live
- 创建流媒体缓存目录
mkdir -p /tmp/stream/live/8295 - 创建RTMP配置子目录方便管理
mkdir /etc/nginx/rtmp.d - 将rtmp.d目录包含到nginx配置中
vim /etc/nginx/nginx.conf
user nginx;
#user fpmuser;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_min_length 1024;
gzip_comp_level 2;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
#包含HTTP服务器配置文件 自动加载目录下所有配置文件
include /etc/nginx/conf.d/*.conf;
}
rtmp {
#包含RTMP服务器配置文件 自动加载目录下所有配置文件
include /etc/nginx/rtmp.d/*.conf;
}
- 创建RTMP流媒体配置文件
vim /etc/nginx/rtmp.d/rtmp_live.conf
server {
listen 1935; #监听端口
chunk_size 4096; #切块大小
application live {
live on; #开启直播
hls on; #开启hls
hls_path /tmp/stream/live; #rtmp推流路径
hls_fragment 5s; #TS流时长
}
}
- 创建RTMP流媒体站点页面配置文件
vim /etc/nginx/conf.d/svr_live.conf
server {
listen 80; # http 监听端口
server_name live.example.com; # 域名
# 此URL提供XML格式的RTMP统计信息
location /stat {
rtmp_stat all;
# 在浏览器中网页形式显示
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# 设置stat.xsl文件路径
root /home/live;
}
# HLS 流访问配置
location /8295 {
# HLS 流文件格式
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp/stream/live; # hls文件路径,必须是rtmp配置里hls_path指定路径
add_header Cache-Control no-cache; # 禁用缓存
}
}
4.2.2 官方详细配置参数
4.2.2.1 实用参数中文解译
rtmp {
server {
listen 1935; # 监听端口
chunk_size 4096; # 网络流的块大小
# 电视直播模式: 一个推流,多个观看
application mytv {
live on; # 开启直播模式
max_connections 1024; # 最大连接数
# 录制直播流
record all; # 录制所有 off/all/video/audio/keyframes/maual
#record maual;# 通过接口控制录制的启动停止
record_path /tmp/av; # 流文件保存路径
record_max_size 1K; # 录制文件的最大值是5120k
# 为录制的流媒体文件名自动加上时间戳 避免重名
record_unique on;
# 推流权限
# 仅允许 127.0.0.1 推流至服务器,拒绝其他任何来源
allow publish 127.0.0.1;
deny publish all;
# 播放权限
#allow play all; # 允许所有地址播放实时流
#allow play 127.0.0.1;# 仅允许本地播放
}
# 转码配置 (服务器中需要安装ffmpeg)
application big {
live on;
# 转码配置生效时,为每个已发布的直播流进行转码
# 需要指定被转码的实例名和直播流,格式 $app/$name
# 转码后发布到实例名为small(可自定义)的低画质实例中
# 转码可指定文件格式、编码格式、画面尺寸等参数,详见ffmpeg手册
exec ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32x32
-f flv rtmp://localhost:1935/small/${name};
}
# 低画质实例
application small {
live on;
# 从转码实例中接收到的低画质直播流
}
# 推流分发
application mypush {
live on;
# 自动将接收到的直播流分发到其他RTMP服务器
push rtmp1.example.com;
push rtmp2.example.com:1934;
}
# 视频点播 (Video On Demand)
application vod {
play /var/flvs;
}
# HLS 直播流 (HTTP Live Streaming)
# HLS 输入流必须是 H264/AAC 格式
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
# MPEG-DASH 直播流 (Dynamic Adaptive Streaming over HTTP)
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
}
# HTTP 访问RTMP统计数据和观看HLS直播
http {
server {
listen 80; # http 监听端口
server_name live.example.com; # 域名
# 此URL提供XML格式的RTMP统计信息
location /stat {
rtmp_stat all;
# 在浏览器访问以网页形式显示
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# 设置stat.xsl文件的完整路径(stat.xsl示例文件从nginx-rtmp-module目录复制到此路径)
root /path/to/stat.xsl/;
}
# HLS 流访问配置
location /hls {
# HLS 流文件格式
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp; # hls文件路径,必须是rtmp配置里hls_path指定路径
add_header Cache-Control no-cache; # 禁用缓存
}
# DASH 流访问配置
location /dash {
root /tmp;
add_header Cache-Control no-cache;
}
}
}
4.2.2.1 官方原版示例配置
rtmp {
server {
listen 1935;
chunk_size 4000;
# TV mode: one publisher, many subscribers
application mytv {
# enable live streaming
live on;
# record first 1K of stream
record all;
record_path /tmp/av;
record_max_size 1K;
# append current timestamp to each flv
record_unique on;
# publish only from localhost
allow publish 127.0.0.1;
deny publish all;
#allow play all;
}
# Transcoding (ffmpeg needed)
application big {
live on;
# On every pusblished stream run this command (ffmpeg)
# with substitutions: $app/${app}, $name/${name} for application & stream name.
#
# This ffmpeg call receives stream from this application &
# reduces the resolution down to 32x32. The stream is the published to
# 'small' application (see below) under the same name.
#
# ffmpeg can do anything with the stream like video/audio
# transcoding, resizing, altering container/codec params etc
#
# Multiple exec lines can be specified.
exec ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32x32
-f flv rtmp://localhost:1935/small/${name};
}
application small {
live on;
# Video with reduced resolution comes here from ffmpeg
}
application webcam {
live on;
# Stream from local webcam
exec_static ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an
-f flv rtmp://localhost:1935/webcam/mystream;
}
application mypush {
live on;
# Every stream published here
# is automatically pushed to
# these two machines
push rtmp1.example.com;
push rtmp2.example.com:1934;
}
application mypull {
live on;
# Pull all streams from remote machine
# and play locally
pull rtmp://rtmp3.example.com pageUrl=www.example.com/index.html;
}
application mystaticpull {
live on;
# Static pull is started at nginx start
pull rtmp://rtmp4.example.com pageUrl=www.example.com/index.html name=mystream static;
}
# video on demand
application vod {
play /var/flvs;
}
application vod2 {
play /var/mp4s;
}
# Many publishers, many subscribers
# no checks, no recording
application videochat {
live on;
# The following notifications receive all
# the session variables as well as
# particular call arguments in HTTP POST
# request
# Make HTTP request & use HTTP retcode
# to decide whether to allow publishing
# from this connection or not
on_publish http://localhost:8080/publish;
# Same with playing
on_play http://localhost:8080/play;
# Publish/play end (repeats on disconnect)
on_done http://localhost:8080/done;
# All above mentioned notifications receive
# standard connect() arguments as well as
# play/publish ones. If any arguments are sent
# with GET-style syntax to play & publish
# these are also included.
# Example URL:
# rtmp://localhost/myapp/mystream?a=b&c=d
# record 10 video keyframes (no audio) every 2 minutes
record keyframes;
record_path /tmp/vc;
record_max_frames 10;
record_interval 2m;
# Async notify about an flv recorded
on_record_done http://localhost:8080/record_done;
}
# HLS
# For HLS to work please create a directory in tmpfs (/tmp/hls here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
#
# Incoming stream must be in H264/AAC. For iPhones use baseline H264
# profile (see ffmpeg example).
# This example creates RTMP stream from movie ready for HLS:
#
# ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264
# -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
# -f flv rtmp://localhost:1935/hls/movie
#
# If you need to transcode live stream use 'exec' feature.
#
application hls {
live on;
hls on;
hls_path /tmp/hls;
}
# MPEG-DASH is similar to HLS
application dash {
live on;
dash on;
dash_path /tmp/dash;
}
}
}
# HTTP can be used for accessing RTMP stats
http {
server {
listen 8080;
# This URL provides RTMP statistics in XML
location /stat {
rtmp_stat all;
# Use this stylesheet to view XML as web page
# in browser
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
root /path/to/stat.xsl/;
}
location /hls {
# Serve HLS fragments
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
location /dash {
# Serve DASH fragments
root /tmp;
add_header Cache-Control no-cache;
}
}
}
4.3 重启Nginx
nginx -s reload
5. 配置防火墙
5.1 配置防火墙,放行HLS端口80,RTMP端口1935
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=1935/tcp --permanent
5.2 重启防火墙
firewall-cmd --reload
6. 推流与观看
6.1 访问推流状态页面
- 浏览器访问流媒体站点stat页面

这里live streams数量说明有几路直播流,出现这个页面,说明nginx运行正常 - 可能出现的错误 403 Forbidden
设置站点目录权限755
chmod 755 /home/live
6.2 OBS推流
6.2.1 推流地址设置
4.4 RTMP URL 书写格式
rtmp://live.example.com[:port]/app[/stream]
app - 配置中application {} 块的名称,理解为哪个电视台
stream - 串流码,理解为电视台哪个频道
port - 默认端口号1935

6.2.2 OBS推流状态

右下角绿灯常亮,kb/s:1976 有数据,说明正在以1976kb/s的码流推流中:


6.2.3 后台查看实时流媒体缓存
[root@VM-32-7-centos ~]# cd /tmp/livestream/live
[root@VM-32-7-centos live]# ll
total 2740
-rw-r--r-- 1 nginx nginx 272224 Aug 14 17:31 8295-12.ts
-rw-r--r-- 1 nginx nginx 272224 Aug 14 17:31 8295-13.ts
-rw-r--r-- 1 nginx nginx 271660 Aug 14 17:31 8295-14.ts
-rw-r--r-- 1 nginx nginx 272224 Aug 14 17:31 8295-15.ts
-rw-r--r-- 1 nginx nginx 272224 Aug 14 17:31 8295-16.ts
-rw-r--r-- 1 nginx nginx 271660 Aug 14 17:31 8295-17.ts
-rw-r--r-- 1 nginx nginx 272224 Aug 14 17:32 8295-18.ts
-rw-r--r-- 1 nginx nginx 272224 Aug 14 17:32 8295-19.ts
-rw-r--r-- 1 nginx nginx 271660 Aug 14 17:32 8295-20.ts
-rw-r--r-- 1 nginx nginx 272224 Aug 14 17:32 8295-21.ts
-rw-r--r-- 1 nginx nginx 55460 Aug 14 17:32 8295-22.ts
-rw-r--r-- 1 nginx nginx 237 Aug 14 17:32 8295.m3u8
6.3 收看直播
6.3.1 拉流设置

6.3.2 播放直播

本章节完
729

被折叠的 条评论
为什么被折叠?



