How to replace string in reverse proxy HTTP request using Lua & nginx

You can use access_by_lua_block in order to replace arbitrary strings in an nginx request. Note: This will replace in the request to the reverse proxy, not in the response from the reverse proxy. See How to replace HTTP response data in nginx using lua for a Lua-based approach on how to replace a string in the response.

access_by_lua_block {
    ngx.req.read_body()
    local body = ngx.req.get_body_data()
    if body then
        body = ngx.re.gsub(body, "string_to_replace", "string_by_which_to_replace")
    end
    ngx.req.set_body_data(body)
}

This is typically used in a location {} block so you can just replace when a specific set of URLs is accessed:

location /config {
   proxy_pass http://127.0.0.1:12345/;
    proxy_set_header X-Forwarded-For $remote_addr;

    access_by_lua_block {
       ngx.req.read_body()
       local body = ngx.req.get_body_data()
       if body then
            body = ngx.re.gsub(body, "myvar", "myothervar")
       end
       ngx.req.set_body_data(body)
    }
}