Nginx搭建视频点播服务器(仿真专业流媒体软件)

来源:互联网 发布:手机时间不与网络同步 编辑:程序博客网 时间:2024/05/01 21:11

最近研究视频点播服务器的搭建方案,因项目原因笔者只能忍痛割爱舍弃专业的流媒体软件HelixServerRED5WMS等专业的流媒体软件当然其中还有不要钱的Darwin。按照坑爹的要求使用web服务器作为视频点播服务器。

经过前期调研发现选择Nginx也不失为一种理想的替代方案,可是网络上的资料大多不够完整和详尽且没有给出在部署过程中产生的错误相应的解决方法,所以笔者只好自己亲自动手整理和编写了一篇完整版安装手记,以供大家借鉴和参考。

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

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

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

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

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

2)准备安装的软体

Anginx-1.0.5.tar.gz      ##运行主程序

Bnginx_mod_h264_streaming-2.2.7.tar.gz   ##MP4支持模块

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

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

  Dyamdi-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、下载pcrewgethttp://autosetup1.googlecode.com/files/pcre-7.9.tar.gz

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

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

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

8、安装程序make && make install

9、安装部署nginx软体

./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,呵呵这次终于成功了。

11make install安装完成

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

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

3make && 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”;


原创粉丝点击