首页 > 技术文章 > 使用 nginx_rtmp_module 搭建个人学习流媒体服务器

lovesKey 2019-06-15 13:37 原文

nginx 共享服务器中的视频

如果服务器处于内网或者没有公网ip,可以使用 相关软件进行打洞或转发 如: frpc https://github.com/fatedier/frpholer https://github.com/wisdom-projects/holer

需求场景:
服务器中有很多视频,有时候在外面想要看些视频的时候就有些麻烦了.可以通过http共享然后使用vlc之类播放器播放.但是需要自制播放列表很是麻烦.
实现方法:
使用nginx 的 nginx_rtmp_module 插件可以实现 在网页中点击目录方式在线观看视频.

所以nginx是必要的.而且要源码编译,编译时候要加上nginx_rtmp_module 这个模块.

下载编译安装

必要的编译包自行安装.gcc之类还有一些依赖包

 wget http://nginx.org/download/nginx-1.16.0.tar.gz
 wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
 tar xf nginx-1.16.0.tar.gz
 unzip master.zip
 cd nginx-1.16.0
 mv * ../
 cd ..
 ./configure --with-http_ssl_module  --add-module=../nginx-rtmp-module-master
 make -j4
 make install

至此nginx编译安装完成.

配置nginx

vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  2;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

rtmp { #不接受推流这里可以不需要
    server {
        listen 1935;
 
        application vod {
            play /mnt/s2t; #这是一个目录的名称,如果是linux,则写具体位置如/opt/video
        }
    }
}


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       8080;
        server_name  localhost;

        charset utf8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /mnt/s2t;
	    autoindex on;
	    auth_basic "needAuth";
        auth_basic_user_file /usr/local/nginx/conf/passwd.db;
	    index  index.html index.htm;
        }
 
}

生成登录验证密码文件

htpasswd -c /usr/local/nginx/conf/passwd.db makeit

测试访问

/usr/local/nginx/sbin/nginx -s reload

关于 倍速播放
经测试火狐浏览器支持倍速播放的. 其他chrome,opera 不支持

参考:

关于模块更多信息

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

推荐阅读