Nginx之设置白名单以及半年之前的html禁止访问
location ~ .*\.(html|css|jpg|gif|js|png|htm)?$ {
access_by_lua_block {
-- 获取请求的文件名称
local filename = ngx.var.request_filename
-- 定义白名单
local white_filenames = {
"zyk-homePage.html",
"l1604562900.html",
"zyk-checkPage.html",
"zyk-userinfo.html"
}
-- 从filename不能获取文件名,因为filename显示的完整的路径,所以还需要进行处理
local index = string.find(filename, "/[^/]*$")
local file_name = string.sub(filename, index + 1)
local log_message = "White filenames: " .. table.concat(white_filenames, ", ")
ngx.log(ngx.ERR, log_message)
ngx.log(ngx.ERR, "My variable value: ", file_name)
-- 检查白名单,在白名单的直接放行,后面修改时间不做检查
for _, whitelist_filename in ipairs(white_filenames) do
if file_name == whitelist_filename then
return
end
end
-- 获取该文件的修改时间
local command = "stat -c %Y " .. filename
local handle = io.popen(command)
local result = handle:read("*a")
handle:close()
-- 解析命令的执行结果,获取文件的修改时间
local mtime = tonumber(result)
-- 文件修改时间小于6个月前的时间戳,返回403
local six_month_ago_time = os.time() - (60 * 60 * 24 * 30 * 6)
if mtime < six_month_ago_time then
ngx.status = 403
ngx.exit(ngx.HTTP_FORBIDDEN)
end
}

浙公网安备 33010602011771号