502 Bad gateway and how to solve it?
The client-server architecture (in short):
To understand what is 502 Bad gateway error and how to fix it, we need to understand how server network functions. Usually, there is one origin server in which the web application or the web site is saved. This server is used to get constant data (visual or quantitative) into edge servers which then in-turn serves the end-user ( mostly the browser).
The purpose of doing this is, to save money, increase web site or web application response, and improve the overall experience for everyone. This is done because the origin server does not have to be very powerful to be able to handle the large request as it is taken care of by the edge server.
The edge server saves the contents of the origin server and serves it to the end-user as fast as possible. This is done efficiently by the edge server as it is designed to do this process well. The edge server is very quick in this process and it is simultaneously serving many origin servers to the corresponding end users.
What happens during the 502 Bad gateway:
When the origin server does not function, it is called the server is down. When the origin server is down, the edge server requests the origin server for the content but the origin server does not respond to the request. This results in a 502 Bad gateway response from the origin server to the edge server.
The edge server, in turn, gives a 502 Bad gateway response by the origin server to the respective end-user browser. This browser is what we call the 502 Bad gateway.
Some of the important reasons for this error:
502 Bad gateway: This error will happen when the origin server is down or there is no connectivity to the origin server. The problem can also arise when the domain name is not resolvable. This might be the case when there is a change to the IP address of the site where it is hosted. Another interesting blocking can happen by the firewall. The firewall might be programmed to block the edge server to prevent DDOS attack or to block request from the same IP address which.
The blocking of the server from the same IP address can happen as the edge server has only a limited number of IP addresses from which it will request the origin server.
Track the error in the server:
The best way to know what went wrong we should take look into the server error log. The server error log can be found in the corresponding locations fo the server. Below we have given the error logs where the users can find the error log depending on the service type they use.
IIS Server Error Logs:
Apache Server Error Logs:
EnginX Server Error Logs:
Navigate to the above locations in the corresponding server that is being used and analyze the errors to find out what exactly has gone wrong. Fix the issue and that will bring the origin server back up. The edge server, in turn, will read the origin server and return the data to the end-users.
How to fix 502 Bad gateway:
The 502 Bad gateway is essentially the fault of the origin server. There is little that the end-user can do but actually notify the server administrators. The server administrators should be alerted of the situation so that they can act fast to fix whatever the issue is.
The end-user can clear the browser cache just in case to make sure that the user is seeing the latest output from the edge servers.
How to clear Browser Cache:
Clear Browser Cache in Chrome:
- Open a new tab and enter Chrome://settings in the URL bar.
- Enter settings in the search bar and press enter.
- Scroll to the bottom till you reach the option shown in the image below.
- Now click on Clear browsing data.
- It will open a new pop up window.
- In that pop-up window, go to the advanced tab.
- Now make sure that the cookies and other site data are checked and now click clear.
The above-mentioned method will work only if the 502 Bad gateway was being shown because of the persistent browser cache. 502 Bad gateway is usually not due to this error and if you were a web the server administrator, you will have to see what needs to be done in that case. Please see the solutions to what needs to be done.
Server Communication problems:
Most websites and web apps are run on multi-server architecture. Often times, developers overlook this case while developing web applications. This results in undesired outputs when the application strictly relies on the data that is communicated from one page to another. Therefore the data might not be available to one server node from another. This will cause the 502 Bad gateway to happen. In situations like that, we have to redesign how the application works and make it suitable for multi-server architecture.
Make sure that the Domain Name is pointing to the right IP:
If the website has been moved to a different IP then the DNS records of the website should also be updated as well. In case of a failure of updating, the site will not throw the 502 Bad Gateway. So the DNS records should also be updated.
Fix the bugs in the application:
Ultimately if nothing seemed to fix the problem, it is obviously the error in the web application itself. In such a case, the developer should find the error and fix the error. This will remove the 502 Bad Gateway.
In this post, we saw how to fix the 502 Bad Gateway and many reasons why this happens. The reasons are that there may be a firewall misconfiguration, change in the DNS records, and also a bug in the website or web application code itself. This is usually a server-side issue and therefore there is little that a user can do form the browser to try and solve the 502 Bad Gateway issue. 502 Bad Gateway fix is mostly done on the server. If you would like to see a more detailed post on how to fix 502 Bad Gateway in any server please let us know in the comments below.