Azure function app returning 502 Bad Gateway

azure application gateway intermittent 502
azure application gateway 502
azure function app 502 bad gateway
azure application gateway 400 error
504 gateway time-out microsoft azure application gateway v2
azure application gateway error 502 bad gateway
502 bad gateway microsoft azure application gateway
502 - web server received an invalid response while acting as a gateway or proxy server. azure

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" "">
<html xmlns="">
<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;} 
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;
#content{margin:0 0 0 2%;position:relative;}
<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>

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

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

I must change this body object to:

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

Troubleshoot Bad Gateway errors, Go to the Azure Support site and click on Get Support. Symptom. When you browse to the app, it returns a HTTP "502 Bad Gateway" error or a  If access to the backend is blocked because of an NSG, UDR, or custom DNS, application gateway instances can't reach the backend pool. This causes probe failures, resulting in 502 errors. The NSG/UDR could be present either in the application gateway subnet or the subnet where the application VMs are deployed.

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');

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

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

Issue #2

As given here,, 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.

"502 bad gateway" and "503 service unavailable , 502 Bad Gateway on Python Azure Function #384 I attached Application Insights to get some logs, but only the logs of successful runs are Do you have an approx range of the response length the function is returning? "502 bad gateway" and "503 service unavailable" are common errors in your app hosted in Azure App Service. This article helps you troubleshoot these errors. If you need more help at any point in this article, you can contact the Azure experts on the MSDN Azure and the Stack Overflow forums.

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.

502 Bad Gateway on Python Azure Function · Issue #384 · Azure , Hi there, all of a sudden, whenever I select a function in my function app, I get a red error message box saying: Error: You may be experiencing  Helps you troubleshoot bad gateway (502) errors that occur in Azure Application Gateway. Who is it for? System administrators who use Application Gateway to manage traffic to web applications. How does it work? Provides a checklist and sequence of steps to help you identify the issue and reach a resolution. Estimated time of completion: 15-30 minutes.

Status: 502 Bad Gateway · Issue #309 · Azure/azure-functions-host , used continuous azure webjobs to perform the long running operation. Used azure queue trigger to trigger the webjob whenever there is a new  Azure Websites - 502 - Web server received an invalid response while acting as a gateway or proxy server 11 502 Invalid Response when calling Google Api from Azure Website

502 bad gateway azure app service + provider hosted add-in , I have a small python Azure Function that intermittently fails with a 502 Bad Gateway I attached Application Insights to get some logs, but only the logs of 10:08:09.853 UTC (Note that a 500 Internal Server error is returned but that's OK​  HTTP 502 from one instance of an Azure Web App. and the odd thing is that the 502s were returning instantly. Azure Java function -502-Bad Gateway. 0.

502 Bad Gateway on Python Azure Function, The official Microsoft Azure account for improving customer Hitting "502 bad gateway" or "503 service unavailable" in App Service? This can  Lack of the log to investigate the HTTP 502 errors from the ARR. I get a bunch of HTTP 502 errors when accessing to my Web site which is running ASP.NET application. HTTP 502 must be returned from the Front End ARR, and I could not find any errors in the Event log, IIS log, and FREB,

  • Do you by any chance have authorization set up on the main slot? In some cases, when misconfigured, it could cause that. Can you share your function app name (that's the app name, not function name!), either directly or indirectly? This will help us investigate.
  • Hi @david, thanks for your response. The function app name we're having issues with 2017-10-06T04:11:48.364 Function completed (Success, Id=94059cd4-e16d-4305-826b-fc107b538e81, Duration=825ms) I am not sure about the authorization set up but I can make changes on the function app's application settings if that helps.
  • these error depends on the type of functionality is being performed by the function might be some database operation that might be taking long time to respond .. you can also check database connection string if using at oll.
  • @astro that invocation was to test slot and not main slot, right? Note sure if it's related but I do see errors in the logs related to the addProductOC function: TypeError: Cannot read property '1' of undefined at line 101 (around the time of the invocation you shared).
  • @MitinDixit Thanks! The function that we're running is an HTTP Triggered one and it only acts as a gateway calling another 3rd party API. There's no database call or any long running computation written in it. The test slot I've created that has the same exact code running in the main slot.
  • @Andrew I don't think this is different question. I think it is actually supposed to be a very short answer with a clumsy introduction that makes it look like a "I'm having the same problem"-type of question.
  • Ok, removed comment.
  • yeah..all that i was trying to say is maybe u missed this one line of code in your index.js file.