【问题标题】:502 bad gateway when running cron job运行 cron 作业时出现 502 错误网关
【发布时间】:2019-12-31 06:56:30
【问题描述】:

我在 laravel forge 服务器上部署了一个 codeIgniter 项目。 Cron 作业计划每晚运行。但是当 cron 作业运行时,它会给出

HTTP request sent, awaiting response... 502 Bad Gateway 2019-08-27 06:03:54 ERROR 502: Bad Gateway.

此错误大约在 60-70 秒后出现。 Cron 作业应该向一群用户(50-70 个用户)发送电子邮件提醒。我发送电子邮件的 cron.php 类的代码是

foreach($reminders as $job) {

        $job_ids[]=$job->id;

        $email_message=$this->load->view('email_templates/job_reminder', array('job'=>$job), TRUE);
        $message=$this->load->view('email_templates/template', array('content'=>$email_message, 'header_image'=>'header_proof_reminder'), TRUE);
        $subject='blablabla: '.format_job_name($job->id, $job->job_number, $job->name).' is still awaiting approval';

        $sent = $this->send_job_reminder_emails($job, $subject, $message);

        if($sent){ //Log if an email was sent
            $this->debug_string .= "<p>Email sent for job: ".$job->id."</p>";
        }else{
            $this->debug_string .= "<p>Didn't send email for job: ".$job->id."</p>";
        }
        $this->debug_string .= "<p><br/></p>";

    }

send_job_reminder_emails 函数是发送电子邮件的函数。 每次 cron 运行时都会发送大约 20,30 封电子邮件,但之后由于我认为的 502 错误而停止发送电子邮件。

我用于 cron 的命令是 wget https://mysite.url/cron/send_reminders

php-fpm 设置为:

max_execution_time = 1000
max_input_time = 200
memory_limit = 512M

在 nginx.conf 中keepalive_timeout=300

site-nginx.cong

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_read_timeout 600;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 600;
    fastcgi_send_timeout 600;
}

为了测试,我只是在服务器上手动运行这个 cron 命令

我不确定是什么导致了这个问题。是 fpm 还是 nginx 问题,还是我应该在代码库中以某种方式处理它。

谢谢

【问题讨论】:

    标签: php codeigniter nginx cron forge


    【解决方案1】:

    不要通过网络代理使用长时间的操作。 使用控制台命令,没有网关也没有超时。

    对于 cron,使用与 php-fpm 相同的用户。

    /path/to/bin/php -f /path/to/cron/app-reminder-wrapper.php
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-08
      • 2016-10-24
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多