What Are 502 Bad Gateway and 504 Gateway Timeout Errors? Causes and Fixes

Sunucun 5 min read

Short answer: A 502 error (502 Bad Gateway) means the intermediate server that received the request (a gateway, reverse proxy, CDN or load balancer) got an invalid response from the server behind it. A 504 error (504 Gateway Timeout) means the intermediate server didn’t get a response in time from that server. Both errors indicate a problem on the server side, not with the visitor.

Illustration: What Are 502 Bad Gateway

How Do 502 and 504 Errors Happen?

When you open a website, your request often doesn’t go straight to the application that runs the site. In between there is a reverse proxy such as Nginx, a CDN such as Cloudflare, or a load balancer. This layer forwards the request to the server behind it (PHP-FPM, Node.js, another web server) and sends you the response. The HTTP standard (RFC 9110) defines the two errors as follows:

  • 502 Bad Gateway: The server, acting as a gateway or proxy, received an invalid response from the server it contacted to fulfil the request.
  • 504 Gateway Timeout: The server, acting as a gateway or proxy, did not receive a timely response from the upstream server.
502 Bad Gateway504 Gateway Timeout
What happened?The backend returned an invalid response or closed the connectionThe backend didn’t respond before the timeout
Common causesPHP-FPM/application service stopped or crashed, wrong socket/port, response header too large, firewall blockLong-running query or job, overload, a slow external API, a short timeout setting
Where to look firstService status and the web server error logSlow queries, resource usage and timeout values

If You Are a Visitor: What Can You Do About a 502 Error?

Because the problem is on the server side, there is little a visitor can do, but these steps help with temporary issues:

  1. Wait a few seconds and reload the page (F5). A 502 often appears while a server is restarting or during a short spike in traffic.
  2. Try the site in another browser, a private window or over mobile data. If it only happens to you, clear your browser cache.
  3. If you see “error code 502” in an app (for example a streaming app such as beIN CONNECT), close and reopen the app and make sure it is up to date. If the problem continues, it is most likely on the service’s own servers; check its announcements or support channel.
  4. If the problem lasts for minutes, let the site owner know.

If You Run the Site: How to Fix a 502 Error

1. Check that the backend service is running

On servers running Nginx + PHP-FPM, the most common cause of a 502 is that the PHP-FPM service has stopped. The service name depends on the PHP version (for example php8.3-fpm):

systemctl status php8.3-fpm nginx
sudo systemctl restart php8.3-fpm

For an application running on Node.js, Python or Docker, apply the same logic: check that the application process is up and listening on the port or socket the proxy forwards to.

2. Read the error log

The web server’s error log shows the cause fastest:

# Nginx
sudo tail -n 50 /var/log/nginx/error.log
# Apache (Debian/Ubuntu and RHEL-based)
sudo tail -n 50 /var/log/apache2/error.log
sudo tail -n 50 /var/log/httpd/error_log

Common lines in the Nginx log and what they mean:

  • connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory): PHP-FPM isn’t running, or the socket path in the Nginx configuration is wrong.
  • connect() failed (111: Connection refused) while connecting to upstream: The backend application isn’t listening on the given port.
  • upstream prematurely closed connection: The backend closed the connection before finishing the response; usually an application error or a memory limit.
  • upstream sent too big header: The response headers are larger than the buffer; increase fastcgi_buffer_size or proxy_buffer_size.

3. Check resource usage

Check CPU and RAM usage with top or htop, and memory with free -m. When memory runs out, the operating system can kill processes, which leads to a 502; you can see this in “Out of memory” entries in the dmesg output. If resources are constantly tight, the server needs optimization or a plan with more resources.

4. Check the CDN and firewall

If you use a CDN such as Cloudflare, its error page usually shows whether the problem is at the CDN or at the origin server. A 502 can also appear if the origin server’s firewall or a security plugin blocks the CDN’s IP addresses; make sure the CDN IP ranges are allowed.

How to Fix a 504 Gateway Timeout

If the log shows upstream timed out (110: Connection timed out) while reading response header from upstream, the backend didn’t respond within the proxy’s waiting time. First find the cause of the slowness: slow database queries, large import/export jobs or an unresponsive external API are the most common. Moving long-running work to the background (a queue or cron) is the lasting fix.

If the job really has to take long, raise the related timeout values together; raising only one isn’t enough:

LayerSetting
Nginx → PHP-FPMfastcgi_read_timeout
Nginx → another application (proxy_pass)proxy_read_timeout, proxy_connect_timeout, proxy_send_timeout
PHP-FPM poolrequest_terminate_timeout
PHPmax_execution_time
# Inside an Nginx server or location block
fastcgi_read_timeout 120s;

# After the change
sudo nginx -t && sudo systemctl reload nginx

On shared hosting you usually can’t access these settings; if the problem persists, contact your hosting provider. To manage the settings yourself you need a server with root access.

Illustration: What Are 502 Bad Gateway

Preventing 502 and 504 Errors

  • Set up monitoring: Use a tool that watches site availability and server resources so you are alerted when errors start.
  • Restart services automatically: With systemd settings such as Restart=on-failure, a crashed process comes back up on its own.
  • Size resources correctly: PHP-FPM’s pm.max_children should match the server’s memory; too low and requests wait, too high and memory runs out.
  • Move long jobs to the background: Reporting, bulk email and imports shouldn’t run inside a web request.
  • Keep software up to date: Keeping the application, plugins and PHP version current reduces known bugs that cause crashes.

Conclusion

A 502 error means the backend server returned an invalid response; a 504 error means it didn’t respond in time. For visitors, waiting and reloading is usually enough; for site owners, the service status, the error log and the timeout settings quickly reveal the cause. If you want to manage server resources and settings yourself, take a look at our VDS server options, and for performance help, our server optimization service.

Categories

Latest guides