如何使用nginx进行流媒体服务器搭建呢?

互联网业务高速发展,流媒体服务器受到越来越多的应用,很多企业在研究视频点播服务器的搭建方案,使用web服务器作为视频点播服务器,那么选择Nginx如何进行流媒体服务器搭建呢?下面一起了解下;(你可能在找:十款免费的流媒体服务器软件介绍

如何使用nginx进行流媒体服务器搭建呢?

一、部署前的环境准备工作

1)检查当前系统是否已经安装zlib、pcre基础软件包

rpm –qa | grep zlib         ##Nginx运行需要的函数库

rpm –qa | grep pcre        ##与Perl兼容的正则表达式库模块

rpm –qa | grep ssh         ##openssh 支持安全的通信

2)准备安装的软体

A)nginx-1.0.5.tar.gz      ##运行主程序

B)nginx_mod_h264_streaming-2.2.7.tar.gz   ##MP4支持模块

C)nginx-accesskey-2.0.3.diff.bz2            ##资源防盗链支持模块

  ##wget  http://wiki.nginx.org/images/5/51/Nginx-accesskey-2.0.3.tar.gz

D)yamdi-1.4.tar.gz                     

 ##渐进式流支持模块(抓取视频资源关键帧实现播放时的随意拖动效果)

E)准备一个播放器

http://blogimg.chinaunix.net/blog/upfile2/100607142612.rar ##flash播放器控件

F)测试资源准备

二、部署步骤

A)安装yamdi

1、解压下载的文件tar –zxvf yamdi-1.8.tar.gz

2、进入解压后的目录cd yamdi-1.8.

3、编译并安装 make && make install

4、使用该软件为视频添加关键帧信息实现拖动效果

具体使用方法如下yamdi -i input.mp4 -o out.mp4 (拖拽功能必须的一步呀)

B)安装Nginx

1、访问http://nginx.org/download/官网下载最新版本的Nginx程序

wget http://nginx.org/download/nginx-1.3.3.tar.gz

2、访问http://h264.code-shop.com官网下载最新版本的MP4支持模块

wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz

3、访问http://sourceforge.net官网下载支持流媒体拖动功能模块

wget http://sourceforge.net/projects/yamdi/files/yamdi/1.8/yamdi-1.8.tar.gz/download

./configure --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_ssl_module --with-pcre --with-zlib --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module 

4、下载pcre包wget http://autosetup1.googlecode.com/files/pcre-7.9.tar.gz

5、下载zlib包wget http://google-desktop-for-linux-mirror.googlecode.com/files/zlib-1.2.3.tar.gz

6、解压文件tar –zxvf pcre-7.9.tar.gz

7、cd pcre-7.9 配置编译环境./configure –prefix=/usr/local/pcre

8、安装程序make && make install

9、安装部署nginx软体 【Linux公社 http://www.linuxidc.com

./configure  --add-module=/nginx/nginx_mod_h264_streaming-2.2.7  --with-pcre=/nginx/pcre-7.9 --with-zlib=/nginx/zlib/1.2.3  --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable  --with-http_mp4_module  --add-module=/nginx/nginx-accesskey-2.0.3  --with-cc-opt='-O3

提示以下错误信息:

make -f objs/Makefile

make[1]: Entering directory `/nginx/nginx-1.3.3'

cd /usr/local/pcre/ \

        && if [ -f Makefile ]; then make distclean; fi \

        && CC="gcc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \

        ./configure --disable-shared

/bin/sh: line 2: ./configure: No such file or directory

make[1]: *** [/usr/local/pcre//Makefile] Error 127

make[1]: Leaving directory `/nginx/nginx-1.3.3'

make: *** [build] Error 2

导致发生该错误的原因是依赖程序路径应指定到源码包而非安装后的程序包。

所以在配置编译环境是应这样写

./configure  --add-module=/nginx/nginx_mod_h264_streaming-2.2.7  --with-pcre=/nginx/pcre-7.9 --with-zlib=/nginx/zlib/1.2.3  --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable  --with-http_mp4_module  --add-module=/nginx/nginx-accesskey-2.0.3  --with-cc-opt='-O3'(正确的指令)

Ok!通过查看返回信息一切正常!

