Prevent output buffering with PHP and Apache

前端 未结 3 385
旧巷少年郎
旧巷少年郎 2020-12-10 06:45

I have a PHP script which sends a large number of records, and I want to flush each record as soon as it is available: the client is able to process each record as it arrive

3条回答
  •  误落风尘
    2020-12-10 07:28

    You don't mention what web server you are using, but I am going to go out on a limb here and guess Apache2. I hit almost the identical thing you describe. I was trying to get my cgi script to pass back information as it had it ready, instead of buffering the whole thing. Worked jiffy in curl, etc., but buffered in a browser (pretty much any browser), which was at least maddening. I went through the exact steps you describe. The resolution in my case was to modify sites-enabled/terrifico.com configuration file in Apache2 (the line in question starts with

    SetEnvIfNoCase

    (You can ignore the stuff above and below that line, I'm just showing it for reference of where I placed it.)

    
    ServerAdmin webmaster@localhost
    ServerName test.terrifico.com
    ServerAlias test.terrifico.com
    
    SetEnvIfNoCase Request_URI \.cgi$ no-gzip dont-vary
    
    DocumentRoot /var/www/test.terrifico.com
    

    From staring at the network traffic going back and forth, it finally dawned on me that the browser was advertising that it accepted deflation for anything (that was text). That was the difference between the browser and curl, for example. The salient bit was

    Accept-Encoding:gzip,deflate,sdch

    There was a bit about chunking, but that didn't impact this particular problem. So, the browser was requesting mod_deflate to kick in, which defeated my carefully spewing out bytes as I got them in my cgi script. You could change it in the browser, but it seemed more sensible to change it on the server once for the works.

    Perhaps this helps.

提交回复
热议问题