J’ai un couple de fichiers HTML statiques qui sont générés le premier de chaque mois et sont bons jusqu’à la fin du même mois. Je voudrais les expire
à la fin du mois mais je ne sais pas comment dire à nginx
de le faire.
Puis-je utiliser Lua ou un autre langage pour calculer cette date dans le fichier de configuration ou cela prend-il trop de puissance de calcul?
Actuellement, j’utilise cette configuration pour les expirer après 7 jours. Que dois-je changer pour les expirer à 23h59 au dernier jour du mois en cours?
location ~* \.(html|HTML)$ { gzip_static on; add_header Cache-Control public; expires 7d; }
En lua, vous pouvez simplement écrire une fonction.
-- 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 ssortingng if type(filename) == 'ssortingng' then -- delete the file os.remove(filename) end end else -- make sure file is a ssortingng if type(file) == 'ssortingng' then -- delete the file os.remove(file) end end end end -- calling the function expire("somefile.txt", 31, "23:59")
Pour plus d’informations sur os.date
consultez https://www.lua.org/pil/22.1.html