various body filter questions

Robert Lemmen robertle at
Thu Jul 19 09:48:59 UTC 2012

hi everyone,

I need to run an nginx with a custom body filter, for the sake of
argument let's say I want to replace "robert" with "bob" in every file
that ends with ".txt". I played around a bit and looked at the various
tutorials and documentation on the net. I have managed to get it
basically working, but am looking for your input to get it "correct":

a) I create a module context and store it via ngx_http_set_ctx(). how do
I clean it up? or do I not need to? does everything allocated with
ngx_pcalloc(r->pool, ...) get freed when the request is sorted?

b) I would like to handle HEAD requests as well, but I can't just do it
with a filter. In the header filter I don't have the actual data in the
file, so I can't do the substitutions, so I don't know what the
content-length will be. what to do? I was thinking doing a GET
sub-request and then simply just return the headers. which is a bit
wastefull but might be the only way to determine the content-length.
would that be the way to go? if so, then it seems wastefull to first do
the HEAD request and then in the filter of the response do a subrequest
and ignore the response to the HEAD. can I make it not do the first
request at all? woudl that mean it needs to be a handler rather than a
a filter? somewhere on the web I read that subrequest can only be done
from filters...

c) in the body filter, I want to read the buffer chain and modify it or
create a new one. but I don't get all the details of the buffer chain.
assuming "ngx_chain_t *in" as a parameter of the filter:
- if in->next != 0 (and so on), does that mean these buffers are parts
  of the actual file? so e.g. in->buf could hold the first half of teh
  file and in->next->buf the second half?
- what are the semantics of the different fields in in->buf? what's the
  difference between pos/last and start/end? what do the different flags
  (temporary, memory, mmap, recycled, in_file, flush, sync) mean?
- what's the difference between last_buf, last_in_chain? is it not the
  same as in->next == 0?
- it might be easier for me to read the buffers and create new ones,
  rather than changing the existing ones. do I need to do any sort of
- are the buffers always complete? how would I know? I assume that if I
  download a 10GB file nginx doesn't just copy it into memory...
- how does sendfile work in nginx? if I do a body filter I of course
  need to make sure sendfile is never used.

thanks  robert

Robert Lemmen                      
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <>

More information about the nginx-devel mailing list