How to Fix Nginx 502 Bad Gateway Error on Plesk

If you are running a high-traffic website on a Plesk server, you might occasionally run into the dreaded 502 Bad Gateway error. This error typically occurs when Nginx, acting as a reverse proxy, fails to get a valid response from the backend service (usually Apache or PHP-FPM).

For developers and system administrators, a 502 error can be frustrating, but it is usually easy to fix once you pinpoint the bottleneck. In this guide, we will walk you through the most common causes of the Nginx 502 Bad Gateway error on Plesk and how to resolve them permanently.

Step 1: Check the Error Logs First

Before making any configuration changes, you need to know exactly why Nginx is throwing the 502 error. Plesk makes it easy to check domain-specific logs.

You can view the logs via the Plesk Panel by navigating to Domains > example.com > Logs. Alternatively, if you have SSH access, you can tail the error log directly:

tail -f /var/www/vhosts/system/example.com/logs/error_log

If you see an error like recv() failed (104: Connection reset by peer) while reading response header from upstream, the issue is almost certainly related to PHP-FPM crashing or timing out.

Step 2: Increase PHP-FPM Memory and Execution Limits

The most common reason for a 502 error is that a PHP script takes too long to execute or consumes more memory than allowed, causing PHP-FPM to forcefully terminate the process.

To fix this, adjust the PHP settings for your domain:

  • Go to Plesk > Domains > example.com > PHP Settings.
  • Increase the memory_limit (e.g., from 128M to 256M or 512M).
  • Increase the max_execution_time (e.g., from 60 to 300).

Step 3: Adjust pm.max_children for High Traffic

If your site receives a sudden spike in traffic, the default PHP-FPM process manager (pm) settings might not be enough. If PHP-FPM runs out of child processes, Nginx will return a 502 error.

To increase the limit, add the following directive in the Additional configuration directives section under your PHP settings:

pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

Note: Adjust pm.max_children based on your server's available RAM. (Each PHP process roughly consumes 30-50MB).

Step 4: Increase Nginx Proxy and FastCGI Timeouts

Sometimes, the backend process is working fine, but it takes longer than Nginx is willing to wait. By default, Nginx might time out after 60 seconds.

You can increase this limit by adding custom Nginx directives. Go to Plesk > Domains > example.com > Apache & nginx Settings, and scroll down to Additional nginx directives. Add the following lines:

proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;

Step 5: Tweak Nginx Buffer Sizes

If your application returns a massive response header (common in complex Laravel or Magento applications), Nginx's default buffer size might be too small, triggering a 502 error.

Add these lines to the same Additional nginx directives box to increase the buffer size:

fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;

After applying the appropriate changes, test your website and review the Nginx and PHP-FPM logs. These adjustments may resolve specific upstream timeout or buffer-related issues, but other causes of 502 Bad Gateway errors require additional investigation.

Still Getting Nginx 502 Bad Gateway Errors?

If your website continues to experience 502 Bad Gateway errors after troubleshooting, the underlying issue may involve PHP-FPM, Nginx configuration, server resources, or application failures. GreenLeafWeb provides professional Linux server management and troubleshooting services for businesses and developers.

Our team can help investigate recurring gateway errors, review server configurations, and identify performance bottlenecks across Linux hosting environments.

Explore Linux Server Management Services

Stop Fighting with Server Errors!

Tired of constantly tweaking server configurations? At GreenLeafWeb.Net, we provide high-performance, developer-friendly Plesk Hosting solutions.

Powered by top-tier German (EEA) data centers and backed by our UK-registered corporate reliability (GREENLEAFWEB LTD), our servers come pre-optimized for Laravel, WordPress, and heavy PHP applications.

👉 Discover Our Plesk Hosting Plans and let our 24/7 expert team handle the server management for you!

Share: