I've done very similar things with Varnish reverse caching proxy. And as a bonus, Varnish is crazy fast if your pages or assets are cacheable.
Its scripting language is very simple (not quite as nice as Lua, IMHO), but it supports multiple backends and rewriting arbitrary headers: https://www.varnish-cache.org/trac/wiki/VCLExamples