centos上搭建nginx视频点播服务器(nginx+vod+lua http发送鉴权消息)

news2024/9/20 10:51:07

需求背景:

    想着搭建一个视频点播服务器,最后选择了nginx+vod的方案,用lua脚本写拉流鉴权,但是环境搭建过程中又发现nginx++vod+lua的环境并不是很容易搭建,是nginx+lua的环境,手动搭建比较麻烦,但还是实现了,还有另一种方案是可以用openresty(nginx+lua)+ vod的方案,因为openresty已经帮你包含了nginx和lua的环境,但是还不包含vod这个点播模块,只需自己add-module vod就可以了

注意本文章介绍的点播lua鉴权,只做了m3u8文件的鉴权,并未对ts文件的鉴权,简单的判断是否带token的鉴权,至于token的设计,还需自己配置,

本文主要介绍了环境的搭建,鉴权流程的分析,重在流程,最后会在文中附上安装脚本和nginx.conf的整个配置文件

方案一 nginx+vod+lua

配置服务器DNS:

echo "nameserver 114.114.114.114" >> /etc/resolv.conf

安装网路工具

yum install wget ntpdate git -y 

安装编译工具及依赖库

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y

同步服务器时间

ntpdate ntp.aliyun.com 
timedatectl set-timezone Asia/Shanghai

创建点播服务器的安装目录

我这里安装到了/usr/cloudland/nginx的目录下

mkdir -p /usr/cloudland/nginx

下载配置安装lua解释器LuaJIT

wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xzvf LuaJIT-2.0.4.tar.gz 
cd LuaJIT-2.0.4
make install PREFIX=$NGINX_INSTALL_PATH/luajit 
export LUAJIT_LIB=$NGINX_INSTALL_PATH/luajit/lib
export LUAJIT_INC=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0
cd -

注意上面的两个export命令,配置lua解释器的环境变量

下载nginx NDK(ngx_devel_kit)扩展模块

wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar -xzvf v0.3.0.tar.gz

下载lua-nginx-module

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
tar -xzvf v0.10.9rc7.tar.gz

下载安装lua-resty-http模块(lua的库,实现http功能的一些库)

wget https://github.com/ledgetech/lua-resty-http/archive/refs/tags/v0.16.1.tar.gz
tar -zxvf v0.16.1.tar.gz
cp -r lua-resty-http-0.16.1/lib/resty/ $NGINX_INSTALL_PATH/luajit/lib/lua/5.1/
cp -r lua-resty-http-0.16.1/lib/resty/ $NGINX_INSTALL_PATH/luajit/share/lua/5.1/

注意上面的两个cp的命令,这个是解决resty-http找不到的问题

下载安装lua-cjson模块(lua的库,为lua提供json相关功能)

wget https://github.com/openresty/lua-cjson/archive/refs/tags/2.1.0.9.tar.gz
tar -zxvf 2.1.0.9.tar.gz
cd lua-cjson-2.1.0.9
make LUA_VERSION=5.1 PREFIX=$NGINX_INSTALL_PATH/luajit/ LUA_INCLUDE_DIR=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0/
make LUA_VERSION=5.1 PREFIX=$NGINX_INSTALL_PATH/luajit/ LUA_INCLUDE_DIR=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0/ install
cd -

注意上面的make参数,指定的安装路径及头文件,解决的是找不到lua-cjson相关库的问题

下载nginx-vod模块

这个模块是为nginx实现点播功能的

wget https://github.com/kaltura/nginx-vod-module/archive/refs/tags/1.28.tar.gz
tar -zxvf 1.28.tar.gz

下载配置安装nginx

wget https://nginx.org/download/nginx-1.20.1.tar.gz
tar -xzvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
./configure --prefix=/usr/cloudland/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre --add-module=../lua-nginx-module-0.10.9rc7 --add-module=../ngx_devel_kit-0.3.0 --add-module=../nginx-vod-module-1.28/
make
make install

将luajia相关库加载一下

