如何在浏览器中忽略来自 AWS ELB 的空闲超时

How to ignore idle timeout from AWS ELB in the browser

我有一个应用程序,用户可以在其中使用 angular-file-upload.js

上传 PDF

此库不支持文件分块:https://github.com/nervgh/angular-file-upload/issues/41

我的弹性负载均衡器配置为有 10 秒的空闲超时,应用程序的其他部分依赖于保留此参数。

问题是,如果文件上传时间超过 10 秒,用户会在浏览器中收到 504 网关超时和错误消息。但是,文件在一段时间后仍然到达服务器。

如何忽略或不向用户显示这个来自 ELB 的 504 网关超时?有其他方法可以解决这个问题吗?

您遇到的问题是 ELB 总是会关闭连接,除非它从您的服务器返回一些流量。见下文来自 AWS docs。这与 ALB 或 Classic 负载均衡器的行为相同。

By default, Elastic Load Balancing sets the idle timeout to 60 seconds for both connections. Therefore, if the instance doesn't send some data at least every 60 seconds while the request is in flight, the load balancer can close the connection. To ensure that lengthy operations such as file uploads have time to complete, send at least 1 byte of data before each idle timeout period elapses, and increase the length of the idle timeout period as needed.

所以要解决这个问题,您有两个选择:

  1. 更改服务器处理以在连接建立后立即开始发回一些数据,间隔小于 10 秒。
  2. 使用其他库进行上传,或使用 vanilla javascript。那里有很多例子,例如this one.

编辑:第三个选项 感谢@colde 提出了有效的观点,即您可以简单地完全解决负载均衡器问题。这样做的另一个好处是可以释放因长时间上传而占用的服务器资源。在我们的实施中,我们使用 pre-signed urls 来安全地实现这一点。