在当月结束时使静态HTML文件到期

时间:2015-10-31 09:04:43

标签: nginx lua

我有几个静态HTML文件,这些文件是在每个月的第一天生成的,直到同月结束时才会生效。我希望在月底expire使用nginx,但不知道如何告诉location ~* \.(html|HTML)$ { gzip_static on; add_header Cache-Control public; expires 7d; } 这样做。

我可以使用Lua或其他语言在配置文件中计算该日期,还是会占用过多的计算能力?

目前,我使用此配置会在7天后过期。我必须在当月最后一天的23:59点钟将其更改为过期?

{{1}}

1 个答案:

答案 0 :(得分:0)

在lua中你可以简单地写一个函数。

-- day is the day of the month as a number
-- file can be 1 filename or a table of several filenames
function expire(file, day, time_)
    day = day or 1 -- day's default value incase it isn't set 
    -- get the current day of the month, convert it to a number and compare it to day
    -- get the current hour and minute and compare it to time_
    if tonumber(os.date("%d")) == day and os.date("%H:%M") == time_ then
        -- see if file is a table
        if type(file) == 'table' then
            -- if it is cycle through the table for the file names
            for _, filename in pairs(file) do
                -- make sure we are dealing with a string
                if type(filename) == 'string' then
                    -- delete the file
                    os.remove(filename)
                end
            end
        else
            -- make sure file is a string
            if type(file) == 'string' then
                -- delete the file
                os.remove(file)
            end
        end
    end
end

-- calling the function
expire("somefile.txt", 31, "23:59")

有关os.date的详情,请查看https://www.lua.org/pil/22.1.html