502 Bad Gateway Nginx in Plesk

Last Update:

Reviewed by Durr E Adan

This post contains affiliate links, and we will be compensated if you buy after clicking on our links.

Read our review guidelines

Facing the error 502 bad gateway nginx in Plesk? It’s difficult to directly solve this error on the first try as there are multiple factors that might cause this error.

This guide covers what causes a 502 error and how you can solve the error of 502 bad gateway in the easiest way possible.

Let’s start.

What is 502 bad gateway nginx in Plesk?

In Plesk, 502 bad gateway nginx error indicates that the Nginx is not able to connect with the Apache server.

In that case, you will receive an error 502 that says bad gateway. It will further say that the “web server received an invalid response while acting as a gateway or proxy server”.

nginx 502 bad gateway error

Before you begin to identify the root cause of the 502 server error, you need to first ensure that Apache is running properly.

You can run the “systemctl status” command in the terminal to find the status of Apache server.

Identify the Cause

You can check the status of PHP-FPM services to begin with. If these services are down, you will get a bad gateway error.

Connect to your server via SSH and run the following command.

sudo service php-fpm status

You can also use the following code to check the status of the service.

service php7.0-fpm status

checking php fpm service status

NOTE: Make sure you replace php-fpm with the specific version of the PHP-FPM service running on your server. For instance, if you are running PHP 7.4, replace it with php7.4-fpm.

Review Nginx and PHP-FPM Logs

The next step is to check the error logs for both the web server (Nginx) and PHP-FPM. Logs maintain all the details if a request fails for some reason. It contains the reasons for the same.

Head over to /var/log/nginx/error.log to find the server logs of Nginx.

nginx server logs configuration

PHP-FPM logs will depend on the configuration you did. If there are no additional configurations, the logs are saved at /var/log/php7.4-fpm.log. Don’t forget to adjust the PHP version name as per your needs.

Now, let’s cover how you can fix 502 bad gateway nginx in Plesk.

How to Fix the 502 gateway error in nginx?

If you already have a root cause from the log, you can directly jump to that root cause and get to the solution.

Restart Services

The Plesk panel mainly depends on two services. The first one is sw-cp-server which is the web server and the next one is sw-engine which handles the generation of web pages. If one or even both of these services are not working properly or stopped, it causes 502 bad gateway.

Start by checking the status of server services and if the service isn’t running, you can restart it by using the following commands.

service sw-engine start

service sw-cp-server start

You can also check the server logs of these services at the following location: /var/log/sw-cp-server/error_log.

You can even stop and start the services again by using the “stop” command via systemctl.

restarting sw engine and sw cp server services

Check Server Load

Even server overloading could cause a bad gateway. In this case, you’ll have to identify and fix the overloading issues.

  • Using tools like top, htop, etc. could help you check server load in Linux.
  • Using Task Manager can help you find the load in Windows.
  • You can even use Plesk monitoring resources to identify CPU usage, memory, etc.

You can use the following commands to check the resource usage.

  • Use the command “df” to check free disk space.
  • Use the command ”free” to check available memory.
  • Use the command “w” to get CPU details and uptime.

sample output of df command

Fix the configuration parameters

Sometimes, there are issues with the configuration. You can check the sw-cp-server file configurations such as buffer sizes, timeout and execution time settings.

Start with the below-given values and increase it as needed depending on your web app and server. You can check the complete list at Nginx documentation.

  • fastcgi_buffers: 8 16k (8 buffers, each 16 kilobytes)
  • fastcgi_buffer_size: 16k (16 kilobytes per buffer)
  • fastcgi_read_timeout: 60s (60 seconds)
  • fastcgi_send_timeout: 60s (60 seconds)

nginx server configuration

Increase Server Timeout Values

By default, there is a limited execution time allowed. So, if the query or request is not completed within that time frame, it fails. This may also cause Nginx error of 502 bad gateway in Plesk.

So, you need to head over to the Nginx config file at /etc/nginx/nginx.conf or within the /etc/nginx/sites-available/ directory.

Now, just increase the proxy_read_timeout and fastcgi_read_timeout directives.

configurations for timeout directives

For PHP-FPM, increase the request_terminate_timeout in the PHP-FPM pool configuration file.

Review Nginx Configuration

Usually, there are two directives where you might have misconfigured nginx so it’s throwing a 502 error.

  • proxy_pass: This directive handles the Nginx when it is used as a proxy to an upstream server. You need to specify the URL if it’s to another URL or non-PHP file.
  • fastcgi_pass: This directive is used to pass the request to an upstream server. You need to specify the socket or IP address and port where PHP-FPM is listening.

nginx proxy pass directive configuration

Adjust Firewall Settings (Fail2Ban)

Before you switch to the Fail2ban firewall, you need to whitelist all IPs on your local host. Fail2ban has a trusted IP address list. It’s located at Tools & Settings >> IP address Banning(Fail2ban) >> Trusted IP Addresses. Manually add all the IPs.

adding trusted ips in fail2ban

Open Blocked Ports

There are two main ports you need to check. You can unblock them from the firewall.

  • 443 or 8443 for https connections
  • 80 or 8880 for http connections.

You can use the following command line to unblock it.

sudo iptables -A INPUT -p tcp –dport 80 -j ACCEPT

Do the same for all the ports.

Update or Disable Faulty Plugins/Modules

If you have plugins or modules, they might be causing the issues. You need to disable all of them and reload the web app again. You can always reactivate all the modules one by one to find out which one of them is causing the issue.

Running the following commands help you remove the modules.

apt-get remove <dynamic_module_name>

delete faulty plugins modules

Contact Support

Nothing working? Don’t worry, you still have two options. Either you can contact your hosting support for help or contact the Plesk support for help. Make sure you mention the OS as steps may vary depending on your environment.

plesk support

Conclusion

The 502 bad gateway error of nginx in Plesk can be caused by a variety of reasons. It could be as small as Apache not working or much more complex like configuration issues. So, you need to start small and investigate multiple areas to find the cause and solve the 502 error.

One of the above-mentioned methods will work. If not, you can always contact the support.

How useful was this article?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Comment