nginx增加flv、mp4流媒体拖放和搭建rtmp协议的流媒体服务器,同时增加深度防盗链功能

本文的编译环境以lnmp1.0为例:

1、登录SSH,下载如下模块:

wget -c https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/ostube/Nginx-accesskey-2.0.3.tar.gz
wget -c https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/ostube/nginx_mod_h264_streaming-2.2.7.tar.gz
wget -c http://db.ci/wp-content/uploads/2013/11/nginx-rtmp-module.tar.gz

2、解压

tar zxvf Nginx-accesskey-2.0.3.tar.gz
tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
tar zxvf nginx-rtmp-module.tar.gz

3、将nginx的编译参数修改成如下形式

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --with-ipv6 --with-http_secure_link_module --add-module=$HOME/nginx_mod_h264_streaming-2.2.7 --add-module=$HOME/nginx-accesskey-2.0.3 --add-module=$HOME/nginx-rtmp-module --add-module=$HOME/nginx-rtmp-module/hls

4、防盗链配置nginx.conf

location ~ \.flv$ {
           accesskey on;
           accesskey_hashmethod md5;
           accesskey_arg "key";
           accesskey_signature "password$remote_addr";
           secure_link $arg_st,$arg_e;
           secure_link_md5 password$uri$arg_e;

                        if ($secure_link = "") {
                                    return 403;
                                 }
 
                        if ($secure_link = "0") {
                                    return 403;
                                 }
           flv;
}

location ~ \.mp4$ {
           accesskey on;
           accesskey_hashmethod md5;
           accesskey_arg "key";
           accesskey_signature "password$remote_addr";
           secure_link $arg_st,$arg_e;
           secure_link_md5 password$uri$arg_e;

                        if ($secure_link = "") {
                                    return 403;
                                 }
 
                        if ($secure_link = "0") {
                                    return 403;
                                 }

           mp4;
}

php代码:

<?php
$secret = 'password'; # 密钥
$path = '/download/she.flv'; # 下载文件
$ipkey= md5("password".$_SERVER['REMOTE_ADDR']); #加密IP
 # 下载到期时间,time是当前时间,300表示300秒,也就是说从现在到300秒之内文件不过期
 $expire = time()+300;
# 用文件路径、密钥、过期时间生成加密串
 $md5 = base64_encode(md5($secret . $path . $expire, true));
 $md5 = strtr($md5, '+/', '-_');
 $md5 = str_replace('=', '', $md5);
# 加密后的下载地址
 echo '<a href=http://s1.xsdou.com/download/she.flv?key='.$ipkey.'&st='.$md5.'&e='.$expire.'>she.flv</a>';
 echo '<br>http://s1.xsdou.com/download/she.flv?key='.$ipkey.'&st='.$md5.'&e='.$expire;
 ?>

5、配置流媒体服务器
修改nginx主配置文件,配置虚拟主机(这里我们暂时只配置和测试点播,直播请看借鉴地址):

user  nobody nobody;
 worker_processes 4;
 error_log  logs/nginx_error.log  info;
 pid        logs/nginx.pid;
 worker_rlimit_nofile 51200;
 events
    {
        use epoll;
        worker_connections 51200;
    }
 #rtmp_auto_push on;
 rtmp {
    server {
    listen 1935;
    application vod {
            play /opt/media/nginxrtmp/flv;
    }
    }
 }
 http
    {
        include      mime.types;
        default_type  application/octet-stream;
        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m ;
        limit_conn_zone $binary_remote_addr zone=perip:256k;
        limit_conn_log_level notice;
        sendfile on;
        tcp_nopush    on;
        keepalive_timeout 6000;#测试并发临时调大
        tcp_nodelay on;
        gzip on;
        gzip_min_length  1k;
        gzip_buffers    4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 2;
        gzip_types      text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
        #log format
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
 
#支持flv
 server
        {
            listen      8081;
            server_name 192.168.0.33;
            root /opt/pub/media/nginx; #http协议时候,flv视频位置
            location ~ .*.(flv|swf|mp4|wma|wmv)$ {
                    valid_referers none blocked *.xxxx.com http://localhost;
                    if ($invalid_referer) {
                    return 403;
                    }
            }
            location ~ \.flv$ {
                    flv;
                    limit_conn one 20;#限制客户端并发连接数
                    limit_rate 200k;#限制每客户端最大带宽
            }
            location ~ \.mp4$ {
                    flv;
                    limit_conn one 20;
                    limit_rate 200k;
            }
            access_log  logs/nginxflv_access.log  main;
 }
 
server
        {
            listen      8082;
            server_name 192.168.0.33;
            index index.html;
            location / {
                root /opt/pub/media/nginx-rtmp;
            }
            access_log  logs/nginxrtmpflv_access.log  main;
        }
 
server {
        listen      8080;
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root /opt/pub/soft/nginx-rtmp-module;
        }
        location / {
            root /opt/pub/soft/nginx-rtmp-module/test/rtmp-publisher;
        }
    }

http://192.168.0.33:8080/stats 查看rtmp客户请求信息
http://192.168.0.33:8081/index.html 查看nginx http协议时候的flv视频
http://192.168.0.33:8082/index.html 查看nginx rtmp协议时候的flv视频

https://github.com/arut/nginx-rtmp-module

点赞