ASP.NET Core and 502 Bad Gateway Response


502 bad gateway error response is not like traditional 500 error which is mostly a code problem, this make it even difficult to troubleshoot the root cause and I also see quite a few confuse from developers that why there is 502 error in ASP.NET Core production environment.

Basically, the HTTP 502 Bad Gateway server error response code indicates that the web server (IIS in our scenario), while acting as a gateway or proxy, received an invalid response from the upstream server. This can happen for multiple reasons - for example, failure to connect to the upstream server, no response from the up stream server, or the up steam server took too long to respond (time out). As a result, the server which is acting as a gateway or proxy will report that as a status of 502. The reason why 502 is difficult to troubleshoot is because the root cause could vary a lot and will be dependent on real application framework, such as Asp.Net, PHP, Java etc.

For ASP.NET Core application, as we know, in order to host the application in IIS, we need install AspNetCoreModule. It is actually a native module in IIS acting as a reverse proxy for dotnet core application, that's why we may experience 502 error for ASP.NET Core application.

Though 502 error could also be caused by underlying platform issue such as disk/storage problem, in most condition, we need to focus on the components running in the upstream server (root cause), and IIS log will give us information about how to narrow down the issue.

Now, suppose we experience 502 error when accessing an ASP.Net Core application, the sub status code might not be helpful enough. Firstly, we can have a check for the IIS log and verify the root cause by sc-win32-status code. Alternatively, we can enable Failed Request Tracing to know more details.

The above log give us two import information, the error comes from AspNetCoreModule and the error code is 2147954402. That means, the 502 error is fired by AspNetCoreModule and it probably  has experienced some invalid response. Furthermore, we can use either err utility or visit http://errors/ to figure out 2147954402 is just WININET_E_TIMEOUT. By default, ASP.NET Core request time out is 2 minutes, and we can change it via requestTimeout in setting file. For more ASP.NET Core configuration, please refer to ASP.NET Core Module configuration reference. Lastly, you can also visit the GitHub repository for AspNetCoreModule, you will be able to see how 502 is handled in it.

Comments (0)

Skip to main content