Fix 502 Bad Gateway Nginx Error

While web browsing, you must have noticed websites returning 502 Bad gateway NGINX error. It is really frustrating to keep seeing this error again and again. While sometimes, your website runs well and returns the same error after a couple of hours.

There are a few reasons why you will find this error message. You might see the following error messages due to the customised 502 Bad Gateway error message. Here is the list of most common 502 error messages:

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

You will find 502 Bad Gateway Error while-

  • Nginx running as a proxy for Apache web server.
  • Nginx running with PHP5-FPM.
  • Nginx running with PHP7-FPM.
  • Nginx running with other services as a gateway.
  • Bad buffering / timeout configuration.

Such type of error is referred to as an HTTP Status Code Error. It generally occurs at the gateway. Where gateway is an access point, a bridge communicates between the two servers. In this case, the gateway can be a service or application that is working and receiving requests from Nginx web server.

Let’s dive deep into the reasons to understand the 502 Bad Gateway Nginx Error.

1. NGINX as proxy for Apache

While using NGINX as a proxy for Apache, there might be a possibility that Apache is not well configured and this might result in 502 Bad Gateway Error. In most of the cases, it can be resolved by just restarting the Apache web server.

2. NGINX with PHP5-FPM

While using NGINX with PHP5-FPM, if it is not well configured, the website will throw 502 Bad Gateway Error. There might be a possibility that PHP5-FPM will get overloaded with too many requests, may be because of PPC running on that website. In this case, restart PHP5-FPM and check the error logs.

3. NGINX with Services/Apps.

You should restart the other services that you are using along with the NGINX with some service or Apps and check the error logs.

These 3 reasons will help you understand the reason for 502 Bad Gateway Error. It is very important that you know what services you are using and whether it is configured correctly or not. To know the issue, you need to check the log files and then you will get some idea and find the right solution.

Here is the simple command for NGINX that you can use to check your logs and identify the issue:

tail -f /var/log/nginx/error.log

Now, Let’s have a look at the other quick solutions for 502 Bad Gateway Nginx Error:

Solutions For 502 Bad Gateway Nginx Error

Solution 1: Increase Buffer & Timeout Inside http{..} Block

You need to increase buffer and timeout inside nginx.conf file under http{..} block section. Run the following command and edit the nginx.conf.

sudo vim /etc/nginx/nginx.conf

Add the following under http{..} block:

http {

#...
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

#...
}

Solution 2: Ensuring PHP-FPM5 is Listening Properly

Ensure that your PHP5-FPM is listening according to what you have configured in NGINX. Open the www.conf file and search for one of these two options:

listen = /var/run/php5-fpm.sock

or

listen = 127.0.0.1:9000

In Ubuntu

sudo vim /etc/php5/fpm/pool.d/www.conf

In CentOS

sudo vim /etc/php-fpm.d/www.conf

After ensuring what PHP-FPM listening according to NGINX, restart the PHP-FPM service.

You can use one of the following commands to restart PHP-FPM:

Using SysV Init scripts directly:

   /etc/init.d/php-fpm restart # typical
    /etc/init.d/php5-fpm restart # debian-style

Using service wrapper script

   service php-fpm restart # typical
    service php5-fpm restart # debian-style

Using Upstart (e.g. Ubuntu)

   restart php5-fpm # typical (ubuntu is debian-based)
    restart php-fpm # uncommon

Using systemd (newer servers)

   systemctl restart php-fpm.service # typical
    systemctl restart php5-fpm.service # uncommon

If you are getting confused by seeing these list of commands. I personally advise you to use to get your work done:

sudo service php5-fpm restart

Solution 3: Disable APC Cache

Disable APC Cache if used, instead, try Xcache, APC can cause this kind of errors under particular environments causing segmentation faults.

Solution 4

Once you check the log file, you will find error stating some like this:

php5-fpm.sock failed (13: Permission denied) error.

Most of the people get this error because of the PHP update. PHP fixed a security bug where o had rw permission to the socket file.

To get rid of this error:

Open /etc/php5/fpm/pool.d/www.conf

Uncomment all permission lines, like:

listen.owner = www-data
listen.group = www-datalisten.mode = 0660

And finally, restart the PHP-FPM using the command:

sudo service php5-fpm restart

Note: If your web server is running as a user other than www-data, you will require updating the www.conf file accordingly.

If you have any other way to fix this issue, please share them with us in the comment section below or contact me on my email [email protected].

Send this to a friend