How to fix ‘502 server error – Bad Gateway’ in web servers
Google forums get bombarded by customer complaints whenever they see a ‘502 server error – Bad Gateway’ in their Google applications. Though an intermittent error, it can often shoo away visitors from your website.
Bobcares helps website owners and web hosting providers fix all website and application errors, and this error is often reported at our Website Administration Services.
The 502 Bad Gateway error in sites can present itself in different ways, such as ‘502 – Service Temporarily Overloaded’ or ‘HTTP Error 502 – Bad Gateway’.
Recently, we were contacted by a VPS server owner whose customers were complaining about 502 temporary errors on all sites every hour. This was affecting his business badly and wanted a permanent solution.
What causes ‘502 server error’ in web servers
A 502 error means that a web server that is serving as a reverse proxy (say, Apache) for the website origin server (such as NginX), did not receive a valid response from the original web server.
Upon examining the server, we could see that the Apache web server in it was running as an NginX proxy. The web server was restarting the http service about once an hour.
In our experience handling 502 server errors, we have seen that a ‘502 Bad Gateway’ error happens due to one of these reasons:
- An overloaded server – A web server can crash often due to resource crunch such as memory shortage, which can be caused by abusive users or process overloads.
- Web server frequently restarting – Due to any configuration errors, module failures, too many applications and processes running on it or even any attacks, the web server can restart frequently and cause temporary 502 servers errors in websites.
- Bad code in websites – Websites that run legacy applications or sites with improper programming code, can tamper with its proper functioning, and show up errors intermittently.
- Network related errors – Other network related errors such as DNS issues, routing problems, firewall blocks in server, ISP related issues, also can lead to 502 server errors such as ‘502 Bad Gateway’.
- Server software timeout – This error can also occur when requests at the NginX server takes more time to complete and a caching tool such as Varnish Cache reaches its timeout value in that time. Other issues such as slow queries can also cause 502 errors.
[ Worried about your broken websites? Get our server specialists’ assistance to fix your site from all errors. ]
How to fix 502 service error in NginX web server
It is important to identify the actual cause of the 502 server error before pinpointing on the actual solution. We examined the web server logs during the restart times and identified the ‘seg fault’ errors during that time.
During further investigation, we examined the configuration settings of the web server and found that there was a missing module ‘mod_rpaf’, which was causing the web server to crash.
root@server [~]# ls -l /usr/local/apache/modules/mod_rpaf-2.0.so /bin/ls: cannot access /usr/local/apache/modules/mod_rpaf-2.0.so: No such file or directory
Rpaf is the reverse proxy add forward module designed for backend Apache servers. It’s useful when you are running Nginx as front end server, and you need to get the real IP of the backend requests.
Since the module was conflicting with the Apache-2.4 version, we patched it further to get it working fine. Once the recompilation was done and Apache was restarted, the segmentation fault issues stopped.
We monitored the web server for some more hours and noticed that it was working fine without restarts or giving any 502 server errors in websites.
Some other fixes that help in avoid 502 server errors for websites are:
- Keeping the website files such as plugins and themes updated and relevant.
- Optimising and fixing slow MySQL queries in the server.
- Auditing the web server software and maintaining the modules up-to-date.
- Avoiding any routing issues and monitoring the web server for any overload or attacks.
[ A single misconfiguration may break your site! Click here for a customized and error-free website to help you boost your business. ]
Bobcares helps online businesses of all sizes achieve world-class security and uptime, using tried and tested solutions. If you’d like to know how to make your server more reliable, we’d be happy to talk to you.