Plesk hosting configurations often use a hybrid web server architecture, combining Nginx and Apache. In this setup, Nginx acts as a high-performance reverse proxy that handles incoming traffic, serves static files, and passes dynamic requests (like PHP) to Apache in the backend. While efficient, this hybrid design can sometimes experience communication failures, resulting in Nginx 502 Bad Gateway errors. When this occurs, visitors are blocked from loading the website.
For systems administrators, resolving a 502 Bad Gateway error requires identifying whether the issue is due to backend Apache service crashes, PHP-FPM processing limits, or socket timeouts between proxy layers. In this guide, we will analyze Plesk's reverse proxy architecture, inspect error logs, and walk through the SSH commands needed to adjust buffer settings and restore server communications.
1. The Nginx-Apache Reverse Proxy Architecture
In Plesk's hybrid web server setup, Nginx listens on ports 80 and 443, handling external requests. Static files are served directly by Nginx from disk, bypassing Apache. Dynamic requests (such as PHP scripts) are proxied to Apache, which listens on local backend ports (typically 7080 and 7081) or connects directly to PHP-FPM socket files.
A 502 Bad Gateway error indicates that Nginx connected to the backend service (Apache or PHP-FPM) correctly, but the backend returned an invalid response, timed out, or closed the connection unexpectedly. This can occur due to script timeouts, memory exhaustion, or service crashes in the backend processor.
2. Step-by-Step Diagnostics via SSH
To troubleshoot Nginx 502 errors in Plesk, log into your server via SSH as the root user. Run these diagnostic commands to check service statuses, inspect log files, and adjust timeout parameters.
Step 2.1: Check Web Service and PHP-FPM Daemon Status
Verify if the backend web server and PHP-FPM services are active and running. Run the following status commands:
systemctl status httpd # On RHEL/AlmaLinux (Apache)
systemctl status apache2 # On Debian/Ubuntu (Apache)
plesk srvman status php-fpm # Check PHP-FPM status
If Apache or PHP-FPM is stopped, restart the service to restore the backend processor:
systemctl start httpd # Restart Apache
Step 2.2: Inspect Nginx and Apache Error Logs
Plesk logs web server errors in domain-specific directories. Inspect the logs for the affected domain to find details about the 502 block:
# View domain-specific Nginx error log
tail -n 50 /var/www/vhosts/system/domain.com/logs/proxy_error_log
# View domain-specific Apache error log
tail -n 50 /var/www/vhosts/system/domain.com/logs/error_log
If you see log entries like "recv() failed (104: Connection reset by peer) while reading response header from upstream", the backend Apache or PHP-FPM process terminated unexpectedly while processing the request.
Step 2.3: Increase Nginx Proxy Buffers and Timeouts
If log files indicate timeouts (e.g., 'upstream timed out') during database-heavy operations or large uploads, increase Nginx's proxy buffer and timeout settings. Open the Plesk custom Nginx configuration file for the domain or add these settings under the Additional Nginx directives section in Plesk:
proxy_connect_timeout 600s;
proxy_send_timeout 600s;
proxy_read_timeout 600s;
proxy_buffers 16 16k;
proxy_buffer_size 32k;
Apply these configurations globally in Nginx settings by adding them to /etc/nginx/conf.d/timeout.conf, and restart Nginx to update the parameters:
systemctl restart nginx
Step 2.4: Troubleshoot PHP-FPM Resource Limits
If PHP-FPM logs indicate resource limits (such as 'max_children reached'), the PHP processing pool has exhausted its capacity, causing subsequent requests to be rejected. To resolve this, increase the maximum child processes in the domain's PHP Settings interface in Plesk or edit the pool configuration file:
# Edit domain-specific PHP-FPM configuration pool
nano /opt/plesk/php/8.2/etc/php-fpm.d/domain.conf
# Set pm.max_children to a higher value based on RAM (e.g. pm.max_children = 50)
Save changes and restart the PHP-FPM service daemon to apply the update.
3. Nginx 502 Error Resolution Summary
| Log Error Message | Common Root Cause | Recommended Action |
|---|---|---|
| Connection refused to upstream | Apache service is offline or crashed | Start Apache service; check for RAM exhaustion |
| upstream timed out (110: Connection timed out) | PHP script execution exceeded Nginx timeout limits | Increase proxy_read_timeout in Nginx settings |
| Connection reset by peer / Broken pipe | PHP-FPM process crashed during script execution | Increase PHP memory_limit; inspect PHP error logs |
Frequently Asked Questions (FAQ)
What is the role of Nginx in a hybrid Plesk setup?
In a hybrid setup, Nginx acts as a reverse proxy. It listens on public web ports, serves static assets (like images and stylesheets) directly from disk, and forwards dynamic requests (like PHP scripts) to Apache in the backend for processing.
How do I fix a 502 error caused by PHP memory limits?
If a PHP script exceeds the allocated memory limit, PHP-FPM terminates the process, causing Nginx to return a 502 Bad Gateway error. To resolve this, increase the memory_limit value in the domain's PHP settings in Plesk.
Can firewall rules block internal proxy requests?
Yes. If the server firewall blocks local connections on loopback interfaces, Nginx cannot connect to Apache on local ports (7080/7081). Ensure your firewall whitelists local traffic on the loopback adapter.
