-- 将url地址替换为服务器真实文件路径 local uri = ngx.var.uri -- 第四个参数指定1,限制替换1次 local fpath = string.gsub(uri, "/logs", "/opt/worker/logs", 1)
-- 读取文件全部内容 local f = io.open(fpath, "r") -- 文件不存在时,返回404 if f == nil then ngx.status = 404 ngx.say("file not found") ngx.exit(ngx.OK) end local content = f:read("*all") f:close() ngx.say(content)
-- 将url地址替换为服务器真实文件路径 local uri = ngx.var.uri -- 第四个参数指定1,限制替换1次 local fpath = string.gsub(uri, "/logs", "/opt/worker/logs", 1) -- 文件不存在时,返回404 local f = io.open(fpath, "r") if f == nil then ngx.status = 404 ngx.say("file not found") ngx.exit(ngx.OK) end
local tail = ngx.var.arg_tail -- tail约定合法值范围为:非零整数 iftonumber(tail) == 0 then ngx.status = 400 ngx.say("arg tail should not be 0") ngx.exit(ngx.OK) end -- tail默认为10 if tail == nil then tail = 10 end
-- 调用shell获取文件尾部n行 -- 拼接cmd local cmd = 'tail -n ' .. tail .. ' ' .. fpath -- 执行cmd并读取执行结果 local handle = io.popen(cmd) local result = handle:read("*a") handle:close() -- 输出结果 ngx.say(content)
-- 调用shell读取文件尾部tail行 localfunctiontail_file(fpath, tail) -- 调用shell获取文件尾部n行 -- 拼接cmd local cmd = 'tail -n ' .. tail .. ' ' .. fpath -- 执行cmd并读取执行结果 local handle = io.popen(cmd) local content = handle:read("*a") handle:close() return content end
-- 读取文件全部内容 localfunctionread_file(fpath) -- 读取文件全部内容 local f = io.open(fpath, "r") local content = f:read("*all") return content end
-- 将url地址替换为服务器真实文件路径 local uri = ngx.var.uri -- 第四个参数指定1,限制替换1次 local fpath = string.gsub(uri, "/logs", "/opt/worker/logs", 1) -- 文件不存在时,返回404 local f = io.open(fpath, "r") if f == nil then ngx.status = 404 ngx.say("file not found") ngx.exit(ngx.OK) end
local tail = ngx.var.arg_tail -- tail约定合法值范围为:非零整数 iftonumber(tail) == 0 then ngx.status = 400 ngx.say("arg tail should not be 0") ngx.exit(ngx.OK) end -- tail默认为10 if tail == nil then tail = 10 end
-- tail为正数时,读取文件尾部n行;tail为负数时,读取文件全部内容 local result = '' iftonumber(tail) > 0 then result = tail_file(fpath, tail) else result = read_file(fpath) end -- 输出结果 ngx.say(result)
-- 调用shell读取文件尾部tail行 localfunctiontail_file(fpath, tail) -- 调用shell获取文件尾部n行 -- 拼接cmd local cmd = 'tail -n ' .. tail .. ' ' .. fpath -- 执行cmd并读取执行结果 local handle = io.popen(cmd) local content = handle:read("*a") handle:close() return content end
-- 读取文件全部内容 localfunctionread_file(fpath) -- 读取文件全部内容 local f = io.open(fpath, "r") local content = f:read("*all") return content end
-- url输入路径是否合法 localfunctionis_legal_url(uri) -- find函数指定第四个参数为true,表示搜索的字符串为纯文本,默认false表示pattern匹配 ifstring.find(uri, "..", 1, true) then returnfalse end ifstring.find(uri, "~", 1, true) then returnfalse end ifstring.find(uri, "`", 1, true) then returnfalse end ifstring.find(uri, "'", 1, true) then returnfalse end ifstring.find(uri, "\"", 1, true) then returnfalse end ifstring.find(uri, "$", 1, true) then returnfalse end returntrue end
-- 将url地址替换为服务器真实文件路径 local uri = ngx.var.uri ifnot is_legal_url(uri) then ngx.status = 400 ngx.say("uri not legal") ngx.exit(ngx.OK) end
-- 第四个参数指定1,限制替换1次 local fpath = string.gsub(uri, "/logs", "/opt/worker/logs", 1) -- 文件不存在时,返回404 local f = io.open(fpath, "r") if f == nil then ngx.status = 404 ngx.say("file not found") ngx.exit(ngx.OK) end
local tail = ngx.var.arg_tail -- tail约定合法值范围为:非零整数 iftonumber(tail) == 0 then ngx.status = 400 ngx.say("arg tail should not be 0") ngx.exit(ngx.OK) end -- tail默认为10 if tail == nil then tail = 10 end
-- tail为正数时,读取文件尾部n行;tail为负数时,读取文件全部内容 local result = '' iftonumber(tail) > 0 then result = tail_file(fpath, tail) else result = read_file(fpath) end -- 输出结果 ngx.say(result)