questions about proxy buffers

Maxim Dounin mdounin at
Sat Jun 11 06:08:39 MSD 2011


On Sat, Jun 11, 2011 at 01:50:59AM +0200, Xavier Noria wrote:

> I have read the documentation of the HTTP Proxy Module regarding
> buffers and would like to understand which is the exact (basic)
> workflow regarding buffers only. Let me ask about some scenarios,
> assuming everything fits in memory and ignoring caching workflows etc.

Just a side note: you may want to read this relatively recent 

> Let's suppose proxy_buffer_size is 4k and proxy_buffers is 8 4k.
> Say the proxied server sends a response of 2k. Then I guess the entire
> response is buffered, as quickly as possible, and when the proxied
> server is done, then nginx starts sending the buffer to the client. Is
> that right?

Basically, yes.  "Bascially" here assumes proxy_buffering on (the 
default), and "response" being interpreted as "response body", as 
header is processed separately and may be sent without buffering 
(this will be usually prevented by other options like 
postpone_output though).

> The description of proxy_buffer_size says "This directive set the
> buffer size, into which will be read the first part of the response,
> obtained from the proxied server." What is the *first* part of the
> response?

This is a buffer where response header will be read and parsed.  
It may also happen to contain some parts of response body as long 
as they were read from kernel along with header.

This buffer is also used in unbuffered mode, but this is 
completely different story.

> Does it mean the chunk that fits in the first buffer? In
> this case exactly 4k?


> If that's correct, what does the module do with
> the "subsequent" parts? does proxy_buffer_size have anything to do
> with them? Or are those for the remaining 7 buffers, 4k each?

Subsequent parts of the response are read to proxy_buffers.

> Let's say now the proxied server sends a response consisting of 16k.
> Does the module fill 4 buffers, as quickly as possible, and when
> everything is done it sends the 16k to the client? Or does it start
> sending the first buffer while filling the second one?

It will start sending as long as it has nothing to read for a 
while (i.e. read() from upstream will return EAGAIN).

> Here "nginx does not attempt to read the entire answer from the
> proxied server, the maximum size of data which nginx can accept from
> the server is set by directive proxy_buffer_size." should
> proxy_buffer_size be proxy_buffers, or rather a combination of the
> two?

There were a stray newline before the sentence in question, I just 
removed it from wiki.  It's about work with buffering switched 

> What happens if the proxied server sends a response greater than the
> total buffer sizes, say 50k? Does nginx frees buffers sending data,
> and then buffers another chunk from the proxied server, iterate until
> done?

Yes.  With some nuances though: as long as sending is not possible 
(client is slow) nginx will spool data to disk in an attempt to 
free backend as soon as possible.  Disk buffering may be limited 
(or even completely disabled) with proxy_max_temp_file_size 

> What's proxy_busy_buffers_size?

This directive specifies how many data may be in "busy" buffers, 
i.e. already passed to output filter chain but still not sent.  
It limits amount of busy buffers and leaves some buffers available 
for reading upstream response and buffering it to disk.

Maxim Dounin

More information about the nginx mailing list