Nginx 502 Error: Backend Problem

Diagnosing communication between Nginx and PHP-FPM.

The 502 Bad Gateway error with Nginx means the reverse proxy cannot get a valid response from the backend, usually PHP-FPM. It's a communication problem between the two services.

Causes vary: PHP-FPM crash, timeout, misconfigured socket, or exhausted resources. This guide helps you identify the exact source of the problem.

Main Causes

  • PHP-FPM down: PHP-FPM service is stopped or crashed. No worker available to process requests.
  • Socket doesn't exist: Nginx tries to connect to a Unix socket that doesn't exist or has wrong permissions.
  • Upstream timeout: PHP-FPM takes too long to respond and Nginx gives up.
  • Exhausted workers: All PHP-FPM workers are busy, new requests are rejected.

Diagnosis

  • PHP-FPM status: Check that PHP-FPM is active: systemctl status php-fpm.
  • Nginx logs: Check /var/log/nginx/error.log for exact message (upstream, timeout, connection refused).
  • Socket/Port: Verify socket or port configured in Nginx matches PHP-FPM.

Solutions

  • Restart PHP-FPM: systemctl restart php-fpm to relaunch the service.
  • Increase workers: Adjust pm.max_children in PHP-FPM config based on available memory.
  • Increase timeouts: Configure fastcgi_read_timeout in Nginx if legitimate scripts are slow.

Frequently Asked Questions

How to see if PHP-FPM is overloaded?

Enable pm.status_path in PHP-FPM to see active/idle workers. If all are "active", you're short on workers.

Socket or TCP, which is better?

Unix Socket is faster on the same server. TCP is needed if PHP-FPM is on a different server.

Why does PHP-FPM crash silently?

Often due to memory overflow. Check PHP memory_limit and system memory. Check PHP-FPM logs.

Does MoniTao detect 502 errors?

Yes, MoniTao monitors HTTP code and alerts you immediately on 5xx errors including 502.

Ready to Sleep Soundly?

Start free, no credit card required.