nginx1.8.0+php-fpm5.3.29 页面502错误

Mailerm 发布于 2015/07/15 17:57
阅读 724
收藏 0

平台信息:

Ubuntu 14.04.1 LTS 1Ghz 1G

代码:

<?php
if (Application::getInstance()->input->get('code') == null) {  // Go To OAuth...
    header('Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . WECHAT_CORP_ID . '&scope=' . WECHAT_OAUTH_SCOP . '&redirect_uri=' . urlencode(XUtils::pageURL()) . '&response_type=code&state=' . session_id() . '#wechat_redirect');
    exit();
} else {
    if (Application::getInstance()->input->get('state') != session_id()) {
        Application::getInstance()->redirectError(403, '抱歉您没有访问权限');
        die();
    }
    $link = MsgSrv::open(MSGSRV_PLATFORM);
    $params = array(
        'type' => USER_TYPE_WECHAT, 
        'terminal' => 0, 
        'agent' => WECHAT_APP_AGENT,
        'sid'=> Application::getInstance()->input->get('code')
    );
    $content = 'v=1.0 s=' . session_id() . ' t=json i=' . XUtils::getIP() . ' d=' . base64_encode(json_encode($params));
    $response = msgsrv_request('WeChatServices', 'AuthenticateEx', $content, $link);
    // Bubble Response...
    $body = XUtils::splitArray($response['body'], ' ', '=');
    $result = $body['r'];
    if ($result != 1001) {
        Application::getInstance()->redirectError(403, '抱歉您没有访问权限');
        die();
    }
    // Login Success!
    $content = base64_decode($body['d']);
    $this->logger->info('[OAuth][WeChat][FIN][SUCCESS] - User:' . PHP_EOL . $content);
    $userinfo = json_decode($content);;
    $userinfo->application = WECHAT_CORP_ID;
    $userinfo->identity = $userinfo->account;
    $userinfo->attach = json_decode($userinfo->attach);
    $userinfo->department = json_decode($userinfo->attach->department);
    
    if (!in_array(48, $userinfo->department)) {
        $this->logger->warn('[OAuth][WeChat][REF][FIN] - User:' . PHP_EOL . json_encode($userinfo));
        Application::getInstance()->redirectError(403, '抱歉您没有访问权限');
        die();
    } else {
        $userinfo->merchant = '';
    }
    
    $_SESSION[SESSION_USER_INFO] = $userinfo;
    $this->logger->info('[OAuth][WeChat][FIN][DONE] - User:' . PHP_EOL . json_encode($userinfo));
    header('Location: ' . XUtils::pageURL());
    die();
}
PS: 

1.XUtils::pageURL() 为自定义方法用于获取当前页面URL。

2.Application::getInstance()->redirectError() 为跳转到指定模块执行。

问题现象:

页面偶然出现nginx错误,分析日志如下:

PHP-FPM输出日志:

==> /var/log/php-fpm/php-fpm.log <== [15-Jul-2015 17:31:35] WARNING: [pool www] child 17238 exited on signal 11 (SIGSEGV) after 2752.258454 seconds from start [15-Jul-2015 17:31:35] NOTICE: [pool www] child 18340 started

NGINX输出日志:

171.221.148.24 - - [15/Jul/2015:17:31:35 +0800] "GET /business/1.0/?code=8cb0e1dee18d282abadd2a1c8a6437f0&state=saakgfi39e1pb8mc2qggkclnu7 HTTP/1.1" 502 537 "-" "Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12H143 MicroMessenger/6.2.3 NetType/WIFI Language/zh_CN" "-"

PHP输出日志:

2015-07-15 17:31:35 UserViewOAuth INFO - [OAuth][WeChat][FIN][SUCCESS] - User: {"account":"joe"..........} 2015-07-15 17:31:35 UserViewOAuth INFO - [OAuth][WeChat][FIN][DONE] - User: {"account":"joe"..........}


尝试解决:

1. 根据日志分析log正常输出,调试发现执行到页面结束,然后数据返回nginx,nginx就502了。

2. 发生错误时刷新页面,正常登陆成功,说明SESSION记录成功。

3. 删除部分代码。

比如删除
    $userinfo->application = WECHAT_CORP_ID;
    $userinfo->identity = $userinfo->account;
    $userinfo->attach = json_decode($userinfo->attach);

页面正常执行。

加载中
返回顶部
顶部