Send headers in output body filter ?

Maxim Dounin mdounin at mdounin.ru
Sat Mar 27 01:48:52 UTC 2021


Hello!

On Sat, Mar 27, 2021 at 12:56:16AM +0100, Alfred Sawaya wrote:

> Actually, I will need to modify the http status code depending on the
> output body contents, so it is not just a question of  content-length,
> it is more general than that : I need to have the full backend response
> (headers + body) in order to produce the full client response.
> 
> 
> It does not seem possible as I understand that the read upstream - write
> downstream loop is hardcoded into nginx, and a module does not have any
> chance to change that, isn't it ?

If you want to change the response status and headers based on the 
response body content, the only way is to buffer the response body 
and send the response headers after you've done processsing the 
response body.  Note that since this implies buffering, this is 
not going to work for large responses.

Examples of how to implement this can be seen in the image filter 
and xslt filter modules.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx-devel mailing list