[SOLUTIONS] 502 Bad Gateway Error on Nginx

admin

January 2, 2016

Nginx, Tutorial

2 Comments

If you run a Nginx web server you may have already encountered the annoying 502 bad gateway errors. This is pretty common error, are generated most probably by the PHP or  FastCGI buffer and timeouts settings. This tutorial shows you how to fix nginx 502 bad gateway on the nginx webserver. This post shows how to fix this problem, and the configuration option to prevent it occurring again on reboot.

This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. The tweak is quite simple. I will show you through the step by step solve 502 bad gateway error on Nginx

Method 1. Changes in Nginx Config

#nano /etc/nginx/nginx.conf
 
http {
    ...
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    ...
}

Method 2. change PHP-FPM to listen on a unix socket or TCP socket.

#nano /etc/php-fpm.d/www.conf
 
listen = /var/run/php5-fpm.sock
 
To:
 
listen = 127.0.0.1:9000

If you are configuring php-fpm to listen on a Unix socket, you should also check that the socket file has the correct owner and permissions.

chmod 0660 /var/run/php5-fpm.sock
chown www-data:www-data /var/run/php5-fpm.sock

Method 3. Disable APC

APC caching can cause 502 Bad Gateway issues under particular environments causing segmentation faults. I highly suggest using Memcache(d), but XCache is also a good alternative.

 

Solving “502 Bad Gateway” with nginx & php-fpm

After upgrading php-fpm, my PHP-based sites were returning “502 Bad Gateway” errors. This can happen when the php5-fpm package reconfigures itself to listen on a different socket. Here’s how you can solve it.

Check to make sure that php-fpm is running with ps aux | grep php – if you can’t see any php-fpm processes in the output, then you may need to re-install php-fpm. If php-fpm is running okay, then skip this first step.

sudo apt-get remove php5 php5-cgi php5-fpm
sudo apt-get install php5 php5-cgi php5-fpm

The thing to notice here is that the order in which you install the packages is important. In the past I have found that installing them in the wrong order causes the packages to be configured incorrectly.

Next, get php-fpm to listen on the correct host/port. In/etc/php5/fpm/pool.d/www.conf change the listen value to match the fastcgi_passlocation in your Nginx configuration. For example, I changed mine from:

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

To:

listen = 127.0.0.1:9000

If you are configuring php-fpm to listen on a Unix socket, you should also check that the socket file has the correct owner and permissions. While I wouldn’t recommend it, you can simply give read-write permissions to all with sudo chmod go+rw /var/run/php5-fpm.sock.

Restart php-fpm with sudo service php5-fpm restart and everything should work normally again.

Read also:

[SOLUTIONS] 500 INTERNAL SERVER ERROR PHP

[SOLUTIONS] 504 GATEWAY TIMEOUT NGINX

Source:

How To Fix 502 Bad Gateway Error on Nginx

Solving “502 Bad Gateway” with nginx & php-fpm

Related Posts

Finding a DropShipping Wholesaler

June 5, 2016

Tutorial

So now you are probably wondering how you can find and qualify a genuine wholesale drop shipper. You will need some patience and time to search through a drop shipper directory in order to find the best drop shipping companies for your particular product niche. There are many drop shipper directories available on the Internet, […]

Read More

Enabling/Installing FFmpeg Extension PHP From cPanel

May 29, 2016

PHP, Tutorial

FFmpeg is php extension used to manipulate videos files in PHP using the ffmpeg program. It provides classes for retrieving information from video and audio files like the duration, frame and sample rate, frame size. It can also extract individual frames from the videos and create GD image resources. It supports the all the video formats supported by ffmpeg […]

Read More
  • http://www.websitevidya.com/ Website Vidya

    Unfortunately, this is not the right solution for 502 Bad Gateway Error.

  • http://design1online.com Jade Krafsig

    Updating the php5-fpm listen.mode and then restarting php5-fpm fixed it for me