By | February 2, 2018

Sponsored

Nginx is one of the fast performing web servers on the internet. Nginx and php-fpm are the most used combinations on the internet to server PHP websites. Sometimes we came across a very bad Nginx 502 Bad Gateway error generated by Nginx server and it is not very easy to identify the problem and fix. Here are some quick solutions to fix Nginx 502 Bad Gateway error on a web-server.

Nginx 502 Bad Gateway Error

Nginx 502 Bad Gateway Error

How Nginx 502 Bad Gateway Error happens and what is its connection with php-fpm

Common 502 errors include the following messages from Nginx server

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

These errors point to the same situation of Nginx server and the error came across below situations

Sponsored

  • Nginx server running with php-fpm service
  • Nginx is running as a reverse proxy to Apache server
  • Nginx running as a gateway to other services like Python, Ruby, etc.
  • Bad configuration of cache or timeout.

Now the error says there is a “BAD GATEWAY”. That means Nginx server is trying to communicate to another service through a gateway and it is not getting any answer from that gateway. This is Nginx 502 Bad Gateway error situation.

So Nginx and another service (Apache, php-fpm, other services) running. Both are communicating through a gateway and Nginx is not getting any answer from the gateway. To solve 502 Bad Gateway Error the other service running with Nginx must answer through the gateway.

Usually, this happens due to a misconfiguration in the configuration files of the server.  Or the other service is not running properly with Nginx. So to solve this problem a two-step check is required.

  • Step 1: Check the other server status, whether it is running perfectly, is it giving the desired output?
  • Step 2: Is the configuration correct? Is the second service configured to answer the gateway perfectly?

Nginx 502 Bad Gateway Error

Nginx 502 Bad Gateway Error with PHP-FPM

The problem occurs due to the following conditions

  • PHP-FPM won’t get started
  • PHP-FPM not running
  • NGINX can’t communicate with PHP-FPM server
  • NGINX is timing out
  • PHP-FPM is timing out with requests

Check whether php-fpm is running or not

service php-fpm status

or

systemctl status php-fpm.service

will tell you about the current situation of php-fpm server

Just restart the php-fpm server to solve this problem

service php-fpm restart

or

systemctl restart php-fpm.service

for php5 the name of service may be php5-fpm, for PHP 7 it may be php7.0-fpm or php7.1-fpm or php7.2-fpm check accordingly.

Php-fpm is not communicating with Nginx and causing a 502 Bad Gateway Error

This is caused due to the misconfiguration of the php-fpm server or the misconfiguration of Nginx server.

Check the following configuration files in the worker pool configuration (the file is in /etc/php/fpm/pool.d/www.conf, this may vary with installation) and verify these lines ( www-data is the owner of the web server process, that is nginx)

user = www-data
group = www-data

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

listen.owner = www-data
listen.group = www-data

then restart the php-fpm service.

If the Nginx server and php-fpm are running and still getting a 502 Bad Gateway error then it is the problem with communicating the gateway. The problem is happening due to the misconfiguration in the file /etc/nginx/sites-enabled/default, by default or the file name in the name of the website called /etc/nginx/sites-enabled/mywebsite.com.conf

Look at the segment where the fastcgi_pass configuration. It must be the same as the listen in the above configuration

      location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php7-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
                include fastcgi_params;

        }

See

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

and

fastcgi_pass unix:/var/run/php7-fpm.sock

must be the same.

Then restart both Nginx and php-fpm servers. Now check the website whether it is serving properly.

502 Bad Gateway Error due to Nginx is timing out

if php-fpm is running and serving the requests properly then there will be a chance of Nginx is timing out. That is php-fpm do not respond to NGINX in a timely manner will cause a 502 because NGINX couldn’t wait any longer for a response from php-fpm.

In this case, increasing the maximum execution time of the application and NGINX’s timeout window would be the best solution.

To get this we need two changes:

First, increase the maximum execution time of the php-fpm server. To do this open PHP-FPM’s configuration file

sudo nano /etc/php7/fpm/php.ini

find the following line and increase the number to a higher value. Usually 60 or 120.

max_execution_time = 30

The value depends upon the expected execution time of the application running in the php-fpm server. Add some extra buffer time to get rid of the situation again.

Exceptionally large execution time will affect the server performance and it may lead to a dead server process in the memory.

Second, edit the NGINX config

sudo nano /etc/nginx/nginx.conf

add the following within the HTTP block to increase timeout windows, buffers, and buffer sizes:

http { 
...

fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}

Save the changes to the file and exit. Now restart Nginx and php-fpm

service php-fpm restart

or

systemctl restart php-fpm.service

and
service nginx restart

 

systemctl restart nginx.service

502 Bad Gateway Error due to PHP-FPM is timing out

if the php-fpm server is timing out upon execution of an exceptionally long PHP script or the script is blocked by a large MySQL query process then these solutions may not work. In that situation, you could temporarily extend PHP’s execution time limit by invoking the set_time_limit() function from within the PHP application, but if you are continuously reaching into the execution timeout limit then as a long-term solution like profiling or increasing PHP-FPM’s timeout may be more appropriate to get rid of a 502 error more safely. These things must be done after an extensive research of the execution time of the corresponding PHP function or MySQL query. Take a look at the memory and system load during the execution is a must for a smooth operation of this server.

Nginx 502 Bad Gateway Error when Nginx as Proxy for Apache

In this situation, the gateway service is Apache and the Nginx is a proxy for Apache. If apache server dies or it’s not well configured, it can cause this 502 Bad Gateway error. How to fix the apache error? 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.

Nginx with other services/apps and 502 Bad Gateway Error

In this situation, the app is gone not responding. Try restarting the other service behind Nginx server and check the logs created by the application to find the exact reason why a 502 Error happened.

Check the status codes HTTP Status Codes on W3C for more details.

Sponsored

Share your Opinion