A 502 error, which is most commonly referred to as “Bad Gateway Error” is generally caused due to timeout of PHP request. A gateway is an access point or bridge that establishes a connection between two services.
In order to prevent this error, you need to adjust some server settings i.e. you can increase “Execution Limit” from server settings, by following this article.
Fix 502 Bad Gateway Error on Nginx
Method 1. Changes in Nginx Config
fastcgi_buffers 8 16k;
Method 2. change PHP-FPM to listen on a unix socket or TCP socket.
listen = /var/run/php5-fpm.sock
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