是否可以增加 CloudFlare 超时?

Is it possible to increase CloudFlare time-out?

是否可以增加 CloudFlare 的超时时间?如果是,如何?

我的代码需要一段时间才能执行,而且我不打算在未来几天对其进行 Ajax 化。

不,CloudFlare 只提供那种 customisation on Enterprise plans

  • 如果 15 秒后未能建立 HTTP 握手,CloudFlare 将超时。
  • 在您看到 524 timeout error.
  • 之前,CloudFlare 还将等待 100 秒以等待来自您的服务器的 HTTP 响应
  • 除此之外,您的原始 Web 服务器可能会超时。

听起来您需要 Inter-Process Communication. HTTP should not be used a mechanism for performing blocking tasks without sending responses, these kind of activities should instead be abstracted away to a non-HTTP service on the server. By using RabbitMQ(或任何其他 MQ),然后您可以将消息从服​​务器的 HTTP 元素传递到网络服务器上的处理服务。

我就同样的问题与 Cloudflare 进行了沟通,并与 RabbitMQ 的技术支持进行了沟通。

RabbitMQ 建议使用依赖于 Web 套接字的 Web Stomp。但是 Cloudflare 建议...

Websockets would create a persistent connection through Cloudflare and there's no timeout as such, but the best way of resolving this would be just to process the request in the background and respond asynchronously, and serve a 'Loading...' page or similar, rather than having the user to wait for 100 seconds. That would also give a better user experience to the user as well

更新:

为了完整起见,我也在这里记录一下 我还通过子域和 "grey-clouding" 询问了 CloudFlare 关于 运行 报告的问题,他们的回答如下:

I will suggest to verify on why it takes more than 100 seconds for the reports. Disabling Cloudflare on the sub-domain, allow attackers to know about your origin IP and attackers will be attacking directly bypassing Cloudflare.

进一步更新

我终于解决了这个问题,方法是 运行 使用线程报告,并使用 AJAX 来 "poll" 报告是否已创建。参见

我知道不能将其视为解决方案,但有两种方法可以避免这种情况。 1) 由于这个超时通常与长时间生成某些东西有关,这种类型的工作可以通过 crontab 完成,或者如果你可以访问 SSH,你可以 运行 一个 PHP 命令直接执行。在这种情况下,连接不是通过 Cloudflare 提供的,因此只要您的配置允许它 运行 就可以连接。检查 Google 如何从命令行 运行 脚本或如何使用 /usr/bin/php /direct/path/to/file.php

在 crontab 中确定它们

2) 您可以创建未添加到 cloudlflare 的子域,然后将您的脚本移到那里,然后 运行 它们直接通过 URL、Ajax 调用或其他方式。

Cloudflare 社区论坛对此有很好的回答:

If you need to have scripts that run for longer than around 100 seconds without returning any data to the browser, you can’t run these through Cloudflare. There are a couple of options: Run the scripts via a grey-clouded subdomain or change the script so that it kicks off a long-running background process and quickly returns a status which the browser can poll until the background process has completed, at which point the full response can be returned. This is the way most people do this type of action as keeping HTTP connections open for a long time is unreliable and can be very taxing also.

关于 Whosebug 的这个主题在 SERP 中排名很高,所以我决定写下这个答案,以供那些会觉得它有用的人使用。

Cloudflare 不会在超时时触发 504 错误

  • 504 是超时 由您的服务器触发 - 与 Cloudflare 无关。
  • 524 是由 Cloudflare 触发的超时。

参见:https://support.cloudflare.com/hc/en-us/articles/115003011431-Troubleshooting-Cloudflare-5XX-errors#502504error

524错误?有一个解决方法:

正如@mjsa 提到的,Cloudflare 只为企业客户端提供超时设置,这对大多数人来说不是一个选项。

但是,您可以通过将橙色云变成灰色来禁用该特定(子)域的 Cloudflare 代理:

之前:

之后:

Note: it will disable extra functionalities for that specific (sub)domain, including IP masking and SSL certificates.

正如 Cloudflare 在其文档中所述:

If you regularly run HTTP requests that take over 100 seconds to complete (for example large data exports), consider moving those long-running processes to a subdomain that is not proxied by Cloudflare. That subdomain would have the orange cloud icon toggled to grey in the Cloudflare DNS Settings . Note that you cannot use a Page Rule to circumvent Error 524.

如果您使用 curl 获取结果,您可以使用 resolve 选项直接访问您的 IP,而不是使用 Cloudflare 代理 IP:

例如:

curl --max-time 120 -s -k --resolve lifeboat.com:443:127.0.0.1 -L https://lifeboat.com/blog/feed 

https://support.cloudflare.com/hc/en-us/articles/115003011431-Troubleshooting-Cloudflare-5XX-errors#502504error

Cloudflare 524 错误源于网页需要 100 多秒才能完全响应。

这可以覆盖到(最多)600 秒...如果您更改为“Enterprise”Cloudflare 帐户。 Enterprise 的成本约为每年 4 万美元(需要年度合同)。