飞道的博客

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

439人阅读  评论(0)

需求背景:

    想着搭建一个视频点播服务器,最后选择了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

同步服务器时间


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

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

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


   
  1. mkdir -p /usr/cloudland/nginx
  2. export NGINX_INSTALL_PATH=/usr/cloudland/nginx

下载配置安装lua解释器LuaJIT


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

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

下载nginx NDK(ngx_devel_kit)扩展模块


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

下载lua-nginx-module


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

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


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

注意NGINX_INSTALL_PATH换成自己nginx的安装路径即可,上面的两个cp的命令,这个是解决resty-http找不到的问题

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


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

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

下载nginx-vod模块

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


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

下载配置安装nginx


   
  1. wget https://nginx.org/download/nginx-1.20.1.tar.gz
  2. tar -xzvf nginx-1.20.1.tar.gz
  3. cd nginx-1.20.1
  4. ./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/
  5. make
  6. make install

将luajia相关库加载一下


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

修改nginx配置文件nginx.conf

有两处修改

修改处一:


   
  1. init_by_lua_block {
  2. cjson = require "cjson";
  3. http = require "resty.http";
  4. }

添加位置:

修改处二:


   
  1. location /vod {
  2. rewrite_by_lua_block {
  3. -- local cjson = require "cjson"
  4. -- local http = require "resty.http"
  5. local httpc = http.new()
  6. local ngx = ngx
  7. local headers = ngx.req.get_headers()
  8. local extension = ngx.var.request_uri:match(".+%.(%w+)$")
  9. local token = headers["token"]
  10. local request_method = ngx.var.request_method
  11. local args = nil
  12. if "GET" == request_method then
  13. args = ngx.req.get_uri_args()
  14. elseif "POST" == request_method then
  15. ngx.req.read_body()
  16. args = ngx.req.get_post_args()
  17. end
  18. if extension == 'm3u8' then
  19. token = args["token"];
  20. if not token then
  21. ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
  22. ngx.status = ngx.HTTP_FORBIDDEN
  23. ngx.say("Nil token,Not Privileged To Play")
  24. -- ngx.say(ngx.var.request_uri);
  25. -- ngx.say(extension);
  26. ngx.exit(200)
  27. end
  28. -- 要实现token鉴权的服务,header和参数已经实现,根据实际需要选择
  29. -- 可以将URL发送给一个golang服务,用golang服务来做具体鉴权
  30. local url = "http://172.20.0.95:11985/api/rest/v1/vod/check";
  31. local res, err = httpc:request_uri(url, {method="GET", headers={["token"]=token}})
  32. if not res then
  33. ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
  34. ngx.say(cjson.encode({message = "Error getting response",status = ngx.HTTP_INTERNAL_SERVER_ERROR }));
  35. ngx.exit(200)
  36. end
  37. if res.body == '0' then
  38. ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
  39. ngx.say("Valid token,Not Privileged To Play.");
  40. ngx.exit(200)
  41. end
  42. end
  43. }
  44. vod hls; # 协议使用hls模式
  45. vod_mode local; # 访问模式指定为local模式
  46. vod_align_segments_to_key_frames on; # 每个切片以关键帧开头
  47. vod_manifest_segment_durations_mode accurate; # 精确显示每个切片的长度
  48. root /media;
  49. #alias /media; # 视频文件路径
  50. #proxy_pass http://172.0.0.74:80/lua;
  51. }

添加位置:

