【问题标题】:Upload large file sizes error H18 Heroku PHP Laravel request interrupted上传大文件大小错误 H18 Heroku PHP Laravel 请求中断
【发布时间】:2021-01-28 11:18:56
【问题描述】:

我不明白如何在上传超过 20mb 的文件时发送响应以避免 Heroku 的服务器错误。

我正在使用 PHP 7.4/Laravel 8.x 并且正在接收,

heroku[router]: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST

H18 表示套接字已连接,一些数据已作为应用程序响应的一部分发送,但随后套接字在未完成响应的情况下被销毁。

由于 Heroku 的 30 秒请求限制,它提前终止。

我已经配置了我的 .user.ini 文件,

post_max_size = 100M
upload_max_filesize = 100M
max_input_time = 300 
max_execution_time = 300

我在 Procfile 中声明它,

web: vendor/bin/heroku-php-apache2 -i .user.ini public/

我接受表单中的多个文件,

<input style="display:none;" type="file" id="files" name="attachment[]" multiple required>
<label for="files" class="files-button" id="filesButton">Upload Images</label>

如果文件是图片,我上传它

ini_set('memory_limit', '-1'); // more memory

if($request->hasFile('attachment')) {
    $valid_exts = ['png', 'jpg', 'jpeg'];
        foreach ($files as $file) {
            $extension = $file->getClientOriginalExtension();
            if (in_array($extension, $valid_exts)) {
                Storage::disk('s3')->put("/model images/$url_name/",  $file);
            }
        }
    }
}

有什么办法可以解决这个 30 秒的强制超时?

【问题讨论】:

    标签: php laravel heroku upload php-ini


    【解决方案1】:

    我遇到了同样的问题。 正如您所说,连接被大型响应的一部分中断。

    通常,H18 表示响应具有多个阶段 - 对于 例如,流式传输大型响应的块 - 以及其中之一 阶段抛出了一个错误。 https://devcenter.heroku.com/articles/error-codes#h18-server-request-interrupted

    文档建议查看日志文件以确定响应的哪个阶段/块引发错误并修复该问题。

    至于配置超时,Heroku 说这是不可能的。

    超时值不可配置。如果您的服务器需要更长的时间 完成给定请求的时间超过 30 秒,我们建议移动 使用后台任务或工作人员定期 ping 您的服务器 查看处理请求是否已完成。这种模式释放 您的 Web 进程最多可以完成更多工作,并且总体上会减少 应用程序响应时间。 https://devcenter.heroku.com/articles/request-timeout#long-polling-and-streaming-responses

    并建议使用“直接”文件上传,将其从浏览器直接上传到 S3。 https://devcenter.heroku.com/articles/s3#file-uploads

    【讨论】:

    • 我最终不必实际实现这一点。但是是的,答案在于直接上传文件并在 S3 中正确配置 CORS 和其他权限。
    猜你喜欢
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 2018-11-11
    相关资源
    最近更新 更多