echo "$NGINX_INSTALL_PATH/luajit/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig

修改nginx配置文件nginx.conf

有两处修改

修改处一:

init_by_lua_block {
        cjson = require "cjson";
        http = require "resty.http";
    }

添加位置:

修改处二:

 location /vod {

                rewrite_by_lua_block {
                                -- local cjson = require "cjson"
                                -- local http = require "resty.http"
                                local httpc = http.new()
                                local ngx = ngx
                                local headers = ngx.req.get_headers()
                                local extension = ngx.var.request_uri:match(".+%.(%w+)$")

                                local token = headers["token"]
                                local request_method = ngx.var.request_method
                                local args = nil
                                if "GET" == request_method then
                                        args = ngx.req.get_uri_args()
                                elseif "POST" == request_method then
                                        ngx.req.read_body()
                                        args = ngx.req.get_post_args()
                                end

                                if extension == 'm3u8' then
                                        token = args["token"];
                                        if not token then
                                                ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
                                                ngx.status = ngx.HTTP_FORBIDDEN
                                                ngx.say("Nil token,Not Privileged To Play")
                                                -- ngx.say(ngx.var.request_uri);
                                                -- ngx.say(extension);
                                                ngx.exit(200)
                                        end
                                        -- 要实现token鉴权的服务,header和参数已经实现,根据实际需要选择
                                        -- 可以将URL发送给一个golang服务,用golang服务来做具体鉴权
                                        local url = "http://172.20.0.95:11985/api/rest/v1/vod/check";

                                        local res, err = httpc:request_uri(url, {method="GET", headers={["token"]=token}})

                                        if not res then
                                                ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
                                                ngx.say(cjson.encode({message = "Error getting response",status = ngx.HTTP_INTERNAL_SERVER_ERROR }));
                                                ngx.exit(200)
                                        end
                                        if res.body == '0' then
                                                ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
                                                ngx.say("Valid token,Not Privileged To Play.");
                                                ngx.exit(200)
                                        end
                                end
                }

                vod hls; # 协议使用hls模式
                vod_mode local; # 访问模式指定为local模式

                vod_align_segments_to_key_frames on; # 每个切片以关键帧开头
                vod_manifest_segment_durations_mode accurate; # 精确显示每个切片的长度
                root /media;

                #alias /media; # 视频文件路径
                #proxy_pass http://172.0.0.74:80/lua;
        }

添加位置:

整个nginx.conf文件的配置


#user  nobody;
worker_processes  1;

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;
}


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;

    init_by_lua_block {
        cjson = require "cjson";
        http = require "resty.http";
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        location /lua {
                default_type 'text/plain';
                content_by_lua 'ngx.say("hello, lua for vod")';
    }


        location /vod {

                rewrite_by_lua_block {
                                -- local cjson = require "cjson"
                                -- local http = require "resty.http"
                                local httpc = http.new()
                                local ngx = ngx
                                local headers = ngx.req.get_headers()
                local extension = ngx.var.request_uri:match(".+%.(%w+)$")
                
                                local token = headers["token"]
                                local request_method = ngx.var.request_method
                                local args = nil
                                if "GET" == request_method then
                                        args = ngx.req.get_uri_args()
                                elseif "POST" == request_method then
                                        ngx.req.read_body()
                                        args = ngx.req.get_post_args()
                                end

                if extension == 'm3u8' then
                                    token = args["token"];
                                    if not token then
                                            ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
                                            ngx.status = ngx.HTTP_FORBIDDEN
                                            ngx.say("Nil token,Not Privileged To Play")
                                            -- ngx.say(ngx.var.request_uri);
                                            -- ngx.say(extension);
                                            ngx.exit(200)
                                    end
                                    -- 要实现token鉴权的服务,header和参数已经实现,根据实际需要选择
                    -- 可以将URL发送给一个golang服务,用golang服务来做具体鉴权
                                    local url = "http://172.20.0.95:11985/api/rest/v1/vod/check";

                                    local res, err = httpc:request_uri(url, {method="GET", headers={["token"]=token}})

                                    if not res then 
                                            ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
                                            ngx.say(cjson.encode({message = "Error getting response",status = ngx.HTTP_INTERNAL_SERVER_ERROR }));
                                            ngx.exit(200)
                                    end
                                    if res.body == '0' then
                                            ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
                                            ngx.say("Valid token,Not Privileged To Play.");
                                            ngx.exit(200)
                                    end
                                end
                }

                vod hls; # 协议使用hls模式
                vod_mode local; # 访问模式指定为local模式

                vod_align_segments_to_key_frames on; # 每个切片以关键帧开头
                vod_manifest_segment_durations_mode accurate; # 精确显示每个切片的长度
        root /media;

                #alias /media; # 视频文件路径
                #proxy_pass http://172.0.0.74:80/lua;
        }


        #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;
    #    }
    #}

}

