dongmao3148 2015-04-21 14:44
浏览 136

PHP / FastCGI错误500

I have a PHP script which takes a long time and makes many DB connections.

After about 5 minutes the server sends me an error 500 and the error_log shows the following :

(104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
(104)Connection reset by peer: mod_fcgid: ap_pass_brigade failed in handle_request_ipc function

I googled this and found that the obvious answer is to change either the "PHP_FCGI_MAX_REQUESTS" variable inside the "fcgi-bin/php5.fcgi" file for the domain or "FcgidMaxRequestsPerProcess" variable inside the Apache config file, but I can't do either of these, as the server hosts multiple websites, all running with FastCGI.

I tried to catch the error 500 and redirect the page with the following PHP code :

register_shutdown_function('rerun');

$rerun = isset($_GET['rerun']) ? true : false;
main($rerun);

function main ($rerun=false) {
    // Lots and lots of stuff
}
function rerun() {
    if (error_get_last() != NULL) {
        header('Location: http://www.example.com/myscript.php?rerun');
    }
}

But it doesn't work either, I still end up with an error 500.

Does anyone have any idea on how to fix this ? Either by declaring the FastCGI variables locally (i.e. for this website only), or, preferably, by correctly catching the error 500 and triggering the "rerun" function before it all crashes down.

  • 写回答

2条回答 默认 最新

  • duaner1634 2015-04-21 14:59
    关注

    I think it's because your script doesn't send any data to apache. And you got this error because of timeout of awaiting response by apache from fast-cgi. I have similar issue on nginx+php-fpm. I've solved it with this workaround:

    function explicitBufferFlush()
    {
        echo str_repeat('*', 1024 * 64);
        flush();
        ob_flush();
    }
    

    What it does it simply send * to output, and while your script is running apache recieve data and don't drop connection. You can call it on every iteration of some heavy loop or anything else. Or you can try to set FcgidIOTimeout directive to some high value in your .htaccess

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度