使用Nginx实现简单的RTMP推流直播笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37674858/article/details/88822445

实现结果

推流的实现:使用OBS Studio进行推流
拉流播放的实现:使用VLC进行拉流地址播放
直播服务器实现:centos系统下的nginx+RTMP模块

实现步骤:

第1步:nginx相关依赖包的安装

nginx相关依赖包的安装:

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++   openssl-devel cmakepcre-develnanowget  gcc gcc-c++ ncurses-devel perl

其他需要用到的依赖包:

 yum -y install git
yum -y install nano
yum -y install wget

第2步:nginx-rtmp-module

nginx-rtmp-module,官方github地址

// 通过git clone 的方式下载到服务器上
[root~]# git clone https://github.com/arut/nginx-rtmp-module.git

第3步:安装Nginx服务器

1.下载Nginx解压,并安装rtmp和openssl的模块
PS:add-module=/data/bak/nginx-rtmp-module 中的/data/bak是自己的下载的相关模块的存放路径

[root~]# wget http://nginx.org/download/nginx-1.10.3.tar.gz 
[root~]# tar -zxvf nginx-1.10.3.tar.gz 
[root~]# cd nginx-1.10.3
//添加rtmp和openssl支持
[root~]# ./configure --add-module=/data/bak/nginx-rtmp-module --with-http_ssl_module
[root~]# make && make install

第4步:查看安装模块信息:

[root@localhost nginx-1.10.3]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
[root@localhost nginx-1.10.3]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --add-module=/data/bak/nginx-rtmp-module --with-http_ssl_module

第5步:启动nginx,查看启动的情况

[root@localhost nginx-1.10.3]# nginx -s stop
[root@localhost nginx-1.10.3]# nginx
[root@localhost nginx-1.10.3]# curl http://192.168.74.128/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost nginx-1.10.3]# 

第6步:配置相关的模块信息修改nginx.conf

PS:注意是在http{}外进行添加:

rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
        }
        application vod {
        play /opt/video/vod;
        }

    }
}

完成的配置文件如下:


#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       1980;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
           root /home/developer/zhoucl/obs/nginx-rtmp-module/;
        }


        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;
    #    }
    #}

}
rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
            hls on;
            hls_path /usr/local/nginx/data/hls;

         }
        application video {
        play /opt/video;
        }
    }
}

第7步:关闭防火墙,为方便测试

ps:一般线上环境是不会直接关闭防火墙,而是开放对应的端口。写对应的规则!

systemctl stop firewalld

第8步:重启的启动nginx,并用浏览器打开:http://192.168.74.128/stat

[root@localhost nginx-1.10.3]# nginx -s reload
[root@localhost nginx-1.10.3]# curl http://192.168.74.128/stat
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="stat.xsl" ?>
<rtmp>
<nginx_version>1.10.3</nginx_version>
<nginx_rtmp_version>1.1.4</nginx_rtmp_version>
<compiler>gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) </compiler>
<built>Feb 26 2018 21:48:59</built>
<pid>29574</pid>
<uptime>608</uptime>
<naccepted>0</naccepted>
<bw_in>0</bw_in>
<bytes_in>0</bytes_in>
<bw_out>0</bw_out>
<bytes_out>0</bytes_out>
<server>
<application>
<name>rtmplive</name>
<live>
<nclients>0</nclients>
</live>
</application>
<application>
<name>vod</name>
<play>
<nclients>0</nclients>
</play>
</application>
</server>
</rtmp>

PS:说明启动成功

<name>rtmplive</name>
<live>
<nclients>0</nclients>
</live>
</application>
<application>
<name>vod</name>

第9步:使用OBS Studio进行推流

首先在PC端安装一下OBS Studio :https://obsproject.com/download#mp
启动后的软件示意图:

点击设置,设置推送地址流服务器 

点击开始推送:

说明连接服务器成功:

如果关闭了nginx,则会提示:

第10步:使用VLC播放器进行播放拉流地址


 

猜你喜欢

转载自blog.csdn.net/qq_37674858/article/details/88822445