最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Disable output buffering for Apache with Nginx in proxy mode - Stack Overflow

programmeradmin6浏览0评论

I have a server ruled by Plesk running on Apache 2.4.41 and Nginx used in proxy mode. I also have ModSecurity enabled with Comodo rules (free). I use PHP 7.4.3 (FPM served by Apache) for back-end scripts with output buffering in my "php.ini" file set as follows output_buffering = 8192.

I'm trying to flush data as soon as they're "echo"ed but everything I tried failed so long.

I have already checked this topic and it didn't help.

I've created the following script for testing purpose:

ob_start();

$respond = function() {
    while(ob_get_level()) ob_end_flush();flush();
};

echo "1\n";
$respond();sleep(2);

echo "2\n";
$respond();sleep(2);

echo "3\n";
$respond();sleep(2);

When I run the above script, it waits for the script to complete before outputting the result.

I have tried to set specific directives for Nginx related to my script as follows

location = /path/to/my/script.php {
    proxy_buffering off;
    proxy_request_buffering off;
    chunked_transfer_encoding on;
    fastcgi_request_buffering off;
    fastcgi_buffering off;
    tcp_nodelay on;
}

I have also added special directives for Apache like so

<Location /path/to/my/script.php>
    SetEnv no-gzip 1
    SetEnv dontlog 1
    SetEnv proxy-nokeepalive 1
</Location>

I tried with PHP the following:

ini_set('output_buffering', '0');
ini_set('zlib.output_compression', '0');
ob_implicit_flush(true);

I've checked using a terminal to execute a similar script with PHP-Cli and it was being displayed every time it was "echo"ed so the problem is not at PHP level.

How to disable buffering in this case to allow data streaming?

One additional note, I don't want to disable buffering for my whole server but only for a specif script. The script is not accessible via a URL but used internally by the application. By the way, I don't mind if buffering is disabled for the URL using that script if no other solution exists.

发布评论

评论列表(0)

  1. 暂无评论