How to Fix 502 Bad Gateway Nginx Error

r00t October 25, 2017

Fix 502 Bad Gateway Nginx Error

In this tutorial we’ll learn how to fix 502 Bad Gateway Nginx error. We will also install and configure its prerequisites. 502 Bad Gateway errors are surprisingly common, and the exact cause of this error code depends on your particular stack. This post is part of a series on troubleshooting NGINX 502 Bad Gateway errors.

Fix 502 Bad Gateway Nginx Error

Step 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;

Step 2. Make sure, your php-fpm service is listening based on what you have configured in nginx, it may be either this 2 choices:

nano /etc/php-fpm.d/www.conf

Try with one of this two options:

listen =


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

You’ll need to reload both PHP-FPM and NGINX afterwards from the command line:

systemctl restart nginx
systemctl restart php-fpm

Step 3. Now we can configure PHP-FPM in www.conf.

PHP-FPM has three options for process management: static, dynamic and ondemand. The default is dynamic and for the purpose of this article that’s what we’ll be configuring:

pm = dynamic

There are four key settings we want to change:

pm.max_children = 97
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 200

  • Pm.Max_children: that is actually the maximum number of procedures PHP-FPM will spool up. This is derived from our calculation above. Here really is the hard limit and total maximum number of processes which will ever be launched by PHP-FPM
  • Pm. Start_servers: this is actually the number of ‘spare’ procedures we want PHP-FPM to twist up as it begins  –  procedures ready and waiting to process requests. Each one will utilize memory and it takes time to spool up new processes, so I chose 20 as a beginning point
  • Pm. Min_spare_servers: this is the minimum number of spare processes we want available (where possible). I chose 10, so if all 20 of my first poem. start_servers are occupied the server will spool up yet another 10 significance 30 operating in complete.
  • Pm. max_spare_servers: this is the maximum number of spare procedures we need available.
  • Pm. max_asks: that one is very helpful. This number specifies how many requests each process should handle prior to is kills itself and re-spawns. In case your code (or even 3rd party libraries) has memory leaks, finally PHP-FPM will eat up your physical RAM and crash (bringing your website down with a 502). By setting this, PHP-FPM continually recreates itself and prevents the ‘memory leak creep’ problem bringing down your web server.

When you’re done you’ll need to restart php-fpm again:

systemctl restart php-fpm

Congratulation’s! You have successfully to fix 502 Bad Gateway Nginx error server on your CentOS 7 server. Thanks for using this tutorial how to fix 502 Bad Gateway Nginx on CentOS 7 system.




The Tags:

Leave a Comment

Comments are closed.