Although both of them are gateway errors, a 502 Bad Gateway is used to indicate that the gateway received an invalid response. A 504 Gateway Timeout implies that the gateway received no response whatsoever within a certain period of time. One is bad data, and the other is no data.