如何在 nginx 中为 PHP 应用程序禁用输出缓冲 [英] How to disable output buffering in nginx for PHP application

查看:22
本文介绍了如何在 nginx 中为 PHP 应用程序禁用输出缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有类似的代码:

<?php
    ob_implicit_flush(true);
    ob_end_flush();

    foreach ($arrayOfStrings as $string) {
        echo time_expensive_function($string);
    }
?>

在 Apache 中,这会在输出时将每个回显发送到浏览器.然而,在 nginx/FastCGI 中,由于 nginx 的工作方式(默认情况下),这不起作用.

In Apache, this would send each echo to the browser as it was output. In nginx/FastCGI however, this doesn't work due tot he way nginx works (by default).

是否可以在 nginx/FastCGI 上进行这项工作,如果可以,如何进行?

Is it possible to make this work on nginx/FastCGI, and if so, how?

推荐答案

首先 php 必须正确刷新所有内容:

First php has to correctly flush everything :

@ob_end_flush();
@flush();

然后,我找到了两个可行的解决方案:

Then, I found two working solutions:

1) 通过 Nginx 配置:

1) Via Nginx configuration:

fastcgi_buffering off;

2) 通过 php 代码中的 HTTP 标头

2) Via HTTP header in the php code

header('X-Accel-Buffering: no');

这篇关于如何在 nginx 中为 PHP 应用程序禁用输出缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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