通过mod_proxy使用Apache和PHP-FPM禁用输出缓冲区 [英] Disabling Output Buffer with Apache and PHP-FPM via mod_proxy

查看:149
本文介绍了通过mod_proxy使用Apache和PHP-FPM禁用输出缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将Apache与PHP作为模块一起使用时,只要生成PHP,就立即输出内容,因为您只需在PHP中禁用output_buffering并使用flush()或hidden_​​flush(1).这是我以前使用过的,效果很好.

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.

自从切换到PHP-FPM以来,我遇到了一个问题,即在整个脚本完成之前,我无法让Apache(2.4)输出PHP的内容.我仍然关闭了output_buffering并进行了冲洗,但这还不够. Apache没有使用mod_gzip(反正也会影响PHP模块).

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提供了一个禁用proxy_buffering的选项,通过阅读其他人的评论可以解决此问题,但是我无法在Apache中找到任何实现此目的的方法.

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.

这是当前在Apache中调用PHP的方式:

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>

Apache文档中提到了flushpackets(上面使用过),这似乎是必需的,但随后它又说,它仅适用于AJS,不适用于所有代理内容,因此在此方面不做任何事情情况.

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.

简而言之:有人知道让Apache在回显后立即发送PHP内容而不是等到脚本完成后才发送PHP内容的正确方法吗?

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?

推荐答案

重新发布答案我刚刚在这里发布了一个非常类似的问题:如何通过apache2和mod_proxy_fcgi禁用缓冲?

Reposting the answer I just posted to a very similar question here: How to disable buffering with apache2 and mod_proxy_fcgi?

一些注意事项,因为我刚刚花了几个小时尝试寻找该问题的答案:

A few notes, since I just spent the past few hours experimenting to find the answer to this question:

  1. 使用mod_proxy/mod_proxy_fcgi时不可能完全禁用输出缓冲,但是,您仍然可以将响应分流传输.
  2. 根据我的实验,似乎块必须至少为4096字节才能将输出刷新到浏览器.
  3. 您可以 使用mod_fastcgimod_fcgi模块禁用输出缓冲,但是这些mod并不像Apache 2.4那样流行/广泛使用.
  4. 如果启用了mod_deflate并且未为virtualhost/directory/etc设置SetEnv no-gzip 1.那是流数据,那么gzip将不允许在请求完成之前刷新缓冲区.
  1. It's not possible to entirely disable output buffering when using mod_proxy/mod_proxy_fcgi, however, you can still have responses streamed in chunks.
  2. It seems, based on my experimentation, that chunks have to be at least 4096 bytes before the output will be flushed to the browser.
  3. You can disable output buffering with the mod_fastcgi or mod_fcgi module, but those mods aren't as popular/widely used with Apache 2.4.
  4. If you have mod_deflate enabled and don't set SetEnv no-gzip 1 for the virtualhost/directory/etc. that's streaming data, then gzip will not allow the buffer to flush until the request is complete.

我正在进行测试,以了解如何最好地使用Drupal 8的新BigPipe功能将请求流传输到客户端,并且我在

I was testing things out to see how to best use Drupal 8's new BigPipe functionality for streaming requests to the client, and I posted some more notes in this GitHub issue.

这篇关于通过mod_proxy使用Apache和PHP-FPM禁用输出缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