一.环境
服务器操作系统:CentOS Linux release 8.2.2004 (Core)
nginx 版本: https://nginx.org/download/nginx-1.18.0.tar.gz
RMTP模块:https://github.com/arut/nginx-rtmp-module
测试主机:ArchLinux
推流工具:OBS-Studio
拉流工具:VLC
二.准备编译环境
1.安装依赖
也可以直接跳过,在下面配置和编译的时候提示缺什么再安装什么
sudo dnf install gcc make pcre pcre-devel openssl openssl-devel
2. 下载nginx-1.18.0和rtmp模块
#####个人习惯,弄个临时文件夹存放#####
mkdir tmp
cd tmp
wget https://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
git clone https://github.com/arut/nginx-rtmp-module
三.编译安装
1.配置
###注意nginx源码文件夹和rtmp模块源码文件夹在同一目录下#########
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
2.编译
make
执行make过程中会报如下错
-o objs/addon/nginx-rtmp-module/ngx_rtmp_eval.o \
../nginx-rtmp-module/ngx_rtmp_eval.c
../nginx-rtmp-module/ngx_rtmp_eval.c: 在函数‘ngx_rtmp_eval’中:
../nginx-rtmp-module/ngx_rtmp_eval.c:160:17: 错误:this statement may fall through [-Werror=implicit-fallthrough=]
switch (c) {
^~~~~~
../nginx-rtmp-module/ngx_rtmp_eval.c:170:13: 附注:here
case ESCAPE:
^~~~
cc1:所有的警告都被当作是错误
make[1]: *** [objs/Makefile:1349:objs/addon/nginx-rtmp-module/ngx_rtmp_eval.o] 错误 1
make[1]: 离开目录“/home/centos/tmp/nginx-1.18.0”
make: *** [Makefile:8:build] 错误 2
原因是Makefile里有Werror选项,语法要求严格,将所有警告当作错误处理啦
解决方法
###位于nginx源码目录下#####
nano objs/Makefile
打开后内容如下
CC = cc
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g
CPP = cc -E
LINK = $(CC)
ALL_INCS = -I src/core \
-I src/event \
-I src/event/modules \
-I src/os/unix \
-I ../nginx-rtmp-module \
-I objs \
-I src/http \
-I src/http/modules
##################################
#######后面内容生略################
将-Werror删除
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -g
ctrl+x,然后输入y保存退出 后再执行make
3.安装
sudo make install
4.查看安装结果
/usr/local/nginx/sbin/nginx -v
输出版本号为
nginx version: nginx/1.18.0
安装成功
四.配置nginx
1.设置nginx开机启动
(1)创建nginx服务文件
sudo nano /usr/lib/systemd/system/nginx.service
创建nginx服务文件,输入以下内容
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
(2)启用nginx服务
sudo systemctl start nginx
sudo systemctl enable nginx
2.修改配置文件
这是安装完nginx后的默认配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
执行
sudo nano /usr/local/nginx/conf/nginx.conf
修改配置文件
在配置文件头部加入
rtmp_auto_push on;
rtmp {
server {
listen 1935;
application live {
live on;
hls on;
hls_fragment 3s;
hls_playlist_length 10s;
hls_path /usr/local/nginx/html/hls;
}
}
}
修改后配置文件内容如下
rtmp_auto_push on;
rtmp {
server {
listen 1935;
application live {
live on;
hls on;
hls_fragment 3s;
hls_playlist_length 10s;
hls_path /usr/local/nginx/html/hls;
}
}
}
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
3.重启nginx,开启防火墙端口
(1)重启ngxin服务
sudo systemctl restart nginx
(2)开启1935端口
sudo firewall-cmd --add-port=1935/tcp --permanent
sudo firewall-cmd --reload
4.测试
(1)设置OBS-Studio的推流地址
rtmp://192.168.1.116:1935/live/
串流密钥随便写,拉流的时候要对应
其中ip为我的CentOS虚拟机的ip地址,将ip改为自己的服务器地址,ip后面的端口号和live是在nginx配置文件里设置的,与配置文件对应
设置好后在OBS-Studio里添加一个屏幕捕获,然后开始推流.
(2)VLC拉流测试
与nginx配置文件对应的rtmp流和hls流地址
rtmp://192.168.1.116:1935/live/demo
http://192.168.1.116/hls/demo.m3u8