整个nginx.conf文件的配置


   
  1. #user nobody;
  2. worker_processes 1;
  3. error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. '$status $body_bytes_sent "$http_referer" '
  15. '"$http_user_agent" "$http_x_forwarded_for"';
  16. access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. init_by_lua_block {
  23. cjson = require "cjson";
  24. http = require "resty.http";
  25. }
  26. server {
  27. listen 80;
  28. server_name localhost;
  29. #charset koi8-r;
  30. #access_log logs/host.access.log main;
  31. location / {
  32. root html;
  33. index index.html index.htm;
  34. }
  35. location /lua {
  36. default_type 'text/plain';
  37. content_by_lua 'ngx.say("hello, lua for vod")';
  38. }
  39. location /vod {
  40. rewrite_by_lua_block {
  41. -- local cjson = require "cjson"
  42. -- local http = require "resty.http"
  43. local httpc = http.new()
  44. local ngx = ngx
  45. local headers = ngx.req.get_headers()
  46. local extension = ngx.var.request_uri:match(".+%.(%w+)$")
  47. local token = headers["token"]
  48. local request_method = ngx.var.request_method
  49. local args = nil
  50. if "GET" == request_method then
  51. args = ngx.req.get_uri_args()
  52. elseif "POST" == request_method then
  53. ngx.req.read_body()
  54. args = ngx.req.get_post_args()
  55. end
  56. if extension == 'm3u8' then
  57. token = args["token"];
  58. if not token then
  59. ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
  60. ngx.status = ngx.HTTP_FORBIDDEN
  61. ngx.say("Nil token,Not Privileged To Play")
  62. -- ngx.say(ngx.var.request_uri);
  63. -- ngx.say(extension);
  64. ngx.exit(200)
  65. end
  66. -- 要实现token鉴权的服务,header和参数已经实现,根据实际需要选择
  67. -- 可以将URL发送给一个golang服务,用golang服务来做具体鉴权
  68. local url = "http://172.20.0.95:11985/api/rest/v1/vod/check";
  69. local res, err = httpc:request_uri(url, {method="GET", headers={["token"]=token}})
  70. if not res then
  71. ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
  72. ngx.say(cjson.encode({message = "Error getting response",status = ngx.HTTP_INTERNAL_SERVER_ERROR }));
  73. ngx.exit(200)
  74. end
  75. if res.body == '0' then
  76. ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
  77. ngx.say("Valid token,Not Privileged To Play.");
  78. ngx.exit(200)
  79. end
  80. end
  81. }
  82. vod hls; # 协议使用hls模式
  83. vod_mode local; # 访问模式指定为local模式
  84. vod_align_segments_to_key_frames on; # 每个切片以关键帧开头
  85. vod_manifest_segment_durations_mode accurate; # 精确显示每个切片的长度
  86. root /media;
  87. #alias /media; # 视频文件路径
  88. #proxy_pass http://172.0.0.74:80/lua;
  89. }
  90. #error_page 404 /404.html;
  91. # redirect server error pages to the static page /50x.html
  92. #
  93. error_page 500 502 503 504 /50x.html;
  94. location = /50x.html {
  95. root html;
  96. }
  97. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  98. #
  99. #location ~ \.php$ {
  100. # proxy_pass http://127.0.0.1;
  101. #}
  102. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  103. #
  104. #location ~ \.php$ {
  105. # root html;
  106. # fastcgi_pass 127.0.0.1:9000;
  107. # fastcgi_index index.php;
  108. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  109. # include fastcgi_params;
  110. #}
  111. # deny access to .htaccess files, if Apache's document root
  112. # concurs with nginx's one
  113. #
  114. #location ~ /\.ht {
  115. # deny all;
  116. #}
  117. }
  118. # another virtual host using mix of IP-, name-, and port-based configuration
  119. #
  120. #server {
  121. # listen 8000;
  122. # listen somename:8080;
  123. # server_name somename alias another.alias;
  124. # location / {
  125. # root html;
  126. # index index.html index.htm;
  127. # }
  128. #}
  129. # HTTPS server
  130. #
  131. #server {
  132. # listen 443 ssl;
  133. # server_name localhost;
  134. # ssl_certificate cert.pem;
  135. # ssl_certificate_key cert.key;
  136. # ssl_session_cache shared:SSL:1m;
  137. # ssl_session_timeout 5m;
  138. # ssl_ciphers HIGH:!aNULL:!MD5;
  139. # ssl_prefer_server_ciphers on;
  140. # location / {
  141. # root html;
  142. # index index.html index.htm;
  143. # }
  144. #}
  145. }

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

