Bad Gateway Error Alerting and Resolution
What is a HTTP 502 Bad Gateway Error?
When a website or API returns a 502 status code, it means there is something wrong on the website. So the user requested a proper page, but something on the server makes it unable to fullfill that request.
The official RFC says: 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.
A Bad Gateway error occurs when a website is served through multiple layers of servers. Normally one server receives the request and then forwards it to another (upstream) server. When the response is received, the first server (proxy) will then return the response to the original client. If somehow the connection between these two server fails a bad gateway error is the result.
In most cases the proxy server will be Nginx, which is used to relief load of the primary servers. The most important thing to note about this error is that it is almost never a problem with Nginx, it is just the messenger.
What causes Bad Gateway Errors?
Most of the time this error occurs, it means the proxy server is not able to reach the upstream server at all.
Your upstream server may have crashed. It might be helpful to utilize a tool that will automatically restart your service after a crash.
Your upstream server may not have auto-restarted after a server reboot
If your upstream server is a python based stack (e.g. Flask or Django) the error may be caused by an error in your code that made it impossible for the upstream service to start.
The upstream server may be overloaded and unable to take a new request as it is already on the maximum number of simultaneous connections.
How will Observu help improve uptime?
Observu will help you in two ways:
- It notifies you by E-mail, SMS and/or Phone when your site starts displaying a bad gateway error
- The server health agent can keep track of your Apache or Nginx error logs where these errors are logged
Combining these two sources of information will let you know about errors as soon as possible as well as making responding to them a whole lot quicker.
Furthermore, because Observu checks so regularly (up to 7 measurements per minute) you are more likely to catch errors that do not happen every time.