انتقل إلى المحتوى

الدروس استكشاف الأخطاء وإصلاحها متوسط1 دقائق قراءة

How to Fix a 502 Bad Gateway Error

A 502 Bad Gateway means one server got an invalid response from another. Find out whether the problem is a proxy, CDN, PHP process or your app.

بواسطة فريق تحرير ZSOLNET آخر تحديث

هذه الصفحة غير متاحة بالعربية بعد، لذا تُعرض بالإنجليزية.

أهم النقاط

  • A 502 is reported by a proxy that got a bad answer from the server behind it.
  • Test the origin directly to tell CDN problems from server problems.
  • Proxy error logs name the upstream that failed.

A 502 Bad Gateway appears when a server acting as a gateway — a reverse proxy, load balancer or CDN — asked another server for the page and got back something invalid, or nothing at all.

Where the chain breaks

A typical request passes through several layers:

Browser → CDN / proxy → web server (Nginx/Apache) → PHP-FPM or app → database

The 502 is reported by the layer that received the bad answer. Your job is to find which upstream layer failed.

If you’re a visitor

  • Reload after a minute — short 502s happen during restarts.
  • Try another network or device to rule out a local proxy.

If it’s your website

  1. Check whether the origin works. If you use a CDN, test the origin server directly. If the origin loads, the CDN can’t reach it — check firewall rules or the origin address set in the CDN.
  2. Restart the application layer. On a VPS, restart PHP-FPM or your app service and watch whether the error clears:
sudo systemctl restart php8.3-fpm
sudo systemctl status nginx php8.3-fpm
  1. Read the proxy’s error log. On Nginx, look in /var/log/nginx/error.log for lines like connect() failed or upstream prematurely closed connection. They name the upstream that failed.
  2. Look for crashes and limits. An app that runs out of memory gets killed and stops answering. Check dmesg or journalctl for out-of-memory messages, and review whether the server needs more RAM.
  3. Check long-running requests. Slow scripts that exceed the upstream timeout can surface as 502 or 504. Fix the slow code first, then adjust timeouts if needed.

On shared hosting you can’t restart services yourself. Check that your site’s PHP processes aren’t hitting the plan’s limits under Resource Usage in cPanel, and contact support with the time of the error.

الأسئلة الشائعة

What is the difference between 502 and 504?

A 502 means the upstream answered with something invalid or closed the connection. A 504 means the upstream didn’t answer in time.

شارك X LinkedIn Facebook

الدخول إلى حسابك

أدِر نطاقاتك واستضافتك وفواتيرك من مكان واحد.

اتصال آمن مباشرة مع منطقة العملاء في ZSOLNET.

ليس لديك حساب؟