Nginx搭建flv mp4流媒体服务器[转]

环境:Centos 6.4 32bit

一、安装依赖包

1.安装zlib

wget http://zlib.net/zlib-1.2.8.tar.gz
tar xzvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install

2.安装gcc-c++

yum -y install gcc-c++

3.安装pcre

wget http://sourceforge.net/projects/pcre/files/pcre/8.36/pcre-8.36.tar.gz/download  -O pcre-8.36.tar.gz
tar zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure --prefix=/usr/local/pcre
make && make install

4.安装 openssl openssl-devel

yum -y install openssl openssl-devel

5.下载mp4支持模块备用

wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
vi nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
将如下几行注释
/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}

二、安装Nginx服务器并配置

1.安装

groupadd www
useradd -g www www
wget http://nginx.org/download/nginx-1.7.7.tar.gz
tar xzvf nginx-1.7.7.tar.gz
cd nginx-1.7.7
./configure --add-module=../nginx_mod_h264_streaming-2.2.7 --with-pcre=../pcre-8.36 --with-zlib=../zlib-1.2.8 --user=www --group=www --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable --with-http_mp4_module --with-cc-opt='-O3'
make && make install

2.验证已安装的Nginx服务器是否支持mp4、flv等视频

/usr/local/nginx/sbin/nginx -V

输出结果如下:

nginx version: nginx/1.7.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
configure arguments: --add-module=/root/nginx_mod_h264_streaming-2.2.7 --with-pcre=/root/pcre-8.36 --with-zlib=/root/zlib-1.2.8 --user=www --group=www --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable --with-http_mp4_module --with-cc-opt=-O3

3.配置

编辑 /usr/local/nginx/conf/nginx.conf 文件

下面仅显示需要修改的参数

user www;
worker_connections 65535;
gzip  on;

    server {
        listen       80;
        server_name  www.2dan.cc;

        location / {
            root   /home/html;
            index  index.html index.htm;
            limit_rate_after 10m;  #在flv视频文件下载了10M以后开始限速
            limit_rate 200k;       #速度限制为200K
        }
        location ~ \.flv {
                         flv;
        }
        location ~ \.mp4 {
                         mp4;
        }
        location ~* \.(swf|flv|mp4)$ {
            valid_referers none blocked *.2dan.cc ; //防盗链授权
              if ($invalid_referer) {
                  return 403;
             }
        }
    }

然后重启nginx

/usr/local/nginx/nginx -s reload

四、使用与测试

1.为视频文件添加关键帧,flv使用 yamdi mp4使用 qt-faststart

2.将输出的文件上传到 /home/html 目录下,并使用播放器调用以测试是否正常播放、随意拖动、边缓存边播放。

标签:Nginx, 服务器, flv, mp4, 流媒体

环境:Centos 6.4 32bit

一、安装依赖包

1.安装zlib

wget http://zlib.net/zlib-1.2.8.tar.gz
tar xzvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install

2.安装gcc-c++

yum -y install gcc-c++

3.安装pcre

wget http://sourceforge.net/projects/pcre/files/pcre/8.36/pcre-8.36.tar.gz/download  -O pcre-8.36.tar.gz
tar zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure --prefix=/usr/local/pcre
make && make install

4.安装 openssl openssl-devel

yum -y install openssl openssl-devel

5.下载mp4支持模块备用

wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
vi nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c
将如下几行注释
/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}

二、安装Nginx服务器并配置

1.安装

groupadd www
useradd -g www www
wget http://nginx.org/download/nginx-1.7.7.tar.gz
tar xzvf nginx-1.7.7.tar.gz
cd nginx-1.7.7
./configure --add-module=../nginx_mod_h264_streaming-2.2.7 --with-pcre=../pcre-8.36 --with-zlib=../zlib-1.2.8 --user=www --group=www --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable --with-http_mp4_module --with-cc-opt='-O3'
make && make install

2.验证已安装的Nginx服务器是否支持mp4、flv等视频

/usr/local/nginx/sbin/nginx -V

输出结果如下:

nginx version: nginx/1.7.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
configure arguments: --add-module=/root/nginx_mod_h264_streaming-2.2.7 --with-pcre=/root/pcre-8.36 --with-zlib=/root/zlib-1.2.8 --user=www --group=www --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable --with-http_mp4_module --with-cc-opt=-O3

3.配置

编辑 /usr/local/nginx/conf/nginx.conf 文件

下面仅显示需要修改的参数

user www;
worker_connections 65535;
gzip  on;

    server {
        listen       80;
        server_name  www.2dan.cc;

        location / {
            root   /home/html;
            index  index.html index.htm;
            limit_rate_after 10m;  #在flv视频文件下载了10M以后开始限速
            limit_rate 200k;       #速度限制为200K
        }
        location ~ \.flv {
                         flv;
        }
        location ~ \.mp4 {
                         mp4;
        }
        location ~* \.(swf|flv|mp4)$ {
            valid_referers none blocked *.2dan.cc ; //防盗链授权
              if ($invalid_referer) {
                  return 403;
             }
        }
    }

然后重启nginx

/usr/local/nginx/nginx -s reload

四、使用与测试

1.为视频文件添加关键帧,flv使用 yamdi mp4使用 qt-faststart

2.将输出的文件上传到 /home/html 目录下,并使用播放器调用以测试是否正常播放、随意拖动、边缓存边播放。