nginx + lua 生成泛目录的方法

singapore uncle 2023-09-22 00:21:01 阅读 0评论
ran.lua
math.randomseed(os.time() + tonumber(tostring({}):sub(8)))

function getRandom(n)
    local t = {
         "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",}
local s =  ""
for i = 1, n do
      s = s .. t[math.random(#t)]
end
return s
end

return getRandom(4)


hello.lua

function file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end

function lines_from(file)
if not file_exists(file) then return {} end
lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end


math.randomseed(os.time() + tonumber(tostring({}):sub(8)))

local content = '/usr/local/openresty/nginx/conf/vhost/file/url.txt'
local lines = lines_from(content)
local key = lines[math.random(1,4900)]
return key



nginx.conf

      default_type text/html; 
       charset utf-8;
       set_by_lua_file $hello '/usr/local/openresty/nginx/conf/vhost/lua/hello1.lua';
       echo $hello;
免责声明:本站所有资源均由网络搜集用户投稿而来,若有侵权,违法违规侵权内容请联系本站并提供必要证明,本站将及时删除违法违规内容,谢谢合作!