Martin Staufcik
Martin Staufcik

Reputation: 9502

App Service hosted in Azure returns 502 error sometimes

For some web API requests, probably those long running, in an App Service hosted in Azure, I am getting response 502 error page. Sometimes the API call fails, sometimes it succeeds.

502 - Web server received an invalid response while acting as a gateway or proxy server

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.

I tried to log into log files (LogFiles/eventlog.xml), but could not trace the error.

I suppose it is connected to some timeout, but do not know where it can be set in App Service's setting.

Upvotes: 0

Views: 2109

Answers (2)

Martin Staufcik
Martin Staufcik

Reputation: 9502

In Azure, IIS is reverse proxy for Kestrel server.

I solved the problem by adding .UseKestrel(...) to the BuildWebHost function in Program.cs file as follows:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); })
        .Build();
}

The more generic question about timeouts of long running tasks is answered here.

Upvotes: 0

Thiago Custodio
Thiago Custodio

Reputation: 18362

It can be some issue with the network. I recommend you the implementation of the retry and circuit breaker patterns to better handle this kind of problems:

https://learn.microsoft.com/en-us/azure/architecture/patterns/retry

Upvotes: 1

Related Questions