how to fix 502 bad gateway error in nginx

How To Fix “502 Bad Gateway” Error In Nginx

Spread the love

The 502 Bad Gateway error is an HTTP status code that means that one server received an invalid response from another server. In more technical words, A 502 Bad Gateway means that the proxy (gateway) server wasn’t able to get a valid or any response from the upstream server.

fixing 502 bad gateway error in nginx

If you are seeing a 502 bad gateway error on a website, it means that the origin server sent out an invalid response to another server that acted as a gateway or proxy. It can be a tricky investigation to locate what and where is the process which caused the issue. However, there are some general troubleshooting steps that you can follow to get it solved.

For example, if you use Nginx as the webserver and you encounter this issue, it could be that Nginx is unable to communicate with the upstream server, this upstream server could be PHP-FPM or other application that you want to access through Nginx, like Odoo, NodeJS, etc.

How to Fix 502 Gateway Error in Nginx

First, you need to investigate your Nginx server block and check what the upstream server is.

For example, we have a WordPress website at blog.yourdomain.com, and this is the Nginx server block configuration.

how to repair 502 bad gateway error in nginx

According to the configuration, as shown in the picture above, the website’s PHP files are processed by fastcgi running on port 9000.

To check what is running on port 9000, we can issue this shell command:

$ sudo netstat -pltn | grep 9000

or

$ sudo lsof -i :9000

If a process is running on port 9000, you should see something like this after invoking the shell command.

repairing 502 bad gateway error in nginx

If the shell command does not print anything on the screen, then the process is dead. This is why you see the 502 bad gateway error when accessing your WordPress website.

The next step is to find what pservice should run on port 9000, you can run this command:

$ sudo grep -rl 9000 /etc
removing 502 bad gateway error in nginx

As we can see in the picture, it seems that php-fpm7.2 should run to process the PHP files.

In the latest CentOS and Ubuntu, you can invoke this command to see where the process should be run from:

$ sudo systemctl list-unit-files | grep fpm

or

$ sudo systemctl -l | grep -i fpm
how to remove 502 bad gateway error in nginx

If the process is stopped, this is most likely because your server has run out of memory, although you can try to restart it by running this command:

$ sudo systemctl start php7.2-fpm

But, if you see that php-fpm process is running, it means php-fpm could not respond in time and Nginx is unable to communicate with it, hence the issue.

To solve this php-fpm issue, we can tweak Nginx and PHP-FPM configurations.

Increase the timeouts and buffers between NGINX and PHP-FPM.

Increasing the buffers and timeouts gives NGINX / PHP-FPM space to work, particularly if you have any heavy PHP scripts. In the HTTP or location block of your NGINX site configuration, add the following to increase buffers and timeouts:

location {
…
   fastcgi_buffers 8 16k;
   fastcgi_buffer_size 32k;
   fastcgi_connect_timeout 60;
   fastcgi_send_timeout 300;
   fastcgi_read_timeout 300;
}

Change PHP-FPM configuration

PHP-FPM can be configured to listen on a file or tcp/ip as a socket. If you configure php-fpm to listen on tcp/ip as a socket, you would want to switch to a file because theoretically the file saved on a disk could be accessed faster.

To do this, in your PHP-FPM configuration file, you need to change the following: listen = 127.0.0.1:9000

to this: listen = /var/run/php7.2-fpm.sock

You can choose whatever the socket name is, just remember and make sure your Nginx is configured to connect to the socket file

After making the changes, you need to adjust your Nginx server block as well. In your Nginx server block, change this line

fastcgi_pass 127.0.0.1:9000; to fastcgi_pass unix:/var/run/php7.2-fpm.sock;

Then, check your Nginx configuration with nginx -t and reload both Nginx and php-fpm services.

$ sudo systemctl reload nginx
$ sudo systemctl reload php7.2-fpm

If the PHP-FPM processes are running but you are seeing the 502 error message, then you would need to tine tune your PHP-FPM’s process management to utilize the available memory on the server properly.

There are three process manager options you can choose from.

static – a fixed number of child processes, i.e the value of pm.max_children dynamic – the number of child processes are set dynamically ondemand – children will be forked when new requests will connect

By default, the dynamic process manager is activated, if you use this process manager and see a similar message below in the PHP-FPM log:

WARNING: [pool nnnnn] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 64 children, there are 8 idle, and 79 total children

then this can trigger the 502 error message on your site, you need to adjust the pm.start_servers, or pm.min/max_spare_servers values to get this fixed. Or, use ‘ondemand’ instead of ‘dynamic’. The ‘ondemand’ will be more efficient, although you need to calculate them based on your server’s amount of RAM, how much a PHP-FPM process consumes memory, etc.

With ‘static’ as the process manager, you will keep everything sitting in memory, traffic spikes will only cause fewer spikes to your CPU, this will make your CPU average lower, but RAM usage may be higher.

If your website is using another application and connected to Nginx as the reverse proxy and you see the 502 error message, most likely the application behind Nginx is down.

fix nginx 502 bad gateway error

The picture above tells us that Nginx is connected to an Odoo instance on port 8069. We can check whether Odoo is running or not, by running the netstat command as mentioned earlier in this blogpost.

$ sudo netstat -pltn | grep 8069

or

$ sudo lsof -i :8069

If none of the above commands print anything, then it means Odoo is down. You need to check the service name and then start it.

$ sudo systemctl -l | grep -i odoo
$ sudo systemctl start odoo

To further investigate why Odoo or PHP-FPM is down, we can run this command:

$ sudo dmesg -T | egrep -i 'killed process'

It might print something like this:

[Tue Jul 13 22:59:47 2021] Out of memory in UB 1644: OOM killed process 27128 (phyton3) score 0 vm:6107440kB, rss:113576kB, swap:0kB

It means your server is running out of memory, you would need to tweak your Odoo configuration. You can check one of our Odoo related posts at How to Speed up Odoo – RoseHosting or contact our support team if you have a server with us.


Of course, you don’t need to get troubled with your server if you have a managed VPS hosting with us – in which case, our technical support team will help you investigate and solve this 502 Bad Gateway issue immediately. They are available 24/7 and can cater to any questions or requests.

PS. If you liked this post, feel free to share it with your friends by using the social media share shortcuts below, or simply leave a comment. Thanks.

Leave a Reply

Your email address will not be published.