HTTP 502 Error: Bad Gateway

When your proxy receives an invalid response from the upstream server

The HTTP 502 "Bad Gateway" error is a status code indicating that a server acting as a gateway or proxy received an invalid response from the upstream server to which it was forwarding the request. It's one of the most common 5xx errors in modern architectures using reverse proxies, load balancers, or CDNs.

Unlike the 504 (Gateway Timeout) where the upstream server doesn't respond at all, the 502 means the server did receive a response, but that response was invalid or incomprehensible. This can result from an application crash, a malformed HTTP response, or an unexpected connection closure.

For DevOps teams and system administrators, quickly understanding and resolving 502 errors is crucial. MoniTao monitors your endpoints continuously and alerts you as soon as a 502 error is detected, enabling rapid intervention before your users are massively impacted.

Common causes of 502 errors

The 502 error can have many origins, all related to communication between the proxy and the backend server:

Architectures where 502 occurs

Understanding where the proxy sits in your stack helps better diagnose the 502 origin:

Diagnosing 502 errors

A methodical diagnosis allows you to quickly identify the cause of the 502:

Nginx configuration example to avoid 502

Good timeout and connection count configuration prevents many 502 errors:

# /etc/nginx/nginx.conf
upstream php_backend {
    server unix:/var/run/php/php8.2-fpm.sock;
    keepalive 32;  # Persistent connection pool
}

server {
    location ~ \.php$ {
        fastcgi_pass php_backend;
        fastcgi_connect_timeout 60s;
        fastcgi_send_timeout 60s;
        fastcgi_read_timeout 60s;

        # Buffer to avoid truncated responses
        fastcgi_buffer_size 32k;
        fastcgi_buffers 8 16k;

        # Retry on another backend if available
        fastcgi_next_upstream error timeout;
        fastcgi_next_upstream_tries 2;
    }
}

This configuration establishes a keepalive connection pool to PHP-FPM, defines appropriate timeouts, and configures sufficient buffers to avoid truncated responses.

Monitoring 502 errors with MoniTao

MoniTao offers several features to detect and respond to 502 errors:

Quick resolution checklist

  • Verify backend is running: ps aux | grep php-fpm
  • Check error logs: tail -f /var/log/nginx/error.log
  • Check memory: free -h and dmesg | grep -i oom
  • Test socket: curl --unix-socket /var/run/php/php-fpm.sock http://localhost/
  • Restart backend if needed: systemctl restart php8.2-fpm
  • Monitor after fix to confirm resolution

Frequently asked questions about 502 errors

What's the difference between a 502 and a 503 error?

The 502 Bad Gateway indicates an invalid response from the backend (the backend responded but poorly). The 503 Service Unavailable indicates the service is temporarily unavailable (overload, maintenance). The 502 points to a communication problem, the 503 to a capacity problem.

Why do I have intermittent 502 errors?

Intermittent 502s are often load-related: under heavy pressure, some requests exceed the timeout or exhaust available workers. It can also indicate a memory leak in the application causing random crashes.

My CDN shows 502 but my site works directly, why?

The CDN cannot reach your origin server correctly. Check firewall rules (CDN uses specific IPs), SSL certificates if CDN connects via HTTPS, and CDN-side timeouts which may be shorter.

How to increase Nginx timeout to avoid 502?

Increase proxy_connect_timeout, proxy_send_timeout and proxy_read_timeout directives in your Nginx configuration. For PHP-FPM, use fastcgi_read_timeout. Warning: timeouts that are too long mask performance issues.

Can 502 errors be caused by DNS issues?

Not directly, as DNS is resolved before TCP connection. However, if your proxy uses a domain name to reach the backend and DNS resolution fails or returns a wrong IP, this can cause a 502.

How does MoniTao help me diagnose 502 errors?

MoniTao records each check with response code, response time and headers. History helps identify when 502s started, their frequency, and whether they correlate with events (deployment, traffic spike).

Conclusion

The HTTP 502 Bad Gateway error is a symptom of a communication problem between your proxy and your backend. Resolution requires methodical diagnosis: verify the backend is running, check logs, test direct connection, and adjust timeouts if necessary.

With MoniTao, you're alerted as soon as a 502 error occurs, allowing you to intervene before user impact becomes critical. Configure monitors on your sensitive endpoints and use history to identify recurring patterns.

Ready to Sleep Soundly?

Start free, no credit card required.