The error arises when a server (which is not necessarily a web server, acting as a proxy or gateway to fulfil and forward the request by the user at the client end to access the requested url) received an invalid response from the main upstream server which it accessed to fulfill the client request.
So it don’t mean that the main upstream server is down or giving error, but it simply means that there was some issue between the mainstream server and the gateway / proxy server on thedata exchange protocol and it gave an invalid response. In simple words, it means that one of the machines, mainstream server or the gateway, has been incorrectly programmed / configured.