How to Fix Nginx 502 Bad Gateway php-fpm Error & Fix on cPanel

502 Bad Gateway is one of the most popular Nginx error messages on the Internet. Sometimes it is a temporary error, and sometimes it is a critical and permanent error, just in case let’s getting started by opening your browser in safe mode and try again making another http request without any browsers cache in the middle, just to be sure.

Today we will find out why this 502 bad gateway error is seen on many pages over the net when you start your browser and hit a page. We will cover how to fix this on Nginx + cPanel based websites, but also on Nginx + plain Linux CentOS servers.

If the error page was customized, you can also find this 502 gateway timeout issue with other phrases/words like this error messages:

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

Nginx 502 Bad Gateway Error

Nginx is a super fast and lightweight web server, that can work as standalone server, or as a proxy. This 502 gateway timeout error can be seen on web browsers (internet explorer, firefox or chrome) and it happens because of a few reasons:

  1. Web server running as proxy for Apache on the backend.
  2. Web server serving PHP using PHP-FPM
  3. Web server running with other services on the backend.
  4. Web server Bad buffering or timeout configuration errors.

In most of the cases, it is related to the server received an invalid response from the backend server (php-fpm) and it simply can’t fulfil the request.

The Bad Gateway

A Bad Gateway is just the backend that connects to Nginx (the front end), on cPanel the most common cause of this is to have Apache down when you are working with Nginx as Proxy for Apache. 502 is the number of the http error (the http status code), it is not related to database servers or dns servers, only http and backend application servers.

How to fix 502 Bad Gateway on cPanel?

Simple, restart Apache and it should be back online:

service httpd restart

However, you may need to find the root cause of the apache failure.

If that doesn’t fix your issue, then it may be a bad buffering configuration on Nginx, try increasing the timeouts from the HTTP block:

http {
fastcgi_buffers 8 32k;
fastcgi_buffer_size 64k;
fastcgi_connect_timeout 300;
fastcgi_read_timeout 300;
fastcgi_send_timeout 300;
}

Make sure you increase the values of your nginx configuration to match this values, altough you can increase them or decreased them if needed by your applications.

If you are using PHP-FPM or other app daemon as Gateway, try to restart that system daemon to get it working:

service php-fpm restart

Again, that may fix the issue temporary, try to explore your app and system logs to find out the root cause of the issue.

For PHP-FPM, you can also explore some common configuration errors on the www.conf file that comes by default with PHP-FPM:

Make sure the php-fpm service is running well listening on the unix socket or on the 9000 port, this can be reviewed manually from www.conf file inside PHP-FPM. You can also try to change from socket to a port by editing the same file, example:

Edit www.conf and try to find ‘listen’ variable:

listen = /var/run/php5-fpm.sock
listen = 127.0.0.1:9000

You’ll also need to tell Nginx the new configuration so he can connect to the socket, or to the port, whatever you choose. This can be done by editing Nginx configuration for that virtual host.

At the time we are writing this, cPanel does not support native Standalone or Proxy Nginx configurations, and you’ll have to use 3rd party Nginx solutions for Nginx, in this cases it’s very common to see this kind of gateway timeout proxy error in the http status.

Another reasons this may happen, is if you use APC as your cache system, try switching to eaccelerator or xcache, are more stable and never generate 502 Bad Gateway Errors.

Conclusion

After reading this tutorial, now your gateway timeout problem should be gone, you won’t see any more 502 bad gateway or proxy response status codes (server errors) while it run php files, start your browser again and test it, you shouldn’t receive any new failing server error response status codes (error codes).

There are many reasons on why this 502 error codes are in your browser, most of the times when you make a http request and get this it’s due to php-fpm failing, but to be sure you will need to investigate the server logs and run serveral tests until you resolve the issue it and there your gateway will be able to fulfil the request on your web sites.

If you are running a CDN for your domain or you just configured a custom CDN integration, this is also very well known due to zone configuration & zone security errors, pull zone settings & push zone settings failing over the CDN setup. If this is your case, ask your CDN provider, or check your logs on the origin servers / upstream servers . If you just set new dns servers, change dns back to your old NS until you can resolve the issue.

In some cases, I’ve also seen firewall blocking the communication between the front end Nginx standalone or proxy server, and the backend due to port restrictions, in this case check your firewall to ensure you ware not blocking your own backend port where it is trying to listen.

Do you know other ways to fix NGINX 502 Bad Gateway error on cPanel or plain CentOS Linux servers? Please share it with us.

Further reading:

About the Author: Martin Keler

Leave a Reply

Your email address will not be published. Required fields are marked *