Azure function app returning 502 Bad Gateway

Viewed 9511

I am getting the error message below when I run a HTTP Triggered function app on our main slot :

 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">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#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>

But when I tried to create a new slot and function app with the same exact code, it works fine without getting the error above. It seems like there's an issue with our main slot's configuration but I just can't find any resource to point me to it.

Has anyone else encountered this problem? How did you fix it?

6 Answers

I have this problem when I sent object from MongoDB (by Mongoose method: Model.find()) directly to the response.

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

I must change this body object to:

          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();
            })

I have seen 502 error many times in different situations.
Most times it is my programming mistake/ config issue.
But today it took a lot more time to find this issue, so sharing here.

Issue #1

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

Here, though the error is at line 1, the issue happens at line 3

Solution is:

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

That is, after loading the aws config, we need to create s3 new instance variable.

Issue #2

As given here, https://stackoverflow.com/a/39240447/984471, is about writing file to un-accessible directory, while you can write to D:\local\Temp, we can't write other directories like current directory or D:\

Unfortunately, in the above examples and also other instances I had, the Azure function exists without any log in azure function console, so there is need for a lot debugging required.

I was also getting the same error. I wrote " context.done(null,output)" at the end of event handler function and it is working fine.

In my case it was a missing WEBSITE_NODE_DEFAULT_VERSION application setting (Node.js Windows function app, error occurs in both Premium and Consumption hosting plans).



The app setting is set automatically if you create a function app in Azure Portal, but you need to set it explicitly if you use an IaC tool (Terraform in my case).

"WEBSITE_NODE_DEFAULT_VERSION" = "~14"

I had the same error and i simply forgot to add the "Content-Type" Header "application/json". After adding the header it works fine for me

If you get 502:s and even lack application insights log entries...

Sanity check: Have you deployed your application?

Related