The server is one of those things that is really hard to maintain. There are a lot of factors that need to be under consideration in order to maintain the server. Such as Security patches, upgrades, server errors. One example of a common server error is 502 bad Gateway on nginx server. 

In more detail, the 502 bad gateway nginx signifies that a response was unable to generate from the main server to the edge server. The edge server is also known as the proxy server. 

There is some particular reason why this could happen. If you’re one of the people who happen to be facing this, then go through the content below to get a clear idea.

502 Bad Gateway: What Is It? What Triggers It?  

nginx bad gateway occurs when there is an issue with the connection. The proxy server becomes unable to receive a response from the mains server, There are few reasons why it happens. Here’s why:

1. Backend Service Fail

As you know Nginx is solely dependent on backend services. Such as the database services, PHP-FPM, cache servers and more. If the backend services crash due to some reason, then Nginx will be unable to retrieve any data from there. 

There could be more than one reason why the services can fail. Such as an increase in traffic, errors in disk or even Denial Of Service Attack. 

2. High Load In Server 

Another reason that is related to the backend services, is traffic spike. Due to many external factors can trigger a sudden spike in traffic. These factors can range from seasonal or occasional marketing, spamming, brute force attacks and software bugs. 

3. Improper Service Configuration

The backend services are also solely dependent on sub-systems. Without sub-systems, Nginx cannot work properly. If there is an incorrect entry in the configurations of any of these services, then you’d get the 502 bad gateway error. Here’s what can go wrong:

  1. Configuration error in DNS. 
  2. Incorrect entry of password entry in the database due to factor slike update, migration, etc. 
  3. Syntax error in Apache firewall settings. 
  4. The capacity of connections limited. 

4. Firewall Blocking Service Port

The firewall’s main purpose is to ensure security to the servers. So, as you know, if the setup is not right, then the error code is likely to generate. To make matters clear, let’s take an example. 

Apache uses the port configuration of 7080 while Nginx runs on the configuration of 80. And firewall has a default protocol that is meant to block port configurations that are uncommon.

5. Web Application Bugs 

This is rather uncommon but the possibility exists. If you see codes that look unfamiliar to you, then it is most likely due to some sort of application error. If there are codes that are unable to function, then you’ll get the 502 bad gateway error. 

How To Resolve  502 Bad Gateway Nginx Error? 

Now that you have a clear idea of what triggers the error, get started with resolving nginx bad gateway, using the below-mentioned solutions! 

1. Kill Processes

If there is an issue with the backend services, then what you can do is kill the process that is not responding. Once you’ve ended the unresponsive sessions, restart them back again from scratch one. Here’s how to kill the processes and restart:

#kill -9 $(pgrep PHP-fpm)

# /etc/int.d/php-fpm restart

* restarting PHP FastCGI Process

Manager php-fpm [OK]

Enter these commands and you’ll be good to go. 

2. Check Source Of High Load

If there is a high load on the backend service, the first thing to do is to check the source that is causing the load. Check if it is the CPU or the Memory. If the system is having a high load, then it is advisable that you help from the support team to rectify the issue. 

3. DNS Test Tool

By using a DNS Test Tool, try checking that the fully qualified domain name (FQDN) is resolving properly or not.  

4. Check Port Settings

To check the services assigned to each and every port used, the following commands to verify; 

#netstat -1pn

Tcp 0 1 0.0.0.0;80

0.0.0.0:*

19785/nginx

Tcp6 0 0:::80

:::*

LISTEN

19785/nginx

If you come to find that there are services running in the ports that are non-standard, then you’d have to make changes. Such as make changes in the service configuration and assigning them to the ports that are standard. 

You can also make modifications to the firewall configurations and then allow the ports that are non-standard. 

5.Clear Browser Cache/Cookies

Browser cookies and cache get accumulated over the course of time. These may affect the server response to generate. Here’s how to get rid of them:

Re-open the Google Chrome web browser and then go to the “Settings” menu on the top. Then from there, scroll down to the section called “Privacy & Security”. Then, from there, go to the section called “Clear Browsing Data”. 

After that, in the following window, you’ll see categories of boxes that say, “cache images & files”, “Browsing Data” and “Cookies”. Make sure you’ve ticked all the boxes. Then click on the option called “Clear Data”. 

6. Change DNS Server

Try changing the server of the Domain Name Server. That might solve the issue. Most likely, you’re on the DNS server that is the default one and has been assigned by the Internet Service Providers. 

Instead of that, try using any other DNS server. Such as Google’s Public Domain name Server. 

7. Reset The Network Connection/ Restart System

Restarting the system almost, all the time resolves many technical or network related issues. What happens is that if there is any bug or configuration that is stuck or causing error, then the system restart would solve it. The configurations will be set to default once it restarts. 

Another thing you can do is, reset the network connection once the system restarts. Disconnect the network connection from the system. Then reconnect to the network. Ensure that you are getting proper access to the Internet connection. 

8. Check Application Logs

Try checking the server-side logs. These logs tell you the past activities of the application. For example, which pages were requested by the application, which servers were responsive and got connected. 

Also, try checking the server logs as they provide a history of hardware. It will give you details of the status as well as the health of the services that are connected. 

9. Contact Support

If none of the solutions have worked out for you so far, then the last thing you can try doing is reaching out to the support team for help. Briefly describe the issue and you should get solutions for 502 Bad Gateway Nginx immediately!