将上述nginx.conf修改到/usr/cloudland/nginx.conf

启动nginx

cd /usr/cloudland/nginx/
./sbin/nginx -p $PWD -c conf/nginx.conf

结果验证:

在验证之前最后关闭防火墙

关闭防火墙:

systemctl stop firewalld

随便拷贝一个视频到/media/vod目录下

用浏览器打开http://172.24.0.74/vod/720p-test.mp4/index.m3u8

上述不带token的结果

用浏览器打开带token的URLhttp://172.24.0.74/vod/720p-test.mp4/index.m3u8

发现会允许下载index.m3u8文件

用VLC打开带token的URL发现视频可以播放

整个环境搭建的脚本:

#!/bin/sh
NGINX_INSTALL_PATH=/usr/cloudland/nginx
echo "nameserver 114.114.114.114" >> /etc/resolv.conf

yum install wget ntpdate git -y

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y


ntpdate ntp.aliyun.com

timedatectl set-timezone Asia/Shanghai


# LuaJIT
if [ ! -f LuaJIT-2.0.4.tar.gz ]; then
    wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
fi
tar xzvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make install PREFIX=$NGINX_INSTALL_PATH/luajit
export LUAJIT_LIB=$NGINX_INSTALL_PATH/luajit/lib
export LUAJIT_INC=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0
cd -

#ngx_devel_kit
if [ ! -f v0.3.0.tar.gz ]; then
    wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
fi
tar -xzvf v0.3.0.tar.gz

#lua-nginx-module
if [ ! -f v0.10.9rc7.tar.gz ]; then
    wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
fi
tar -xzvf v0.10.9rc7.tar.gz


#lua-resty-http
if [ ! -f v0.16.1.tar.gz ]; then
    wget https://github.com/ledgetech/lua-resty-http/archive/refs/tags/v0.16.1.tar.gz
fi
tar -zxvf v0.16.1.tar.gz 
cp -r lua-resty-http-0.16.1/lib/resty/ $NGINX_INSTALL_PATH/luajit/lib/lua/5.1/
cp -r lua-resty-http-0.16.1/lib/resty/ $NGINX_INSTALL_PATH/luajit/share/lua/5.1/ 

#lua-cjson
if [ ! -f 2.1.0.9.tar.gz ]; then
    wget https://github.com/openresty/lua-cjson/archive/refs/tags/2.1.0.9.tar.gz
fi
tar -zxvf 2.1.0.9.tar.gz
cd lua-cjson-2.1.0.9
make LUA_VERSION=5.1 PREFIX=$NGINX_INSTALL_PATH/luajit/ LUA_INCLUDE_DIR=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0/
make LUA_VERSION=5.1 PREFIX=$NGINX_INSTALL_PATH/luajit/ LUA_INCLUDE_DIR=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0/ install
cd -

# vod module
if [ ! -f 1.28.tar.gz ]; then
    wget https://github.com/kaltura/nginx-vod-module/archive/refs/tags/1.28.tar.gz
fi
tar -zxvf 1.28.tar.gz