启动nginx


   
  1. cd /usr/cloudland/nginx/
  2. ./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发现视频可以播放

整个环境搭建的脚本:


   
  1. #!/bin/sh
  2. NGINX_INSTALL_PATH=/usr/cloudland/nginx
  3. echo "nameserver 114.114.114.114" >> /etc/resolv.conf
  4. yum install wget ntpdate git -y
  5. yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
  6. ntpdate ntp.aliyun.com
  7. timedatectl set-timezone Asia/Shanghai
  8. # LuaJIT
  9. if [ ! -f LuaJIT-2.0.4.tar.gz ]; then
  10. wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
  11. fi
  12. tar xzvf LuaJIT-2.0.4.tar.gz
  13. cd LuaJIT-2.0.4
  14. make install PREFIX=$NGINX_INSTALL_PATH/luajit
  15. export LUAJIT_LIB=$NGINX_INSTALL_PATH/luajit/lib
  16. export LUAJIT_INC=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0
  17. cd -
  18. #ngx_devel_kit
  19. if [ ! -f v0.3.0.tar.gz ]; then
  20. wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
  21. fi
  22. tar -xzvf v0.3.0.tar.gz
  23. #lua-nginx-module
  24. if [ ! -f v0.10.9rc7.tar.gz ]; then
  25. wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
  26. fi
  27. tar -xzvf v0.10.9rc7.tar.gz
  28. #lua-resty-http
  29. if [ ! -f v0.16.1.tar.gz ]; then
  30. wget https://github.com/ledgetech/lua-resty-http/archive/refs/tags/v0.16.1.tar.gz
  31. fi
  32. tar -zxvf v0.16.1.tar.gz
  33. cp -r lua-resty-http-0.16.1/lib/resty/ $NGINX_INSTALL_PATH/luajit/lib/lua/5.1/
  34. cp -r lua-resty-http-0.16.1/lib/resty/ $NGINX_INSTALL_PATH/luajit/share/lua/5.1/
  35. #lua-cjson
  36. if [ ! -f 2.1.0.9.tar.gz ]; then
  37. wget https://github.com/openresty/lua-cjson/archive/refs/tags/2.1.0.9.tar.gz
  38. fi
  39. tar -zxvf 2.1.0.9.tar.gz
  40. cd lua-cjson-2.1.0.9
  41. make LUA_VERSION=5.1 PREFIX=$NGINX_INSTALL_PATH/luajit/ LUA_INCLUDE_DIR=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0/
  42. make LUA_VERSION=5.1 PREFIX=$NGINX_INSTALL_PATH/luajit/ LUA_INCLUDE_DIR=$NGINX_INSTALL_PATH/luajit/include/luajit-2.0/ install
  43. cd -
  44. # vod module
  45. if [ ! -f 1.28.tar.gz ]; then
  46. wget https://github.com/kaltura/nginx-vod-module/archive/refs/tags/1.28.tar.gz
  47. fi
  48. tar -zxvf 1.28.tar.gz
  49. # nginx
  50. if [ ! -f nginx-1.20.1.tar.gz ]; then
  51. wget https://nginx.org/download/nginx-1.20.1.tar.gz
  52. fi
  53. tar -xzvf nginx-1.20.1.tar.gz
  54. cd nginx-1.20.1
  55. ./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/
  56. make
  57. make install
  58. cd -
  59. echo "$NGINX_INSTALL_PATH/luajit/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
  60. ldconfig
  61. \cp ./nginx.conf $NGINX_INSTALL_PATH/conf
  62. cd $NGINX_INSTALL_PATH
  63. $NGINX_INSTALL_PATH/sbin/nginx -p $NGINX_INSTALL_PATH -c $NGINX_INSTALL_PATH/conf/nginx.conf

转载:https://blog.csdn.net/u011285281/article/details/129008487
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场