ngx.log(ngx.ERR, "Cache for ", ngx.var.request_uri, " has been purged due to error code 1059")
else
ngx.log(ngx.ERR, "Failed to purge cache for ",cache_file_path, " ", upstream_url)
end
end
end
end
}
header_filter_by_lua_block {
if ngx.var.upstream_cache_status == "HIT" then
ngx.log(ngx.ERR, "Cache hit, removing Cache-Control header for request: ", ngx.var.request_uri)
ngx.header["Cache-Control"] = nil
end
}
body_filter_by_lua_block {
local chunk = ngx.arg[1]
local eof = ngx.arg[2]
if ngx.ctx.response_body == nil then
ngx.ctx.response_body = chunk
else
ngx.ctx.response_body = ngx.ctx.response_body .. chunk
end
if eof then
local body = ngx.ctx.response_body
local body_length = #body
if body_length < 512 then
local json = require "cjson"
local data = json.decode(body)
if data and data.code == 1059 then
local upstream_scheme = "https"
local upstream_host = "api.xxxx.com"
local new_url = ngx.var.request_uri:gsub("^/api", "")
local upstream_url = upstream_scheme .. "://" .. upstream_host .. new_url
local cache_key = ngx.md5(upstream_url)
local cache_dir = "/var/cache/nginx/"
local subdir1 = string.sub(cache_key, -1)
local subdir2 = string.sub(cache_key, -3, -2)
local cache_file_path = cache_dir .. subdir1 .. "/" .. subdir2 .. "/" .. cache_key
local res = os.remove(cache_file_path)
if res then
ngx.log(ngx.ERR, "Cache for ", ngx.var.request_uri, " has been purged due to error code 1059")
else
ngx.log(ngx.ERR, "Failed to purge cache for ",cache_file_path, " ", upstream_url)
end
end
end
end
}
header_filter_by_lua_block {
if ngx.var.upstream_cache_status == "HIT" then
ngx.log(ngx.ERR, "Cache hit, removing Cache-Control header for request: ", ngx.var.request_uri)
ngx.header["Cache-Control"] = nil
end
}
body_filter_by_lua_block {
local chunk = ngx.arg[1]
local eof = ngx.arg[2]
if ngx.ctx.response_body == nil then
ngx.ctx.response_body = chunk
else
ngx.ctx.response_body = ngx.ctx.response_body .. chunk
end
if eof then
local body = ngx.ctx.response_body
local body_length = #body
if body_length < 512 then
local json = require "cjson"
local data = json.decode(body)
if data and data.code == 1059 then
local upstream_scheme = "https"
local upstream_host = "api.xxxx.com"
local new_url = ngx.var.request_uri:gsub("^/api", "")
local upstream_url = upstream_scheme .. "://" .. upstream_host .. new_url
local cache_key = ngx.md5(upstream_url)
local cache_dir = "/var/cache/nginx/"
local subdir1 = string.sub(cache_key, -1)
local subdir2 = string.sub(cache_key, -3, -2)
local cache_file_path = cache_dir .. subdir1 .. "/" .. subdir2 .. "/" .. cache_key
local res = os.remove(cache_file_path)
if res then
ngx.log(ngx.ERR, "Cache for ", ngx.var.request_uri, " has been purged due to error code 1059")
else
ngx.log(ngx.ERR, "Failed to purge cache for ",cache_file_path, " ", upstream_url)
end
end
end
end
}
发表评论