Azure 上的 .NET Core 2.0 导致 502.3 CGI 错误

.NET Core 2.0 on Azure results in a 502.3 CGI Error

过去几天,我试图找到以下错误消息的答案,这些错误消息困扰着我在 Azure Web 应用程序托管上的 .NET Core 2.0 应用程序:

This error occurs when a CGI application does not return a valid set of HTTP headers, or when a proxy or gateway was unable to send the request to a parent gateway. You may need to get a network trace or contact the proxy server administrator, if it is not a CGI problem.

因为花了几天时间才找到答案,所以我在这里发布我的解决方案。

除了CGI错误,我还在错误日志中发现了以下错误代码:0x80072ee2。这表示请求超时。但是,Azure 的超时限制为 4 分钟,错误在大约 1.5 分钟后出现。该应用程序旨在处理 4 分钟内的请求,因此不应发生超时错误。

我在阅读以下页面后找到了解决方案:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/?tabs=aspnetcore2x。在这个页面中IIS之间的关系 并解释了 Kestrel 引擎。尽管 Web 应用程序托管应该与 IIS 托管不同,但我怀疑 Azure 使用了很多 IIS 模块,因为它们有很多相似之处。

github 上的以下问题也解决了我的问题:https://github.com/aspnet/IISIntegration/issues/311. In this issue it is explained that next to IIS, Kestrel also has a timeout limit of 2 minutes. I completely missed that bit of information when searching for a solution at first. Changing the timeout of the Kestrel server (which can be found on the following page: https://docs.microsoft.com/en-us/aspnet/core/hosting/aspnet-core-module) 解决了我的问题并让请求 运行 在 2.5 分钟内完成。

我后来用 Chrome 开发人员工具检查了超时显示之前的确切持续时间,它似乎正好是 2 分钟,这应该是我的问题是 Kestrel 服务器超时的更早线索.