Nginx與Nginx-rtmp-module搭建RTMP視頻直播和點播服務器


一.開發環境

  Nginx地址:  http://nginx.org/download/nginx-1.14.2.tar.gz

  Nginx-rtmp-module地址: https://github.com/arut/nginx-rtmp-module

二.執行安裝命令

  備份nginx原文件

cp /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/nginx.bak.201812.1.rtmp 

  下載模塊並解壓: 

cd /thirdparties

wget https://github.com/arut/nginx-rtmp-module/archive/master.zip unzip master.zip

  添加模塊並編譯openresty(注意:重新編譯的時候,記得一定要把以前編譯過的模塊一同加到configure參數里面)

查看原來的模塊
/usr/local/openresty/nginx/sbin/nginx -V 
cd /thirdparties/openresty-1.11.2.2

./configure --prefix=/usr/local/openresty  --add-module=/thirdparties/nginx-rtmp-module-master --with-http_ssl_module

make

  停止nginx

/usr/local/openresty/nginx/sbin/nginx -s stop

  備份nginx原文件

mv /usr/local/openresty/nginx/sbin/nginx /usr/local/openresty/nginx/sbin/nginx.bak.201812.origin.rtmp 

  將編譯后的nginx文件覆蓋原文件並重啟nginx

cp /thirdparties/openresty-1.11.2.2/build/nginx-1.11.2/objs/nginx /usr/local/openresty/nginx/sbin
/usr/local/openresty/nginx/sbin/nginx
查看module是否已經加入到nginx
/usr/local/openresty/nginx/sbin/nginx -V


三.nginx的配置

  在添加完模塊后nginx的配置文件( /usr/local/nginx/conf/nginx.config)中要增加對rtmp的配置

  與http {... ...}同級的地方配置rtmp

  

  events {
    use epoll;
    worker_connections 1024;
  }


#RTMP服務 rtmp { server { listen 1935; #//服務端口 chunk_size 4096; #//數據傳輸塊的大小 application vod { play /opt/video; #//視頻文件存放位置。 } #直播 application live{ live on; record off; #on時將流轉換成flv視頻文件 #record all; record_path /opt/video/hls; #record_max_size 50M; record_max_size 2M;
       notify_method get;#鑒權        
       on_publish http://localhost:8081/chat_auth;#鑒權
            hls on; #這個參數把直播服務器改造成實時回放服務器。
            wait_key on; #對視頻切片進行保護,這樣就不會產生馬賽克了
            hls_path /opt/video/hls; #切片視頻文件存放位置
            hls_fragment  600s;     #設置HLS片段長度
            hls_playlist_length 1m;  #設置HLS播放列表長度,這里設置的是10分鍾
            hls_continuous on; #連續模式
            hls_cleanup on;    #對多余的切片進行刪除
            hls_nested on;     #嵌套模式
            allow play all;
       }
   }
}

 

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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8081;
        server_name  rtmp_server;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /chat_auth {
             proxy_pass  https://api.xxxx.com/item_chat_api/v1/account/platform/auth;
             proxy_redirect     off;
             proxy_set_header   Host             $host;
             proxy_set_header   X-Real-IP        $remote_addr;
             proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

 

 

注意:

  1. 如果不存在文件夾“/opt/video”、“/opt/video/hls”則要創建一個,並且要改成可讀可寫的權限(chmod  777 /opt/video)

  2. 防火牆是否通:

iptables -I INPUT -p tcp -m state --state NEW --dport 1935 -j ACCEPT #放行1935端口的 NEW狀態請求
service iptables save #保存規則
service iptables restart #重啟防火牆保證新的規則加載進來

 

 

參考:

  https://blog.csdn.net/qzcsu/article/details/72782759

  https://blog.csdn.net/qq_36510261/article/details/78971387

  https://blog.csdn.net/zhiyuan_2007/article/details/52003713

  https://www.cnblogs.com/lidabo/p/7099501.html

java代碼鑒權:

@ResponseBody()
    @RequestMapping(value="/auth", method={RequestMethod.GET,RequestMethod.POST})
    public void auth(HttpServletRequest request, HttpServletResponse response, @RequestParam(required = true)String auth_key, 
            @RequestParam(required = false) String jsonpcallback) {
        //openId$userId$orderId
        logger.info("auth auth_key:{}",auth_key);
        RpcResponseDTO<Boolean> rpcResult = new RpcResponseDTO<>();
        String fail= "{\"code\":\"500\",\"detail\":\"sys error\"}";
        String succ= "{\"code\":\"200\",\"detail\":\"SUCCESS\"}";
        String result = fail;
        if (StringUtils.isEmpty(auth_key)) {
            response.setStatus(500);
        }else {
            String[] auth = auth_key.split("\\$");
            logger.info("auth auth[0]:{},auth[1]:{},auth[2]:{}",auth[0],auth[1],auth[2]);
            if (auth.length!=3) {
                response.setStatus(500);
            }else {
                RtmpAuthParam param = new RtmpAuthParam();
                param.setOpenId(auth[0]);
                param.setUserId(Long.parseLong(auth[1]));
                param.setOrderId(Long.parseLong(auth[2]));
                rpcResult = platformAccountRpcService.validateRtmpAuth(Request.build(param));
                logger.info("auth rpcResult:{}",JsonHelper.getJSONString(rpcResult));
                if (!rpcResult.hasError()&&rpcResult.getPayload()!=null&&rpcResult.getPayload()) {
                    response.setStatus(200);
                    result = succ;
                }else {
                    response.setStatus(500);
                }
            }
        }
        logger.info("auth result:{}",result);
        SpringMVCHelper.renderText(response,result);
    }

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM