How to Fix 502 Bad Gateway Error Using Nginx in Prestashop website?

How you might see the 502 Bad Gateway error

Different websites may customize the 502 bad gateway error message. Here are the 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”

When you will find 502 Bad Gateway error

  1. Nginx running as proxy for Apache web server.
  2. Nginx running with PHP-FPM daemon.
  3. Nginx running with other services as gateway.
  4. Bad buffering/timeout configuration.

Before getting deep into all of this options, you must understand what does the 502 Bad Gateway error message mean. So, the error is an HTTP Status Code error and it’s happening at the gateway. What the hell is a gateway? In simple words, a gateway, is like an access point, a bridge that communicate one service with another, in this case the gateway can be a service/application that is working and receiving requests from Nginx web server.

Now, let’s explore each one of this reasons to understand the 502 Bad Gateway error message

  1. Nginx as Proxy for Apache: in this case, the gateway is Apache. When you use Nginx as proxy for Apache, if apache dies or it’s not well configured, it can cause this 502 Bad Gateway error. How to fix it? Most of the times, restarting apache web server will get rid of this, but you must check the log files to know why exactly this was caused.
  2. Nginx with PHP-FPM: same as Apache example, let’s imagine your php daemon stop working, or get’s overloaded by requests, it will not work properly and the all the php requests from nginx would not be served, so, a 502 Bad Gateway error will appear. How to fix it? Restart php-fpm daemon and check the logs.
  3. Nginx with other services/apps: try restarting the other service behind nginx and explore the logs to find the reason why it happened.

Other quick solutions for 502 Bad Gateway error:

1) 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;
...
}

2) Ensure your php-fpm service is listening according to what you’ve configured in nginx, it can be either this two options:

Edit www.conf file (in CentOS it is located at /etc/php-fpm.d/www.conf and try with one of this two options:

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

or

listen = 127.0.0.1:9000

After that, just restart the php-fpm service.

3) Disable APC Cache if used, instead try Xcache, apc can cause this kind of issues under particular enviroments causing segmentation faults.

4)  I recently found another cause of 502 Bad Gateway error, check it out here: php5-fpm.sock failed (13: Permission denied) error

Original Post: https://www.scalescale.com/tips/nginx/502-bad-gateway-error-using-nginx/

Tagged with:

Related Articles

Leave a reply

You must be logged in to post a comment.