# Sendfile\_max\_chunk breaking unbufferred php-fcgi

**URL:** <https://www.ruby-forum.com/t/sendfile-max-chunk-breaking-unbufferred-php-fcgi/242309>\
**Category:** NGINX\
**Created:** [June 14, 2015, 3:52pm UTC](https://www.ruby-forum.com/t/sendfile-max-chunk-breaking-unbufferred-php-fcgi/242309 "2015-06-14T15:52:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [June 14, 2015, 3:52pm UTC](https://www.ruby-forum.com/t/sendfile-max-chunk-breaking-unbufferred-php-fcgi/242309/1 "2015-06-14T15:52:45Z")

</div>

Hi,

I was having the problem that if a single client on the local LAN is  
downloading a large static file, the download is effectively  
monopolizing  
nginx, and no other requests are handled simultaneously.  
Reading the manual I came across the sendfile\_max\_chunk option that  
sounded  
like that may fix it:

==  
Syntax: sendfile\_max\_chunk size;  
Default: sendfile\_max\_chunk 0;  
Context: http, server, location

# When set to a non-zero value, limits the amount of data that can be transferred in a single sendfile() call. Without the limit, one fast connection may seize the worker process entirely.

However I noticed that if I enable that, PHP scripts running without  
buffering suddenly no longer work properly.

nginx.conf:

==  
events {  
worker\_connections 1024;  
}

http {  
include mime.types;  
default\_type application/octet-stream;

```
server {
    listen 80;
    server_name $hostname;
    sendfile on;
    sendfile_max_chunk 8192;

    root /var/www;

    location / {
        index index.php index.html index.htm;
    }

    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_buffering off;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        include fastcgi.conf;
    }
}

```

# }

t2.php for testing purposes:

==

\<?php for ($i = 0; $i \< 10; $i++) { echo "test!\n"; flush(); sleep(1); } == When retrieving that, the connection stalls after the first flush: == $ telnet 192.168.178.26 80 Trying 192.168.178.26... Connected to 192.168.178.26. Escape character is '^]'. GET /t2.php HTTP/1.0 HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Sun, 14 Jun 2015 13:21:53 GMT Content-Type: text/html; charset=UTF-8 Connection: close X-Powered-By: PHP/5.6.9 test! == If I remove either the "sendfile\_max\_chunk 8192;" or "fastcgi\_buffering off;" line it does work, and I do get all 10 test! messages: == telnet 192.168.178.26 80 Trying 192.168.178.26... Connected to 192.168.178.26. Escape character is '^]'. GET /t2.php HTTP/1.0 HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Sun, 14 Jun 2015 13:22:23 GMT Content-Type: text/html; charset=UTF-8 Connection: close X-Powered-By: PHP/5.6.9 test! test! test! test! test! test! test! test! test! test! Connection closed by foreign host. == Am I doing something wrong, or is this a bug? Posted at Nginx Forum: http://forum.nginx.org/read.php?2,259603,259603#msg-259603

---

<div class="post-metadata">

**Author:** ![Floris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/a3d4f5/32.png) [@Floris](https://www.ruby-forum.com/u/Floris)\
**Post date:** [June 15, 2015, 12:40pm UTC](https://www.ruby-forum.com/t/sendfile-max-chunk-breaking-unbufferred-php-fcgi/242309/2 "2015-06-15T12:40:03Z")

</div>

On Sunday 14 June 2015 09:52:26 Floris wrote:

> Default: sendfile\_max\_chunk 0;
> 
> ```
> default_type application/octet-stream;
> index index.php index.html index.htm;
> 
> ```
> 
> }  
> echo “test!\n”;  
> Connected to 192.168.178.26.  
> test!  
> GET /t2.php HTTP/1.0  
> test!  
> Am I doing something wrong, or is this a bug?

Yes, it’s a bug.

You should set “sendfile\_max\_chunk 0;” in the location with unbuffered  
fastcgi.

Or you can try the following patch:

diff -r c041f1e0655f src/http/ngx\_http\_upstream.c  
— a/src/http/ngx\_http\_upstream.c Wed Jun 10 19:18:20 2015 +0300  
+++ b/src/http/ngx\_http\_upstream.c Mon Jun 15 13:32:55 2015 +0300  
@@ -3303,6 +3303,9 @@ ngx\_http\_upstream\_process\_non\_buffered\_r  
downstream = r-\>connection;  
upstream = u-\>peer.connection;

- /\* workaround for sendfile\_max\_chunk \*/

- downstream-\>write-\>delayed = 0;

- b = &u-\>buffer;