# nginx
if [ ! -f nginx-1.20.1.tar.gz ]; then
    wget https://nginx.org/download/nginx-1.20.1.tar.gz
fi
tar -xzvf nginx-1.20.1.tar.gz
cd nginx-1.20.1
 ./configure --prefix=$NGINX_INSTALL_PATH --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-pcre --add-module=../lua-nginx-module-0.10.9rc7 --add-module=../ngx_devel_kit-0.3.0 --add-module=../nginx-vod-module-1.28/
make
make install
cd -

echo "$NGINX_INSTALL_PATH/luajit/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig

\cp ./nginx.conf $NGINX_INSTALL_PATH/conf

cd $NGINX_INSTALL_PATH

$NGINX_INSTALL_PATH/sbin/nginx -p $NGINX_INSTALL_PATH -c $NGINX_INSTALL_PATH/conf/nginx.conf 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/343896.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Numpy基础与实例——人工智能基础

文章目录一、Numpy概述1、优势2、numpy历史3、Numpy的核心:多维数组4、内存中的ndarray对象4.1 元数据(metadata)4.2 实际数据二、numpy基础1、 ndarray数组2、 arange、zeros、ones、zeros_like3、ndarray对象属性的基本操作3.1 修改数组维度…

羊了个羊游戏开发教程1:堆叠牌的拾取

本文首发于微信公众号: 小蚂蚁教你做游戏。欢迎关注领取更多学习做游戏的原创教程资料,每天学点儿游戏开发知识。嗨!大家好,我是小蚂蚁。最近“羊了个羊”小游戏爆火。一下子让想做微信小游戏或者想学做微信小游戏的人多了很多&am…

Java Map集合

8 Map集合 HashMap: 元素按照键是无序,不重复,无索引,值不做要求 LinkedHashMap: 元素按照键是有序,不重复,无索引,值不做要求 8.1 Map集合概述和特点 Map集合是一种双列集合,每个元素包含两个…

【C++】 C C++ 内存管理

文章目录📕 C、C 内存分布📕 C 内存管理方式1. 操作内置类型2. 操作自定义类型📕 operator new 与 operator delete📕 定位 new📕 C、C 内存分布 C 和 C 的内存分布没什么区别,C 是基于 C 语言的&#xff…

腾讯xSRC[linux+docker]搭建教程

腾讯xSRC[linuxdocker]搭建教程 1.下载镜像 docker pull xsrc/xsrc:v1.0.12.启动镜像 1️⃣启动镜像 docker run -it -d --name xsrc_web -p 60080:80 -p 63306:3306 --privilegedtrue xsrc/xsrc:v1.0.1注意将3306端口映射到8806端口,以便于远程连接访问容器内数…

手写识别字体的步骤是什么?怎么识别图片中的文字?

手写识别字体的步骤是什么?怎么识别图片中的文字? 1. 打开信风工具网,点击拍照按钮,选择拍图识字模式,对准需要识别的文件进行拍摄。在线工具地址: https://ocr.bytedance.zj.cn/image/ImageT…

VScode 自定义主题颜色

vscode其实已经有很多完善且好看的主题了,但我总觉得每一个主题对我来说,都有那么一点点不够完美,比如亮色的主题,颜色就没有深色主题那么好看,对比度高。 好不容易看到一个好看的主题吧,又觉得某一部分的…

2023213-popover弹窗框中的teleported属性--Element-plus踩坑日记

popover弹窗框中的teleported属性–Element plus踩坑日记 今天在做项目时,有一个地方用到了弹窗框,但是有需求需要修改弹窗的阴影部分 比如下方的 我想对阴影进行修改,但是很是纳闷,各种标签选择器都不生效,很奇怪。…

使用地理定位来自定义网络钓鱼

在全球市场中,地理定位的能力是巨大的。 从本质上讲,这意味着企业可以根据收件人的位置定制广告。 纽约人可能会收到与法国人不同的广告。这使得广告对企业更有价值,对消费者来说更个性化。 还有另一群人想要个性化他们的产品:…

