[FIXED] 502 Bad Gateway Error on Nginx Web Server

Category : nginx

HTTP response codes, particularly those that represent an error, can be difficult to deal with. While some are simple, others have obtuse or confusing meanings and, worst of all, since these codes are the result of a much broader relationship between the client, the web application, a web server, and untold numbers of outside web services, pinning down exactly what a given error indicates can be a challenge in the best of circumstances. Today, we will discuss 502 Bad Gateway error on Nginx web server. The error should look like the below image.

502 bad gateway error

A 502 Bad Gateway error indicates that the edge server (server acting as a proxy) was not able to get a valid or any response from the origin server (also called upstream server).  It’s possible the server is overloaded or there are network issues between the two servers, and it’s just a temporary problem. It’s also possible there’s an improperly configured firewall or even a coding error, and that the problem won’t get fixed until those issues are addressed.

Here are a few examples of error page what you might see:

  • “502 Bad Gateway”
  • “HTTP Error 502 – Bad Gateway”
  • “502 Service Temporarily Overloaded”
  • “Error 502”
  • “502 Proxy Error”
  • “HTTP 502”
  • “502 Bad Gateway NGINX”

The main Nginx configuration file is /etc/nginx/nginx.conf
Nginx error log Location: /var/log/nginx/error.log

There are a few main culprits that cause 502 Bad Gateway responses. These include:

  1. Domain name not resolvable: The domain name is not resolving to the correct IP or it does not resolve to any IP. It is important to note that DNS changes could take some time until they are global fully propagated and active. This is dependant on the TTL, or time to live, defined per record.
  2. Origin server down: The server is not reachable, either because it is down or there is no connectivity to the server given.
  3. Bad File Permissions: ff the application was working fine before and suddenly this error occurs, permissions are not a very likely culprit. However, if modifications were recently made (such as upgrades or installations), it’s possible that file permissions were changed or are otherwise incorrect, which could cause an issue to propagate its way throughout the application and eventually lead to a 502 Bad Gateway Error.
  4. Improper Firewall Configuration: In most cases, all potentially harmful traffic is stopped (and may be logged for network admin use). In some situations, it’s entirely possible for a firewall configured somewhere on the network in which your application is running to be preventing some form of critical traffic from getting through.
  5. Application Code or Script Bugs: If all else fails, it may be that a problem in some custom code within your application is causing the issue. Try to diagnose where the issue may be coming from through manually debugging your application, along with parsing through application and server logs.

Troubleshooting Steps

  1. The first step of this is to check for the process ID of the main Nginx process, you can run the following command and except output somewhat similar to the one indicated below:
    ps x | grep nginx
    24152 ? S 0:00 nginx: master process /usr/sbin/nginx
    The first column of each row is the process ID, as we can see, the main/master process ID is 24152in this case, however, this will change in every system.
  2. Is PHP-FPM running?
    Use the following command to check whether PHP-FPM is running.
    ps aux | grep php
    ps aux will output all processes that are running, so we add | grep php to only output processes with php in the name. If you see PHP processes, then this is not the issue. Otherwise, try to stop/start/restart PHP.
    sudo service php7.2-fpm stop
    sudo service php7.2-fpm start
    sudo service php7.2-fpm restart
    If there are still no PHP-FPM processes running, you might try to remove PHP and reinstall it. If PHP-FPM is running correctly, skip this step and go to the following section.
    sudo apt-get remove php7.2 php7.2-cgi php7.2-fpm
    sudo apt-get install php7.2 php7.2-cgi php7.2-fpm
  3. is PHP-FPM listening correctly?
    A common issue is that the PHP-FPM service is not listening to the host/port which is configured in NGINX. Find the www.conf file on your server in the PHP folder. Generally, this can be found here:/etc/php7.2/fpm/pool.d/www.conf
    Search for the listen parameter and make note of it:
    ; The address on which to accept FastCGI requests.

    ; Valid syntaxes are:
    ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
    ; a specific port;
    ; 'port' - to listen on a TCP socket to all addresses on a
    ; specific port;
    ; '/path/to/unix/socket' - to listen on a unix socket.
    ; Note: This value is mandatory.
    listen = 127.0.0.1:9000
    As you can see this is set to 127.0.0.1:9000, but it is also commonly set to /var/run/php7.2-fpm.sock.Now find your NGINX server configuration file, usually located at /etc/nginx/sites-available, and open it. Look for something like this (location ~ \.php$):
    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    }
    The parameter we are looking for is fastcgi_pass. This has to be the same value as in the listen parameter in the www.conf file. Change it accordingly.
    In our case, we changed fastcgi_pass unix:/var/run/php5-fpm.sock; to fastcgi_pass 127.0.0.1:9000;
    Restart the NGINX and PHP7.2-FPM services! It is necessary to do this so that the updated configuration files are loaded.
  4. Change the NGINX config and increase buffer and timeout parameters.
    It might be possible buffer and timeout parameters are configured incorrectly for your server or they don’t suffice anymore. You can find the NGINX configuration file at /etc/nginx/nginx.conf. Try increasing the following parameters.
    Increase buffer and timeouts inside http block:
    http {
    ...
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    ...
    }
  5. PHP Timeout Issue
    PHP timeouts occur if a single PHP process runs for longer than the max_execution_time or max_input_time set in the PHP configuration on your server. When this happens a 502 server error is usually displayed. If you run into a PHP timeout these values may need to be elevated. You can check the current PHP Version used on the website and edit its configuration files increasing the PHP Limits.

If above troubleshooting steps do not help to fix the issue then it is suggested to check the Nginx error log in /var/log/nginx/error.log file. If you aren’t sure and unable to fix the issue then you can reach us out at technical@basezap.com address with the issue.

As you can see there are quite a few things you can do to troubleshoot and fix a 502 bad gateway error on your website. Typically this is not on the client-side, but rather an issue with the hosting server.