# Proxy module buffering and timeouts

**URL:** <https://www.ruby-forum.com/t/proxy-module-buffering-and-timeouts/243254>\
**Category:** NGINX\
**Created:** [September 4, 2015, 2:43pm UTC](https://www.ruby-forum.com/t/proxy-module-buffering-and-timeouts/243254 "2015-09-04T14:43:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [September 4, 2015, 2:43pm UTC](https://www.ruby-forum.com/t/proxy-module-buffering-and-timeouts/243254/1 "2015-09-04T14:43:48Z")

</div>

Given the following nginx config:

* * *

master\_process off;

daemon off;

events {  
worker\_connections 16384;  
}

http {  
error\_log stderr debug;  
access\_log on;  
log\_not\_found on;  
client\_body\_buffer\_size 64k;  
client\_body\_in\_single\_buffer on;

upstream nodes {  
server 127.0.0.1:8000 max\_fails=0;  
server 127.0.0.1:8001 max\_fails=0;  
server 127.0.0.1:8002 max\_fails=0;

```
keepalive 16384;

```

}

server {  
listen \*:7070 backlog=16384 reuseport;  
keepalive\_requests 2147483647;

```
location /demo {
  proxy_pass http://nodes;
  proxy_connect_timeout 1ms;
  proxy_read_timeout 1ms;
  proxy_send_timeout 1ms;
  proxy_buffering on;
}

```

## } }

When requesting nginx server I get either the response from the upstream  
server:

curl -i [http://localhost:7070/demo](http://localhost:7070/demo)

HTTP/1.1 200 OK  
Server: nginx/1.9.4  
Date: Fri, 04 Sep 2015 12:33:00 GMT  
Content-Type: text/plain  
Content-Length: 27  
Connection: keep-alive

{“message”: “Hello World!”}

or a timeout response:

curl -i [http://localhost:7070/demo](http://localhost:7070/demo)

HTTP/1.1 504 Gateway Time-out  
Server: nginx/1.9.4  
Date: Fri, 04 Sep 2015 12:24:34 GMT  
Content-Type: text/html  
Content-Length: 182  
Connection: keep-alive

504 Gateway Time-out
# 504 Gateway Time-out

* * *
nginx/1.9.4

but also i sometimes randomly get partially cut responses:

curl -i [http://localhost:7070/demo](http://localhost:7070/demo)

HTTP/1.1 200 OK  
Server: nginx/1.9.4  
Date: Fri, 04 Sep 2015 12:24:35 GMT  
Content-Type: text/plain  
Content-Length: 27  
Connection: keep-alive

curl: (18) transfer closed with 27 bytes remaining to read

How this can be fixed? Since proxy buffering is on, i expect nginx  
always  
return either 502 error page on upstream timeout or a response from an  
upstream.

Here’s a simple nodejs script of upstream nodes to reproduce the case:

var http = require(‘http’);  
var util = require(‘util’);  
var cluster = require(‘cluster’);

var SERVER\_COUNT = 8;  
var HELLO\_WORLD = ‘“message”: “Hello World!”}’;  
var LOCALHOST = ‘127.0.0.1’;  
var PORT = 8000;

function simple(request, response) {  
response.writeHead(200, {  
‘Content-Type’: ‘text/plain’,  
‘Content-Length’: HELLO\_WORLD.length + 1  
});  
response.write(‘{’);  
response.end(HELLO\_WORLD);  
}

function createServer(port, handler) {  
http.createServer(handler).listen(port, LOCALHOST);  
util.log(util.format(‘Server running at http://%s:%d/’, LOCALHOST,  
port));  
}

if (cluster.isMaster) {  
for (var c = 0; c \< SERVER\_COUNT; c++) {  
cluster.fork({  
port: PORT + c  
})  
}  
} else {  
createServer(process.env.port, simple);  
}

Posted at Nginx Forum:

> **[Proxy module buffering and timeouts](https://forum.nginx.org/read.php?2,261424,261424#msg-261424)**
>
> Given the following nginx config: --- master\_process off; daemon off; events { worker\_connections 16384; } http { error\_log stderr debug; access\_log on; log\_not\_found on; client\_bod

---

<div class="post-metadata">

**Author:** ![donatasm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@donatasm](https://www.ruby-forum.com/u/donatasm)\
**Post date:** [September 4, 2015, 3:59pm UTC](https://www.ruby-forum.com/t/proxy-module-buffering-and-timeouts/243254/2 "2015-09-04T15:59:10Z")

</div>

Hello!

On Fri, Sep 04, 2015 at 08:42:54AM -0400, donatasm wrote:

[…]

> curl: (18) transfer closed with 27 bytes remaining to read
> 
> How this can be fixed?

By fixing your backend to return a full response or not return it  
at all.

> Since proxy buffering is on, i expect nginx always  
> return either 502 error page on upstream timeout or a response from an  
> upstream.

No, this is wrong expectation. Buffering means that nginx will  
avoid doing an extra work for partially filled body buffers, and  
it will be allowed to buffer parts of a response in the filter  
chain. No attempt will be made to obtain a full response and  
check it’s length (and such a behaviour is not at all possible if  
the response is big enough). As long as a response header is  
received from an upstream server, it will be passed to the client,  
and then nginx will start proxying the response body. An error  
can be returned only if the header was not yet passed to the  
client.

–  
Maxim D.

> **[nginx](http://nginx.org/)**

---

<div class="post-metadata">

**Author:** ![donatasm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9fc348/32.png) [@donatasm](https://www.ruby-forum.com/u/donatasm)\
**Post date:** [September 4, 2015, 4:29pm UTC](https://www.ruby-forum.com/t/proxy-module-buffering-and-timeouts/243254/3 "2015-09-04T16:29:32Z")

</div>

Ok, thanks!

Posted at Nginx Forum:

> **[Re: Proxy module buffering and timeouts](https://forum.nginx.org/read.php?2,261424,261428#msg-261428)**
>
> Given the following nginx config: --- master\_process off; daemon off; events { worker\_connections 16384; } http { error\_log stderr debug; access\_log on; log\_not\_found on; client\_bod
