前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx服务器添加rtmp模块支持视频推流

nginx服务器添加rtmp模块支持视频推流

原创
作者头像
请叫我师哥
修改2021-10-25 10:46:54
3.9K3
修改2021-10-25 10:46:54
举报
文章被收录于专栏:师哥分享

注:Nginx服务器默认安装完成,只添加nginx-rtmp-module模块和nginx-http-flv-module模块,如果没有安装nginx服务,请参考 nginx安装

下载nginx所需要的模块下载地址

所有准备工作做完之后开始安装

第一步:将nginx模块解压缩后,上传到服务器,记住这个路径

第二步:通过命令进入到nginx资源目录下,就是后缀为.tar.gz解压出来的文件

第三步:执行命令

代码语言:txt
复制
./configure --add-module=/opt/rh/nginx-rtmp-module 

make && make install 

./configure --add-module=/opt/rh/nginx-http-flv-module 

make && make install 
# 注:这里我试过两个模块一起编译,但是在安装的时候出错,两个模块分开后就没有这个问题,具体情况我也没有深入研究。。将就用吧,麻烦一下子

执行完之后,会看到下面的内容,添加了rtmp和http-flv-live模块.

第四步:启动nginx并修改文件

代码语言:txt
复制
[root@home nginx-1.18.0]# whereis nginx
nginx: /usr/local/nginx
[root@home nginx-1.18.0]#  /usr/local/nginx/sbin/nginx

看到下面图片就说明nginx启动成功了。

编辑配置文件

代码语言:txt
复制
[root@home nginx-1.18.0]# vi /usr/local/nginx/conf/nginx.conf
#添加下面内容

rtmp {

    server {
        listen 1935;
        chunk_size 4000;
        
		application live {
			live on;
			
			hls on;
            hls_path  /usr/local/nginx/html/live;
            hls_fragment 2s; #每个TS文件包含10秒的视频内容
		}
    }
}
 # 这里是支持http拉流的配置
	server {
		listen       82;
		server_name  rtmpserver;
		
		location /test {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Credentials' 'true';
			
			types {
				  application/vnd.apple.mpegurl m3u8;
				  video/mp2t ts;
			}
			alias /usr/local/nginx/html/live;  # 这里的地址与上面rtmp的hls_path一致
			expires -1;
			add_header 'Cache-Control' 'no-cache'; 
		}	
					
	}

完整config内容

代码语言:txt
复制
#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;
}

rtmp {

    server {
        listen 1935;
        chunk_size 4000;
        
		application live {
			live on;
			
			hls on;
            hls_path  /usr/local/nginx/html/live;
            hls_fragment 2s; #每个TS文件包含10秒的视频内容
		}
    }
}

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       82;
		server_name  rtmpserver;
		
		location /test {
			add_header 'Access-Control-Allow-Origin' '*';
			add_header 'Access-Control-Allow-Credentials' 'true';
			
			types {
				  application/vnd.apple.mpegurl m3u8;
				  video/mp2t ts;
			}
			alias /usr/local/nginx/html/live; 
			expires -1;
			add_header 'Cache-Control' 'no-cache'; 
		}	
					
	}

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

}

第五步:重启nginx服务器

代码语言:txt
复制
[root@home nginx-1.18.0]#  /usr/local/nginx/sbin/nginx -s stop
[root@home nginx-1.18.0]#  /usr/local/nginx/sbin/nginx

重启完成后,会在配置的地址下面看到多了一个文件夹

第六步:视频推流

通过obs将视频推到服务器上面

推流成功之后会看到这样一些文件

第七步:测试拉流

http拉流地址:http://192.168.0.114:82/test/mq.m3u8

rtmp拉流地址:rtmp://192.168.0.114:1935/live/mq

注:如果测试不成功,请查看一下防火墙状态,如果没有放行端口,请关闭防火墙在重试

代码语言:txt
复制
systectl status firewalld
systectl stop firewalld

# 开启防火墙
systectl start firewalld 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档