Azure 函数应用返回 502 Bad Gateway

Posted

技术标签:

【中文标题】Azure 函数应用返回 502 Bad Gateway【英文标题】:Azure function app returning 502 Bad Gateway 【发布时间】:2018-03-16 22:54:35 【问题描述】:

当我在主插槽上运行 HTTP 触发函数应用程序时,我收到以下错误消息:

 Status: 502 Bad Gateway
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>502 - Web server received an invalid response while acting as a gateway or proxy server.</title>
<style type="text/css">
<!--
bodymargin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;
fieldsetpadding:0 15px 10px 15px; 
h1font-size:2.4em;margin:0;color:#FFF;
h2font-size:1.7em;margin:0;color:#CC0000; 
h3font-size:1.2em;margin:10px 0 0 0;color:#000000; 
#headerwidth:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;
#contentmargin:0 0 0 2%;position:relative;
.content-containerbackground:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>502 - Web server received an invalid response while acting as a gateway or proxy server.</h2>
  <h3>There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.</h3>
 </fieldset></div>
</div>
</body>
</html>

但是当我尝试使用完全相同的代码创建一个新的插槽和函数应用程序时,它可以正常工作而不会出现上述错误。似乎我们的主插槽配置存在问题,但我找不到任何资源来指向它。

还有其他人遇到过这个问题吗?你怎么修好它的?

【问题讨论】:

您是否在主插槽上设置了授权?在某些情况下,当配置错误时,可能会导致这种情况。您能否直接或indirectly 分享您的函数应用名称(即app 名称,而不是函数名称!)?这将有助于我们进行调查。 嗨@david,感谢您的回复。我们遇到问题的函数应用名称 2017-10-06T04:11:48.364 Function completed (Success, Id=94059cd4-e16d-4305-826b-fc107b538e81, Duration=825ms) 我不是确定授权设置,但如果有帮助,我可以更改函数应用的应用程序设置。 这些错误取决于函数正在执行的功能类型,可能是某些数据库操作可能需要很长时间才能响应..如果使用 at oll,您还可以检查数据库连接字符串。 @astro 调用是为了测试插槽而不是主插槽,对吧?请注意它是否相关,但我确实在与addProductOC 函数相关的日志中看到错误:TypeError: Cannot read property '1' of undefined 在第 101 行(大约在您共享的调用时间)。 @MitinDixit 谢谢!我们正在运行的函数是一个 HTTP 触发函数,它仅充当调用另一个 3rd 方 API 的网关。没有数据库调用或任何长时间运行的计算。我创建的测试槽在主槽中运行的代码完全相同。 【参考方案1】:

如果您获得 502:s 甚至缺少应用程序洞察日志条目...

健全性检查: 您是否已部署应用程序?

【讨论】:

【参考方案2】:

我遇到了同样的错误,我只是忘记添加“Content-Type”标头“application/json”。添加标题后它对我来说很好

【讨论】:

【参考方案3】:

在我的情况下,这是缺少 WEBSITE_NODE_DEFAULT_VERSION 应用程序设置(Node.js Windows 功能应用程序,高级和消费托管计划中都出现错误)。

如果您在 Azure 门户中创建函数应用,则会自动设置应用设置,但如果您使用 IaC 工具(在我的例子中为 Terraform),则需要明确设置。

"WEBSITE_NODE_DEFAULT_VERSION" = "~14"

【讨论】:

【参考方案4】:

当我将对象从 MongoDB(通过 Mongoose 方法:Model.find())直接发送到响应时,我遇到了这个问题。

          Claim.find()
            .then(claims => 
                context.res = 
                    status: 200,
                    headers: 
                        'Content-Type': 'application/json',
                        'Access-Control-Allow-Origin': '*'
                    ,
                    body: claims
                ;
                context.done();
            )

我必须将此 body 对象更改为:

          Claim.find()
            .then(claims => 
                context.res = 
                    status: 200,
                    headers: 
                        'Content-Type': 'application/json',
                        'Access-Control-Allow-Origin': '*'
                    ,
                    body: JSON.parse(JSON.stringify(claims))
                ;
                context.done();
            )

【讨论】:

【参考方案5】:

我也遇到了同样的错误。我在事件处理函数的末尾写了“context.done(null,output)”,它工作正常。

【讨论】:

@Andrew 我不认为这是一个不同的问题。我认为它实际上应该是一个非常简短的答案,并带有一个笨拙的介绍,使它看起来像一个“我有同样的问题”类型的问题。 好的,删除评论。 是的..我想说的是你可能错过了 index.js 文件中的这一行代码。【参考方案6】:

我在不同的情况下多次看到 502 错误。 大多数时候这是我的编程错误/配置问题。 但是今天发现这个问题花了很多时间,所以在这里分享。

问题 #1

s3 = new AWS.S3();
AWS.config.loadFromPath(dirName1 + '\\aws-config.json');
s3.listObjectsV2(..);

这里,虽然错误出现在第 1 行,但问题发生在第 3 行

解决办法是:

AWS.config.loadFromPath(dirName1 + '\\aws-config.json');
s3 = new AWS.S3();
s3.listObjectsV2(..);

也就是说,在加载 aws config 之后,我们需要创建 s3 新的实例变量。

问题 #2

这里给出的https://***.com/a/39240447/984471是关于将文件写入不可访问的目录,虽然您可以写入D:\local\Temp,但我们不能写入当前目录或D:\等其他目录

不幸的是,在上面的例子和我的其他例子中,Azure函数存在,没有任何登录azure函数控制台,所以需要大量调试。

【讨论】:

以上是关于Azure 函数应用返回 502 Bad Gateway的主要内容,如果未能解决你的问题,请参考以下文章

当后端请求成功时,如何调试从 Azure API 管理到 Front Door 的 502 Bad Gateway 错误响应?

Apache 使用 mod_proxy 和大文件返回 502 Bad Gateway

服务器域返回 502 Bad Gateway - Nginx

当代理服务器关闭时,NGINX 反向代理返回 502 bad gateway

我的提交功能以 Bad Gateway 502 响应

502 bad gateway nginx + puma + rails 3.2 on Elastic Beanstalk