C# Azure Function returning 502 Bad Gateway, but no error message in the Streaming Logs or Application Insights Failures

Jan Mechtel 31 Reputation points
2021-06-09T06:29:09.087+00:00

I have a simple Azure Function in C# Script (.csx) that's basically a proxy (receiving requests and forwarding them, then sending back the response).

It works fine except now i started sending requests to an ngrok target. Everything inside the functions works fine, include my log messages.

Except that the application making the call to the function (my application but also Postman for testing) receives a 502 Bad Gateway message.

The odd thing is that these 502 failures do not show up in Application Insights Failures.

How can I investigate more?

C# Azure Function returning 502 Bad Gateway, but no error message in the Streaming Logs or Application Insights Failures.

Update 1: I actually found entries in the Application Insights that show the request was completed successfully 200. Even though that's not the case for postman which receives 502

Update 2: Since i have working request (consumer receives 200) and non-working requests (consumer receives 502). I've tried comparing the headers of the responses.

One difference is that working request have a "Request-Context: appId=cid-v1:59e26eaa-a962-4c9d-a2c1-2541b4ae2e15" <> non-working have "Request-Context: appId=" where appId is empty.

Another difference is that non-working request have a response header "Transfer-Encoding: chunked".

<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>
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,266 questions
{count} votes

Accepted answer
  1. JayaC-MSFT 5,526 Reputation points
    2021-07-01T05:31:39.67+00:00

    Hello @Jan Mechtel , I have discussed this issue with the internal core team. In your case , it is ERROR_HTTP_INVALID_SERVER_RESPONSE 12152 The server response could not be parsed.

    Error Messages (Wininet.h) - Win32 apps | Microsoft Learn

    A retry of the client request will resolve the issue. However, if you still continue getting the error, I would request you to open a support case with Microsoft Support and share the code with them so that it could be investigated.


0 additional answers

Sort by: Most helpful