Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Output Buffer with Apache and PHP-FPM via mod_proxy

Tags:

php

apache

Outputting content as soon as PHP generates it is fine when using Apache with PHP as a module as you can simply disable output_buffering in PHP and use flush() or implicit_flush(1). This is what I previously used and it worked fine.

I'm running into an issue since having switched to PHP-FPM wherein I cannot get Apache (2.4) to output PHP's content until the entire script has completed. I still have output_buffering off and flushing in place but that's not enough. Apache isn't using mod_gzip (and that would have affected both the PHP module as well anyway).

Nginx has an option to disable proxy_buffering which, from reading other people's comments fixes this, but I cannot find any way of doing this in Apache.

Here's how PHP is currently being called within Apache:

<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost/"
</FilesMatch>

<Proxy fcgi://localhost/ enablereuse=on retry=0 timeout=7200 max=500 flushpackets=on>
</Proxy>

The Apache documentation mentions flushpackets (used above) which appears to be what is needed, but then it also goes on to say that it only applies to AJS for now, not all proxied content so it won't do anything in this case.

Echoing enough whitespace to fill the buffer may work, but it's a messy workaround which is far from ideal.

In short: Does anyone know the correct way of having Apache send PHP content as soon as it's echo'd rather than waiting until script completion?

like image 768
Enverex Avatar asked Nov 10 '15 11:11

Enverex


1 Answers

I successfully disabled output buffering by rewriting your Proxy section (based on this answer):

<FilesMatch \.php$>
    SetHandler "proxy:unix:/run/php-fpm/php-fpm.sock|fcgi://localhost"
</FilesMatch>

<Proxy fcgi://localhost>
    ProxySet enablereuse=on flushpackets=on
</Proxy>
like image 179
vholten Avatar answered Sep 30 '22 16:09

vholten