Nginx,nginx-rtmp-module-master搭建直播平台

Nginx,nginx-rtmp-module-master搭建直播平台

提示:首先查看最近最近的服务器有没有开启防火墙


前言

提示:后续会更加完善加入:nginx-http-flv-module、ffmpeg-4.2.3、openssl-1.0.2l、pcre-8.38、zlib-1.2.11


提示:以下是本篇文章正文内容,下面案例可供参考

一、环境的搭建

1、防火墙

# 关闭防火墙
systemctl stop firewalld

# 禁用防火墙
systemctl disable firewalld

#查看防火墙的状态
systemctl status firewalld

2、安装相关依赖

# 安装wget
yum -y install wget

# 安装 gcc gcc-c++
yum -y install gcc gcc-c++

# 安装PCRE库
cd /usr/local/
wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
tar -zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install

# 安装openssl
cd /usr/local/ 
wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz 
tar -zxvf openssl-1.0.1j.tar.gz 
cd openssl-1.0.1j
./config
make && make install

### 如果提示You need Perl 5,则输入下面这个命令。 ###
yum -y install Perl 5

# 安装zlib
/**
*注意如果一下地址出现404,
*可以到网上找一个zlib相关的包然后用Xftp传入到服务器中
*/

cd /usr/local/
wget http://zlib.net/zlib-1.2.11.tar.gz
如果上面地址出现404,那就试一下下面这个链接
wget https://zlib.net/fossils/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11.tar.gz
./configure
make && make install

# git clone
cd /usr/local/
yum -y install git
git clone https://github.com/arut/nginx-rtmp-module.git 

# 安装nginx
cd /usr/local/
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
./configure --prefix=/usr/local/src/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module  --add-module=../nginx-rtmp-module  --with-openssl=<path> --with-http_ssl_module
make && make install 

###如果提示###
./configure: error: SSL modules require the OpenSSL library.
#执行
yum -y install openssl openssl-devel
#如果提示
yum -y install pcre-devel

#nginx相关命令
/usr/local/src/nginx/sbin/nginx
#如果启动不了,进入到/usr/local/src/nginx/sbin文件中用如下命令
./nginx    : 启动nginx

./nginx -s stop : 停止nginx

./nginx -s reload : 重启nginx

二、配置nginx.conf 的rtmp


user root;
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;
}
#流媒体
rtmp {
	server {
		listen 1935; #监听的端口
		chunk_size 4000;
		drop_idle_publisher 10s; #指定的时间内丢弃空闲的publisher
		application rtmplive {#rtmp推流请求路径 (切记路径错了会推不上流)
			record off;
			live on; #开启实时
			hls on; #开启hls
			hls_path /usr/local/nginx/hegui; #rtmp推流请求路径,文件存放路径
			hls_continuous on; #切片编号从上一次结束开始
			hls_fragment 5s; #每个TS文件包含5秒的视频内容
		}
	}
    }

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;
        listen	    [::]:80 ipv6only=on; #开启TCP6,根据个人,可要可不要
        server_name  localhost;

      location  /rtmplive {
            types {
	      application/vnd.apple.mpegurl m3u8;
                         video/mp2t ts;
	}
	root    /usr/local/nginx/hegui;
	chunked_transfer_encoding  on;
	add_header 'Access-Control-Allow-Credentials' 'true';
	add_header 'Access-Control-Allow-Origin' '*';
	add_header Access-Control-Allow-Headers X-Requested-With;
	add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
                  add_header 'Cache-Control' 'no-cache';
	#ngx_fastdfs_module;
	#add_header Cache-Control no-cache;
	#expires -1;
        }

     #   location / {
      #      root   html;
      #      index  index.html index.htm;
      #  }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }



    #项目
    server {
        listen       163;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	#vue项目的打包后的dist
        root         /www/test/dist/;
        location /{
                   try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
                   index  index.html index.htm;
               }

        #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
        #因此需要rewrite到index.html中,然后交给路由在处理请求资源
        location @router {
                   rewrite ^.*$ /index.html last;
        }


     location /api {
            proxy_pass http://*.*.*.*:9001/;
            #proxy_pass http://&env;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header REMOTE-PORT $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            rewrite ^/api/(.*) /$1 break;
	    proxy_set_header Upgrade $http_upgrade;
	    proxy_set_header Connection "upgrade";
        }  


	#nacos配置
        location /nacos/ {
            rewrite  ^//(.*)$ /$1 break;
            proxy_pass http://*.*.*.*:8080/nacos;
            proxy_set_header Host $host;
            proxy_set_header User-Agent $http_user_agent;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header authorization $http_authorization;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

下载一个OBS进行推流

# 推流地址
rtmp://*.*.*.*:1935/就是下面的application 后面跟的名字/你推流的文件名(不要后缀)

# 下载一个VLC 进行拉流  拉流地址
rtmp://*.*.*.*:1935/就是下面的application 后面跟的名字/你推流的文件名


#流媒体
rtmp {
	server {
		listen 1935; #监听的端口
		chunk_size 4000;
		drop_idle_publisher 10s; #指定的时间内丢弃空闲的publisher
		application rtmplive { #rtmp推流请求路径 (切记路径错了会推不上流)
			record off;
			live on; #开启实时
			hls on; #开启hls
			hls_path /usr/local/nginx/hegui; #rtmp推流请求路径,文件存放路径
			hls_continuous on; #切片编号从上一次结束开始
			hls_fragment 5s; #每个TS文件包含5秒的视频内容
		}
	}
    }

自己测试了,是可以的,有问题评论,或者私信!

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值