nginx+nginx-http-flv-module

简介: nginx+nginx-http-flv-module

nginx+nginx-http-flv-module(推荐主流)

FLV文件 就是由“ FLV Header + RTMP数据 ”构成的。  

FLV与 RTMP之间的关 系秘密,即 FLV 是在 RTMP 数据之上加了一层“⻢甲”

测试网页http://bilibili.github.io/flv.js/demo/

模块地址

通过服务端将其 RTMP 流实时转为 http-flv 流,从而浏览器可直接使用该流进行直播(使用bilibili提供的 flv.js )。

所需资料集(以及下载地址):

  1. nginx
  2. nginx-http-flv-module
  3. ffmpeg
  4. flv.js

linux实现

linux实现步骤

  1. 下载需要的压缩包并解压
  2. 在nginx的文件夹下配置环境并编译
  3. 在编译后的nginx目录下修改conf/nginx.conf的配置
  4. 在编译后的nginx目录下执行sbin/nginx启动服务器
  5. 推流
  6. 测试

提前准备依赖

yum -y install unzip
yum -y install gcc-c++ 
yum -y install pcre pcre-devel  
yum -y install zlib zlib-devel 
yum -y install openssl openssl-devel

不准备就是等报错了根据提示去安装所需的

nginx+flv编译

这里是nginx+flv

nginx和flv的版本号要贯穿整个配置,如果修改了版本,记得文件夹和其他配置的地方也要同步

cd /opt
mkdir nginx
cd nginx
git clone https://github.com/winshining/nginx-http-flv-module.git
wget http://nginx.org/download/nginx-1.24.0.tar.gz
[root@iZbp1c400avts4bhhmibppZ nginx]# ls
nginx-1.24.0.tar.gz  nginx-http-flv-module

解压

tar -zxvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 其他环境根据自己的需要去添加
./configure --prefix=/usr/local/nginx --add-module=../nginx-http-flv-module

下面的configuration就是配置路径

编译

make && make install

第一次启动

cd /usr/local/nginx
sbin/nginx

访问ip:80端口来判断是否成功,因为nginx,service可能会有未卸载干净的,出现此界面表示nginx成功启动

修改nginx.conf文件

修改编译后的nginx文件夹的conf下的nginx.conf文件

cd /usr/local/nginx
vim conf/nginx.conf

新增两部分

rtmp {
    server {
        listen 1935;
        application myapp {
            live on;
        }
    }
}
location /live {
            flv_live on;
        }
        location /flv {
            add_header 'Access-Control-Allow-Origin' '*';
            flv_live on;
            chunked_transfer_encoding on;
        }

为了解决该跨域问题

添加add_header 'Access-Control-Allow-Origin' '*';

location / {
            add_header 'Access-Control-Allow-Origin' '*';
            root   html;
            index  index.html index.htm;
        }

完整配置

#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;
        application myapp {
            live on;
        }
    }
}
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 / {
            add_header 'Access-Control-Allow-Origin' '*';
            root   html;
            index  index.html index.htm;
        }
        location /live {
            flv_live on;
        }
        location /flv {
            add_header 'Access-Control-Allow-Origin' '*';
            flv_live on;
            chunked_transfer_encoding on;
        }
        #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;
    #    }
    #}
}

重启服务器

sbin/nginx -s reload

推流

播放本地视频

ffmpeg -re -i ".\test.mp4" -c copy -f flv "rtmp://ip:1935/myapp/test"

播放测试flv

http://ip/flv?port=1935&app=myapp&stream=test

这里的参数是和nginx.conf相对应,test是推流时选择的steam流标识

播放测试rtmp

rtmp://ip:1935/myapp/test

目录
相关文章
|
3月前
|
应用服务中间件 nginx
百度搜索:蓝易云【利用nginx内置ngx_http_mirror_module模块实现流量复制及流量放大】
以上就是使用Nginx内置 `ngx_http_mirror_module`模块实现流量复制和流量放大的简要示例。通过合理配置和利用该模块,可以实现更复杂的流量控制和调试需求。
91 1
|
3月前
|
应用服务中间件 nginx
百度搜索:蓝易云【HTTP请求是如何关联Nginx server{}块的?】
总结来说,Nginx中的 `server{}`块用于关联HTTP请求和虚拟主机,通过配置不同的 `server{}`块,可以实现多个域名或IP地址的请求分发和处理。这样,Nginx可以根据不同的请求来提供不同的服务和内容。
55 0
|
3月前
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
8天前
|
缓存 应用服务中间件 API
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(三)
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(三)
15 3
|
8天前
|
缓存 安全 应用服务中间件
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(二)
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(二)
14 1
|
8天前
|
运维 算法 应用服务中间件
运维系列.Nginx中使用HTTP压缩功能(一)
运维系列.Nginx中使用HTTP压缩功能(一)
13 1
|
12天前
|
缓存 应用服务中间件 nginx
[nginx]定制http头信息
[nginx]定制http头信息
|
8天前
|
负载均衡 应用服务中间件 网络安全
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(一)
Nginx七层(应用层)反向代理:HTTP反向代理proxy_pass篇(一)
17 0
|
8天前
|
缓存 应用服务中间件 nginx
运维系列.Nginx中使用HTTP压缩功能(二)
运维系列.Nginx中使用HTTP压缩功能(二)
14 0
|
3月前
|
缓存 负载均衡 应用服务中间件
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
130 1
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish