# Flush FastCGI response early vs. GZIP

**URL:** <https://www.ruby-forum.com/t/flush-fastcgi-response-early-vs-gzip/221395>\
**Category:** NGINX\
**Created:** [July 12, 2012, 2:37pm UTC](https://www.ruby-forum.com/t/flush-fastcgi-response-early-vs-gzip/221395 "2012-07-12T14:37:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 12, 2012, 2:37pm UTC](https://www.ruby-forum.com/t/flush-fastcgi-response-early-vs-gzip/221395/1 "2012-07-12T14:37:11Z")

</div>

Sending the first few kilobytes of an HTML page to the client as early  
as possible enables the client to download stylesheets without having  
to wait for the whole HTML document (which might require database  
queries and can therefore be relatively slow). Loading the stylesheets  
early is important because the browser blocks rendering on  
stylesheets. Details are available here:

> **[Progressive rendering via multiple flushes](https://calendar.perfplanet.com/2009/progressive-rendering-via-multiple-flushes/)**
>
> Perceived page loading time is just as important as the real loading time. And when it comes to user perception, visible indication of progress is always good. The user gets feedback that something is going on (and in the right direction) and feels...

So what I would like to do is: 1) Send the element with its  
stylesheet references to the client immediately. 2) Afterwards query  
the database and generate the whole HTML page and send it to the  
client. Meanwhile the client had a chance to already fetch the  
stylesheets.

My problem is that nginx buffers FastCGI responses. I can only flush  
the first few kilobytes early if I configure nginx to use very small  
FastCGI buffers:

fastcgi\_buffer\_size 2k;

This does not seem to be a good idea in general because it might cause  
the “upstream sent too big header” error in other scripts.  
Additionally, the GZIP module has its own buffers that I’d have to  
shrink as well (which does not sound like a great idea in general).

Is it possible to handle this issue with an nginx feature that I have  
overlooked?

My idea would be a new internal header “X-Accel-Flush: [offset]” that  
FastCGI apps could send to nginx to flush the partial response after  
[offset] bytes. The GZIP module should obey this header, too (instead  
of continuing to buffer the response).

Thanks  
Steffen
