An HTTP status code of 502 – Bad Gateway means “The server, while acting as a gateway or proxy, received an invalid response from the upstream server it accessed in attempting to fulfill the request.” I interpret that to mean that there is a server (server A) between my client and the server (server B) that actually should respond to my request. And for some reason server B responded in a way that server A doesn’t know how to handle which triggers the 502.
Looking at the HTTP sub status code is always a good idea. Running on the Windows OS using IIS, an HTTP sub status code of 3 means “Bad Gateway: Forwarder Connection Error (ARR)”. Where ARR stands for Application Request Router. I wrote some articles about ARR here. When I read this one, my mind flows in the opposite direction as the what I mentioned in the above paragraph. In that, instead of the inbound response from server B to server A, it is my outbound request passing from server A to server B, forward, and server A returns the error <handled>.
502.3s are most common when running a web site in which IIS acts as a proxy. For example, when running a PHP site on IIS, the W3WP process spawns 1 or more php-cgi.exe process which actually handle the execution of the request. Similarly, when running and ASP.NET Core in IIS, the W3WP process spawns a dotnet.exe process which manages the execution of the requests.
If for some reason the spawned executable cannot respond correctly, this 502.3 HTTP status/sub status code (502.3.12092 or 502.3.1292) is commonly returned. Here are a few points to move it forward:
- This is most likely an application issue, it is getting hung up making a outgoing request to a database or other Web API. If you can reproduce, then you can capture a memory dump and check it out. See here.
- Make sure you are using ASYNC methods (caution using Task.Result(), it blocks)
- Use the HttpClient as a singleton (*)
- Check your ServicePointManager.DefaultConnectionLimit and HttpClientHandler.MaxConnectionsPerServer and make sure they are what you expect
If you ever experience this, look at the application is it is very unlikely to be something else. It’s not IIS.