Roman Arutyunyan
February 05, 2019 08:58AM
Hi Alex,

On Tue, Feb 05, 2019 at 07:29:19AM -0500, abmackenzie wrote:
> Hi all,
> I am attempting to develop an NGINX module which is required to modify the
> response headers based on the content of the response body, whether from an
> upstream proxy or being fulfilled by the server itself.
> My current approach, a body filter, does not seem fruitful. By the time the
> body filter is invoked the headers have already been sent. There is no way
> to generate the correct header without the response body. I cannot find any
> mechanism in the NGX API to postpone sending of the headers or access the
> response body in a header filter.
> I was hoping somebody could either point me in the direction of a solution
> (or confirm that what I am trying to do is impossible without modification
> of the NGINX source).

This is possible. There are two examples of this in the nginx source:
ngx_http_xslt_filter_module and ngx_http_image_filter_module.

In a nutshell, you need to register both a header filter and a body filter.
In the header filter you don't call the next filter in chain.
You only call it from the body filter once you're done with receiving and
caching the body. After calling the next header filter, you should output
all the body cached by your module by calling the next body filter.

Also notice the ngx_http_filter_finalize_request() call in the body filter.
Use this call to finalize the request in case of error if header is not yet

PS: it is better to send development questions to
mailing list

Roman Arutyunyan
nginx mailing list
Subject Author Posted

Modifying Headers by Analyzing Body

abmackenzie February 05, 2019 07:29AM

Re: Modifying Headers by Analyzing Body

Roman Arutyunyan February 05, 2019 08:58AM

Re: Modifying Headers by Analyzing Body

abmackenzie February 05, 2019 09:11AM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 81
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready