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_passparameters 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.
- Perform a "hard" refresh of the page: press
Ctrl + F5(Windows/Linux) orCmd + Shift + R(macOS). This forces the browser to ignore the saved cache. - 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.
- 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.
- Temporarily disable your VPN, proxy extensions, and corporate tunnels.
- 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
- 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.
- Check service status. Ensure the web application process is running and not in a
failedstate:
systemctl status nginx
systemctl status php8.2-fpm
- Analyze the logs. In Nginx, a 502 error is detailed in the file
/var/log/nginx/error.log. Look for lines containingconnect() failedorupstream timed out— they will indicate the exact IP and port of the crashed worker. - Increase timeouts if the backend processes heavy requests longer than the standard 30 seconds. In the
httporserverblock of your configuration, add:
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
- 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.