Other 502Medium

502 Bad Gateway Error: Causes and Quick Fix

Explains why HTTP 502 occurs and how to resolve it from the client or server side. You'll get ready-made diagnostics and connection recovery algorithms.

Updated at April 6, 2026
5-10 min
Easy
FixPedia Team
Применимо к:All modern browsers (Chrome, Firefox, Safari, Edge)Web servers Nginx and ApacheCDN providers (Cloudflare, AWS CloudFront)

What Does Error 502 Mean

The HTTP 502 Bad Gateway status code indicates that a server acting as a gateway or proxy received an invalid response from an upstream server. In simpler terms, an intermediary component (a load balancer, CDN, or local proxy) attempted to communicate with the primary server, but that server returned nothing, terminated the connection, or responded in violation of the protocol. You will see this message in your browser when trying to open a website, web application, or API endpoint.

Causes

Error 502 is rarely related to hardware failure. Most often, it is triggered by the following factors:

  • Overload or crash/unexpected stop of a backend process (PHP-FPM, Node.js, Python Gunicorn).
  • Incorrect proxy_pass parameters or excessively low timeouts in Nginx/Apache configuration.
  • Temporary DNS resolver failure or traffic blocking by a corporate firewall.
  • Exceeding the server's RAM limits, causing a process to be forcibly terminated (OOM-Killer).
  • Application code errors causing infinite loops or worker crashes when processing a heavy request.

Solutions

Method 1: Quick Client-Side Check

If you are a visitor to the resource, start with local actions. These will help rule out cached bad data.

  1. Perform a "hard" refresh of the page: press Ctrl + F5 (Windows/Linux) or Cmd + Shift + R (macOS). This forces the browser to ignore the saved cache.
  2. Open the site in incognito mode. If the error disappears, the culprit is one of your installed extensions. Disable them one by one in your browser's menu to find the conflicting one.
  3. Check access via mobile internet or a different device. If the problem reproduces everywhere, the failure is on the site's infrastructure side, and all you can do is wait.

Method 2: Network Connection Diagnostics

Sometimes a 502 error is generated by local proxies, VPNs, or outdated DNS records.

  1. Temporarily disable your VPN, proxy extensions, and corporate tunnels.
  2. Reset the network stack and clear the DNS cache. Open a terminal or command prompt as an administrator and run:
# For Windows
ipconfig /flushdns
netsh winsock reset

# For Linux
sudo resolvectl flush-caches

# For macOS
sudo killall -HUP mDNSResponder
  1. Reboot your router. A failure in the NAT table of a home router can disrupt long-term connections with web servers, mimicking a gateway error.

Method 3: Troubleshooting on the Web Server

If you are the administrator of the resource, the error indicates a broken connection between the frontend and backend.

  1. Check service status. Ensure the web application process is running and not in a failed state:
systemctl status nginx
systemctl status php8.2-fpm
  1. Analyze the logs. In Nginx, a 502 error is detailed in the file /var/log/nginx/error.log. Look for lines containing connect() failed or upstream timed out — they will indicate the exact IP and port of the crashed worker.
  2. Increase timeouts if the backend processes heavy requests longer than the standard 30 seconds. In the http or server block of your configuration, add:
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
  1. Reload services to apply changes: sudo systemctl reload nginx. If the backend process is constantly crashing, check the application logs (journalctl -u php-fpm -f) for memory leaks or fatal syntax errors.

Prevention

To minimize occurrences of 502 Bad Gateway, configure automatic monitoring of upstream server health using built-in health_check modules. Regularly update your web server and interpreter packages to avoid known stability bugs. Implement an automatic restart system for crashed workers (e.g., supervisord or the pm.max_requests parameter in PHP-FPM). For public projects, use a CDN with graceful degradation functionality, which will serve visitors a cached version of the page if the primary host becomes temporarily unavailable.

F.A.Q.

Is a 502 error a problem on my computer or the website?
How to quickly restore access to a site with a 502 error?
Can a 502 error appear when developing your own application?

Hints

Perform a hard refresh of the page
Clear cache and cookies in your browser
Test the site in incognito mode
Disable VPN, proxy, and external networks
Check web server logs and restart services

Did this article help you solve the problem?

FixPedia

Free encyclopedia for fixing errors. Step-by-step guides for Windows, Linux, macOS and more.

© 2026 FixPedia. All materials are available for free.

Made with for the community