2023年要跟踪的11个销售管理关键指标

销售管理关键指标有:营销合格线索数量(MQL)、MQL 到 SQL 的转换率、商机赢单率、获客成本、总销售额、客户终身价值(LTV)、LTV 与 CAC 比率、赢单周期、每客户平均销售额(平均客单价)、每销售人…

全球十大资质正规现货黄金交易平台排名榜单(最新版汇总)

如今,在金融市场上,黄金已经成为公众喜爱的避险产品,尤其是近年来出现的现货黄金,这是许多朋友日常财务管理的标准。但我们在参考黄金交易平台排名进场时,需要留意哪些因素? 1、交易模式 事实上&#xf…

软件测试 -- 高阶 2 软件测试与软件开发

辅车相依,唇亡齿寒。-- 《左传僖公五年》 释译:颊骨和齿床互相依靠,嘴唇没有了,牙齿就会感到寒冷。比喻利害密要相关,命运紧密相关联。-- 百度百科 测试与开发是什么关系? 1. 软件开发流程 2. 开发和测…

AcWing 167. 木棒(DFS + 剪枝优化)

AcWing 167. 木棒(DFS 剪枝优化)一、问题二、分析1、整体分析2、剪枝优化(1)优化搜索顺序(2)排除等效冗余(3)可行性剪枝(4)最优性剪枝(5&#xf…

ASEMI低压MOS管AO3401封装,AO3401图片

编辑-Z ASEMI低压MOS管AO3401参数: 型号:AO3401 封装:SOT-23 漏极-源极电压(VDS):30V 栅源电压(VGS):12V 连续漏电流(I):4.2A …

K_A12_004 基于STM32等单片机采集人体红外感应(HC-SR501)模块串口与OLED0.96双显示

K_A12_004 基于STM32等单片机采集人体红外感应(HC-SR501)模块串口与OLED0.96双显示一、资源说明二、基本参数参数引脚说明三、驱动说明模块工作原理:对应程序:四、部分代码说明1、接线引脚定义1.1、STC89C52RCHC-SR501模块1.2、STM32F103C8T6HC-SR501模块…

docker-compose概述与简单编排部署

一、Docker-compose 简介Docker-Compose项目是基于Python开发的Docker官方开源项目,负责实现对Docker容器集群的快速编排。Docker-Compose将所管理的容器分为三层,分别是 工程(project),服务(service&#…

MySQL学习笔记——CSDN学习记录九:数据库存储引擎

存储引擎 一、MySQL 体系结构: 二、存储引擎概念: MySQL 中的数据用于各种不同的技术存储在文件或内存。这些技术的每一个都使用不同的存储机制、索引技巧、锁定水平,最终提供不同的功能。通过选择不同的技术,能够得到更好的数据处…

03- SVC 支持向量机做人脸识别 (项目三)

数据集描述: sklearn的lfw_people函数在线下载55个外国人图片文件夹数据集来精确实现人脸识别并提取人脸特征向量数据集地址: sklearn.datasets.fetch_lfw_people — scikit-learn 1.2.1 documentationPCA降维: pca PCA(n_components0.9) 数据拆分: X_train, X_test, y_tra…

正大期货本周财经大事抢先看

美国1月CPI、Fed 等央行官员谈话 美国1月超强劲的非农就业人口,让投资人开始上修对这波升息循环利率顶点的预测,也使本周二 (14 日) 的美国 1月 CPI 格外受关注。 介绍正大国际期货主账户对比国内期货的优势 ​第一点:权限都在主账户 例如…

B站基于缓存优化 PRESTO 集群查询性能

导读:本次分享主题为 B 站 Presto 集群查询性能的优化,首先会简单介绍 Presto以及 B 站内部 Presto 集群的架构。接下来讲解针对 Presto 做的改造,主要是 Presto 搭配 Alluxio 和 Presto 搭配 Alluxio local cache 的使用。最后会对后续计划开…