Sendfile_max_chunk breaking unbufferred php-fcgi

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

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;

    do_write = do_write || u->length == 0;