使用 openresty/nginx 搭建mp4视频服务器,openrestynginx


首先,环境中必然要有gcc-c++环境

yum -y install gcc-c++

1 使用openresty

这里使用的是1.11.2.1版本的openresty和1.0.2版本的openssl

yum install readline-devel pcre-devel openssl-devel gcc
tar -zxvf openresty-1.11.2.1.tar.gz
tar -zxvf openssl-1.0.2h.tar.gz
cd openresty-1.11.2.1
./configure --prefix=/app/openresty --user=xxx --group=xxx --with-http_v2_module --with-openssl=/home/appdeploy/nginx/openssl-1.0.2h --with-select_module --with-poll_module --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_sub_module --with-http_stub_status_module --with-http_perl_module --with-http_mp4_module --with-http_flv_module 

可以在这里设置user和group,也可以稍后使用下面的命令进行设置

chown -R [user]:[group] 文件夹名

上面的命令执行之后,编译并安装openrestry,安装目录就是配置中指定的/app/openrestry

make && make install

如果安装的时候没有权限,可以用su切到root,注意安装之后的openrestry目录的权限即可。
此时openrestry已经安装好,到安装目录中修改openrestry下的nginx文件夹下的nginx.conf配置文件

worker_processes 1;       #工作进程数,一般设置为1就可以了
#error_log  /usr/local/nginx/logs/error.log  crit;
#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 $bytes_sent '
                                                '"$http_referer" "$http_user_agent" '
                                                '"$gzip_ratio"';
        keepalive_timeout  60;
        server_names_hash_bucket_size  128;
        client_header_buffer_size    32k;

        large_client_header_buffers  4 32k;

        access_log off;
        gzip on;
        gzip_min_length  1100;
        gzip_buffers     4 8k;
        gzip_types       text/plain;

        output_buffers   1 32k;
        postpone_output  1460;

        client_header_timeout  3m;
        client_body_timeout    3m;
        send_timeout           3m;

        sendfile                on;
        tcp_nopush              on;
        tcp_nodelay             on;

    server {
           listen 8080;
           server_name  10.202.94.16;
           root    /app/openresty/nginx/html/;
           limit_rate_after 30m;    
           limit_rate 700k;            #这里根据需要设置,意思是视频缓冲30M之后,限速为700k/s

           index   index.html;
           charset utf-8;
           location ~ \.flv$ {
              flv;
           }

           location ~ \.mp4$ {
              mp4;
           }
           error_page   500 502 503 504  /50x.html;
           location = /50x.html {
               root   html;
           }

    }
}

修改之后,启动nginx服务器

/app/openresty/nginx/sbin/nginx -c /app/openresty/nginx/conf/nginx.conf

将mp4文件放到/app/openresty/nginx/html/目录下
在浏览器上访问http://10.202.94.16:8080/xxx.mp4即可。

2 使用nginx

建议采用nginx 1.1.3版本之后的nginx,默认支持mp4,就无需再安装一堆繁琐的插件。这里使用的是1.3.14版本。

tar -zxvf nginx-1.3.14.tar.gz 
cd nginx-1.3.14 
./configure --prefix=/app/nginx --user=xxx --group=xxx --with-select_module --with-poll_module --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_sub_module --with-http_stub_status_module --with-http_perl_module --with-http_mp4_module --with-http_flv_module 
make&& make install

然后同样的,去/app/nginx做和openrestry中的nginx一样的修改即可,注意修改目录。

效果如下所示,此时的视频是横跨整个屏幕的,如果想要修改,比如做页面的内嵌视频,可以把视频放到html5页面中,再通过nginx服务器访问html文件即可。有个开源的video.js很好用,这里就不再赘述了。

说明
如有转载,请在开头注明出处:
http://blog.csdn.net/antony9118/article/details/60965453

相关内容

    暂无相关文章