Openresty 学习笔记(三)扩展库之neturl

时间:2021-03-01 16:01:30

github地址:https://github.com/golgote/neturl

最近在搞一个视频加密播放,中间使用要用lua 匹配一个域名,判断该域名是否正确

PS:使用PHP很好做,lua 的没找到呀

preg_match("/^((http|https):\/\/)?([^\/]+)/i", "http://www.tinywan.com/p/124.html", $matches);
var_dump($matches); // 输出结果
array(4) {
[0]=>
string(22) "http://www.tinywan.com"
[1]=>
string(7) "http://"
[2]=>
string(4) "http"
[3]=>
string(15) "www.tinywan.com"
}

下来使用lua 在Nginx 中实现

(1)直接下载官网的url.lua文件,放在 $PATH/openresty/lualib/resty 目录下

(2)编写文件 ngx_re_match.lua

local url = require "resty.url"
local u = url.parse("https://www.tinywan.com/p/124.html")
ngx.say("host: ",u.host)

(3)location 匹配

location /lua_match {
content_by_lua_file .../nginx/conf/lua/ngx_re_match.lua;
}

(4)通过curl 请求访问结果 

curl https://hls-auth.tinywan.com/lua_match
host: www.tinywan.com