前言
一句唠叨,工欲善其事,必先利其器,在程序员的工作里,搭建各种环境往往花费过多不必要的时间,这里建议搭建服务端环境时,尽量避开win、macos这种系统,个人比较推崇centos。
操作
下面以centos环境为例(macos安装nginx运气不好会让人崩溃)。
安装nginx及nrm模块
请提前确保已经安装gcc、g++、zlib、pcre、openssl(如果编译nginx过程中仍显示缺少已安装过的库,可以在下方给我留言一起探讨问题)。
cd /usr/local
wget http://nginx.org/download/nginx-1.12.2.tar.gz #下载nginx
tar -xzvf nginx-1.12.2.tar.gz #解压nginx
wget https://codeload.github.com/arut/nginx-rtmp-module/legacy.tar.gz/master #下载nginx-rtmp-module
tar -xzvf arut-nginx-rtmp-module-v1.2.1-0-g791b613.tar.gz #解压nginx-rtmp-module
mv ./arut-nginx-rtmp-module-v1.2.1-0-g791b613 ./NRM #改个名
./configure --add-module=/usr/local/NRM --prefix=/usr/local/nginx --with-debug
make
make install #至此nginx安装完成
/usr/local/nginx/sbin/nginx #启动nginx
/usr/local/nginx/sbin/nginx -V #查看nginx安装模块信息和版本号
配置nginx.conf
#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;
}
######################ADD RTMP################
rtmp { #RTMP服务
server {
listen 1935; #//服务端口
chunk_size 4096; #//数据传输块的大小
application vod {
play /opt/video/vod; #//视频文件存放位置,自定义
}
application live { #第一处添加的直播字段
live on;
}
application push{
live on; #开启直播
push rtmp://<自己公网ip,没有可以填localhost本地玩一下>/live; #推流到上面的直播应用
}
}
}
#####################ADD RTMP################
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;
#################################################
location /stat { #第二处添加的location字段。
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl { #第二处添加的location字段。
root /usr/local/NRM/; #一定要填对
}
##################################################
#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;
# }
#}
}
完成以上步骤后,保存,执行nginx -s reload重新加载。
在
推流端配置
下载并安装obs,点击下图设置(这里我觉得该有个马赛克!!)
并填入如下url(换成自己的ip即可)
点击开始推流,进行推流,可以看到
拉流端
这时候可以选用任意一款支持rtmp的播放器,填入rtmp://
总结
这里的架构虽然比较简单,推流、拉流,中间通过nginx转发,却也给直播入门提供一个清晰的感官上的体验。HAVE FUN!