feat: rewrite Location headers from origin to point at a mirror

This commit is contained in:
Iain Learmonth 2025-04-28 15:39:57 +01:00
parent 09b178dca8
commit d9b8b1b364

View file

@ -1,3 +1,4 @@
-- Determine if we rewrite the body of this type of content
if ngx.header["Content-Type"] then if ngx.header["Content-Type"] then
local content_type = ngx.header["Content-Type"] local content_type = ngx.header["Content-Type"]
if content_type:find("text/html") or if content_type:find("text/html") or
@ -12,3 +13,20 @@ if ngx.header["Content-Type"] then
ngx.header["Content-Length"] = nil ngx.header["Content-Length"] = nil
end end
end end
-- Rewrite any redirects from the origin to point at a mirror
if ngx.header["Location"] then
if ngx.ctx.jasima_pool_map and not ngx.ctx.jasima_config.rewrite_disable then
local location = ngx.header["Location"]
for from, to in pairs(ngx.ctx.jasima_pool_map) do
if ngx.ctx.jasima_config.rewrite_case_insensitive then
local pattern = ngx.re.escape(from)
location = ngx.re.gsub(location, pattern, to, "ijo")
else
local pattern = from:gsub("([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") -- escape Lua patterns
location = location:gsub(pattern, to)
end
end
ngx.header["Location"] = location
end
end