5/5 - (1 vote)

Error 502 Bad Gateway means that Nginx cannot receive a response from some service. Gateway means that the error did not occur inside the server itself, but somewhere at the level of communication between it and another program.

What does it mean?

Often Nginx works in tandem with some other application:

  • With Apache and used as a proxy server.
  • Together with PHP-FPM for processing of PHP requests.
  • Together with another service (for example, Varnish or Memcache).
  • Causes of Nginx 502 Error

The most common causes of bad gateway errors:

  • One of the services is simply turned off. You must restart Apache, PHP-FPM, or another service that Nginx works with.
  • Some of the services are not available on the network. If Nginx is located on one server, and PHP-FPM or Apache on another, then error 502 can occur if there is no connection between the servers. Ping and make sure there is no problem.

PHP-FPM

For PHP-FPM, the 502 Bad Gateway error can also occur if a socket is used and there is no access to it:

[crit] 2963 # 0: * 138 connect () to unix: /tmp/php5-fpm.sock failed (13: Permission denied) while connecting to upstream

In this case, make sure that the php-fpm socket is created with the correct permissions. In the fpm settings file (/etc/php-fpm.d/www.conf):

listen = /tmp/php5-fpm.sock

listen.owner = www-data

listen.group = www-data