Skip to content

Instantly share code, notes, and snippets.

@titpetric
Created November 2, 2016 11:00
Show Gist options
  • Select an option

  • Save titpetric/ed6ec548af160e82c650cf39074878fb to your computer and use it in GitHub Desktop.

Select an option

Save titpetric/ed6ec548af160e82c650cf39074878fb to your computer and use it in GitHub Desktop.
Delete NGINX cached items from a PURGE request
-- Tit Petric, Monotek d.o.o., Thu 27 Oct 2016 10:43:38 AM CEST
--
-- Delete nginx cached assets with a PURGE request against an endpoint
--
local md5 = require 'md5'
function file_exists(name)
local f = io.open(name, "r")
if f~=nil then io.close(f) return true else return false end
end
function explode(d, p)
local t, ll
t={}
ll=0
if(#p == 1) then return {p} end
while true do
l=string.find(p, d, ll, true) -- find the next d in the string
if l~=nil then -- if "not not" found then..
table.insert(t, string.sub(p, ll, l-1)) -- Save it in our array.
ll=l+1 -- save just after where we found it for searching next time.
else
table.insert(t, string.sub(p, ll)) -- Save what's left in our array.
break -- Break at end, as it should be, according to the lua manual.
end
end
return t
end
function cache_filename(cache_path, cache_levels, cache_key)
local md5sum = md5.sumhexa(cache_key)
local levels = explode(":", cache_levels)
local filename = ""
local index = string.len(md5sum)
for k, v in pairs(levels) do
local length = tonumber(v)
-- add trailing [length] chars to index
index = index - length;
filename = filename .. md5sum:sub(index+1, index+length) .. "/";
end
if cache_path:sub(-1) ~= "/" then
cache_path = cache_path .. "/";
end
filename = cache_path .. filename .. md5sum
return filename
end
function purge(filename)
if (file_exists(filename)) then
os.remove(filename)
end
end
if ngx ~= nil then
local cache_key = ngx.var.lua_purge_upstream .. ngx.var.request_uri
local filename = cache_filename(ngx.var.lua_purge_path, ngx.var.lua_purge_levels, cache_key)
purge(filename)
ngx.say("OK")
ngx.exit(ngx.OK)
end
@Jijun
Copy link
Copy Markdown

Jijun commented Jun 7, 2018

great work! but i made some change

local md5 = require 'md5' local md5sum = md5.sumhexa(cache_key)
can be replaced by
local md5 = ngx.md5 local md5sum = md5(cache_key)
will never require module md5

@0xShamil
Copy link
Copy Markdown

Great work! Much thanks.

Noticed that the md5 of the proxy_cache_key does not match the file name when there is a Vary header present. Is it calculated differently in presence of Vary headers?

@titpetric
Copy link
Copy Markdown
Author

titpetric commented Apr 28, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment