Interpreting an API errors can be very difficult sometimes, specially when we have no idea what could it mean or if we can fix it or not. One such API error is HTTP 502 error code.
In this article you will be explained what HTTP 502 error code is, cause for this error and what can you do .
Before we start with HTTP 502 error code, you should know about an important term called proxy server.
As we all know that a client can’t hit the server directly, a reason for it security and privacy while another reason is, there is no direct way.
So proxy server acts as an intermediator between the server and client. Because of that it is also known as intermediary server or gateway server. In other words we can say that proxy server acts as a bridge between client and server, which can pass client’s request to server and server’s response to clients.
In addition to the intermediator, proxy server also improves the security, privacy and performance of the server.
Security : Proxy server can identify and protect user from cyber attacks as it can identify malware and other threats which can ensure the security of the users.
Privacy : Proxies can hide the user’s IP address because of which it can be difficult for the invader to identify the user.
Performance: Proxies can cache frequently-requests of the user, which can decrease the response the time of server.
What is HTTP 502
HTTP 502 error is the code for **bad gateway error**, which means when the user sends a request to the server and there is an invalid response from the side of server, so server send the error code HTTP 502.
Lets understand it with the help of an example, lets assume a client sends a request to the server which user is unable to do it directly so it sends the request to proxy server after establishing client > proxy connection. Then the proxy creates the connection between itself and server which is called proxy-server.
If something wrong happened between the establishment of proxy-server connection or when the server is responding, or server response is invalid, etc, then server send HTTP 502 error.
Causes of HTTP 502
Timeout of Proxy
One of the cause of HTTP 502 error is proxy timeout, which means that after setting the connection between proxy and server, if the response of the server came after the proxy timeout then server issue this error.
One of the reason behind it can be that the timeout duration of proxy server would be less than timeout duration of the server, so before server can respond the request, proxy timeout occurs.
Connection Issue of Proxy > Server
While establishing the connection between the proxy and server or connection loss when server is processing the request can also be a reason for HTTP 502 error. This normally happens when the internet connect is not strong enough, when connection dropped suddenly or when application pool crashes. In this case, you will not be able to find an entry for the request on the server side in IIS access log.
Queued Request
This is the hardest cause to identify why the request is not served or responded validly, or not served at all. This can happen when API server suddenly get lots of requests because of which your request is queued for the response and before server can process your request and respond, server timeout occurs. Another reason of this can be that the request have very small execution time, which most probably lower than proxy timeout.
Identifying Causes
Identify a Proxy Timeout
To confirm that the 502 error was issued because of the proxy timeout, you can follow the following steps.
Check server’s access logs.
In IIS access log, you can find the Status Code of the error. If the Status Code is not 502 from the server side, then 502 was issued by a network element.
To ensure that the problem is timeout, you can check the duration of the request and tally it with the network elements who has smaller timeout.
Identify Connection Issue of Proxy>Server
To ensure that 502 error was issued because of the connection loss between proxy and server, you should do implement the following steps :
Check IIS access logs to make sure whether the request was served or not.
If the request is not served then it means that there was some issue while establishing the connection between proxy and server.
Check Windows event viewer to confirm if the error occurred due to some network related error or application pool crash. You can find Windows event viewer under Windows logs > System.
Confirming the Queued Request
Finding out that the error is caused due to queued request is the hardest, but one of the most effective way to figure it out is :
Check the access logs of proxy as well as server.
If you find out the request in proxy until proxy timeout is occurred but didn’t found any request in the server side then the request wasn’t even received by the server and is still queued.
On the other hand, if you find the request in the server as well as in the proxy access logs then it means that request was received by the server and timeout occurred when request is processing.
Please note that queued request doesn’t always leads to 502 error only, that is why it is recommended to check the access log of both proxy and server.
Conclusion
So after considering all the points we can conclude that, HTTP 502 error code is occurred mainly from the side of proxy and server, and there is not much a client can do about it, rather than making sure that proxy timeout is greater than or equal to the server timeout and your connection is stable.
I hope this article helped you to troubleshoot HTTP 502 error code. If you wanted to read more knowledgeable article like this, then do subscribe our newsletter.
I have been developing software for more than 10+ years, and I am an IT specialist. At the moment, I work with Prodfinity as a Technical Consultant. Through project delivery, I consistently focus on giving the client a high-value product rather than merely software. I take an active position in the Scrum Team and help the team solve problems for clients.
- Devendra Baghelhttps://stepfinity.com/author/devendrabaghel/
- Devendra Baghelhttps://stepfinity.com/author/devendrabaghel/
- Devendra Baghelhttps://stepfinity.com/author/devendrabaghel/
- Devendra Baghelhttps://stepfinity.com/author/devendrabaghel/