10、继续编译该软体使用make命令,哈哈报错啦!(有error信息打印不见的是一件坏事哦)报错信息如下:

make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1

make: *** [build] Error 2

解决方法:

进入支持MP4格式播放的库/nginx/nginx_mod_h264_streaming-2.2.7/src修改ngx_http_h264_streaming_module.c该文件。修改内容如下所示:

将如下几行注释
/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}后我们再次make clean && make一下nginx,呵呵这次终于成功了。

11、make install 安装完成

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

cd  /usr/local/nginx/sbin

nginx –V

configure arguments:

--add-module=/nginx/nginx_mod_h264_streaming-2.2.7 --with-pcre=/nginx/pcre-7.9 --with-zlib=/nginx/zlib/1.2.3 --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl=/nginx/openssl-0.9.3 --with-http_mp4_module --with-cc-opt='-O3'

13、配置Nginx配置文件

cd  /usr/local/nginx/conf/

vi nginx.conf   ##修改配置文件

user  videoapp video;  ##管理用户

worker_processes 8;    ##后台进程

error_log  /usr/local/nginx/logs/error.log;

##nginx错误日志存放路径

pid        /usr/local/nginx/logs/nginx.pid;

events {

        use epoll;

##轮训方式

        worker_connections      65535;

##允许的最大连接数

        }

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  /usr/local/nginx/logs/access.log;

 

    sendfile        on;

    tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    gzip  on;

 

    server {

        listen       801;

        server_name  localhost;

        root /usr/local/nginx/html ;

        #charset koi8-r;

        limit_rate_after 5m;

        limit_rate  512k;

         charset utf-8;

        access_log  /usr/local/nginx/logs/host.access.log  main;

 

        location / {

            root   html;

            index  index.html index.htm;

           # limit_rate_after 5m;

           # limit_rate  512k;

        }

        #error_page  404              /404.html;

       location ~ \.flv$ {

                         flv;

                        }

       location ~ \.mp4$ {

                         mp4;

                        }

       location ~(favicon.ico) {

                        log_not_found off;

                        expires 30d;

                        break;

                               }

14、拷贝视频及falsh播放器到video目录下

15、拖动播放测试http://127.0.0.1:801/video/player.swf?type=http&file=2.flv

         http://127.0.0.1:801/player.swf?type=http&file=xxy.mp4

三、后续完善的工作

1、为节省带宽防止用户直接下载视频我们需要做如下设置(视频防盗链):

1、到http://wiki.codemongers.com/NginxHttpAccessKeyModule去下载防盗链模块,没办法总有人想下载。还是要做好防盗链工作的。
需要注意的是,下载完防盗链模块之后需要修改下配置文件讲config文件中的“$HTTP_ACCESSKEY_MODULE” 改成“ngx_http_accesskey_module”,不改的话没办法开启防盗链模块。
2、执行编译:./configure  --add-module=/nginx/nginx_mod_h264_streaming-2.2.7  --with-pcre=/nginx/pcre-7.9 --with-zlib=/nginx/zlib/1.2.3  --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable  --with-http_mp4_module  --add-module=/nginx/nginx-accesskey-2.0.3  --with-cc-opt='-O3

3、make && make install 安装完毕 其他配置参数同上13步骤的配置

4、最后开启防盗链功能
location ~ \.mp4$ {
mp4;
limit_conn one 2;
limit_rate 200k;

accesskey on;
accesskey_hashmethod md5;
accesskey_arg “key”;
accesskey_signature “movie.weiqp.cn$remote_addr”;

 

您可能还会对下面的文章感兴趣:

COPYRIGHT © 2010-2018 广东木准科技有限公司 All Rights Reserved.

粤ICP备2022104150号
客服中心 customer service center

机房合作咨询 (业务销售请加微信:18300003210 二十四小时在线)

售后服务 (网维技术QQ:404634181 投诉电话:183-0000-3210)

工作时间: 周一至周五 9:30至17:30 周六13:30至17:30,其它日期按国家法定节假日休假,如果有不便之请敬请谅解! 售后技术支持:多部门7*24小时机制。

客户投诉留言

电话和QQ,以便我们为您提供优质服务! *为必填项