openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.35k stars 2.04k forks source link

Read proxied server response before sending the headers to the client #891

Open amreladawy opened 8 years ago

amreladawy commented 8 years ago

I am reopening this because I did not get a clear answer on my previous issue raised https://github.com/openresty/lua-nginx-module/issues/888

I am using openresty as a reverse proxy between a client and a service provider through the following steps

I am changing the headers coming from the client I convert the GET method to POST and create a body to be passed to the proxied server. The response coming back from the proxied server is then altered and sent to the client

The problem

As the final response sent back to the client is altered, I need to change the Content-Length to match the new content. I am able to read the response from the proxied server only in the body_filter_by_lua_file but at that stage, the headers are already sent.

In the header_filter_by_lua_file phase, I don't have access on the response ngx.args[1]

Currently, I am removing the Content-Length header making the response chunked which is causing a problem the client.

How can I send the correct content length back to the client?

williamgateslyj commented 8 years ago

You could use ngx.location.capture which is synchronous , when it returns you can get the body length: local len = #(res.body) ngx.header["Content-Length"] = len howerver there may be a out of memory problem when the upstream respone body is too large, in my case it is less than 1KB (hls live m3u8)

annakz commented 7 years ago

Hi, Did you find another solution for this issue? (without using ngx.location.capture) Thanks

Prasanna-sr commented 6 years ago

bump. is there any solution or over around for this ? Basically need to set headers based on response body from proxied upstream.

luksbelt commented 6 years ago

Hi @Prasanna-sr , i need to do the same thing, could you solve that? thanks.

Prasanna-sr commented 6 years ago

@luksbelt i ended up using "ngx.location.capture" as a work around for this

GumpSun commented 5 years ago

Hi, Is there any solution for this?