connect() failed (111: Connection refused) on Nginx in front of a Node app. Where now?

502nginxreverse-proxy27 replies · 417 views · started
  1. hmmmm ok getting 502 Bad Gateway every few hours at random, and its starting to cost us orders.

    502 Bad Gateway

    502 Bad Gateway
    nginx
    

    The setup is Nginx in front of a Node app on a small VPS 😅 CPU graphs look normal, disk isnt full. The proxy itself is clearly alive cuz it serves teh error page. So something behind it is dying or not answering, but what?? Id appreciate a checklist from someone who has hunted these before. Detailed answers weclome, Im comfortable on the command line. seriously need help here 😭

    1
  2. after reboots and deploys, teh classic causes are a socket path mismatch (fastcgi_pass unix:/run/php/... pointing at a socket name that changed), teh app not being enabled at boot (systemctl enable yourapp), or a container listening on 127.0.0.1 inside Docker sooo teh proxy container cant reach it, bind to 0.0.0.0 in the container and use the service name in the proxy config. fingers crossed 🤞

    2 2 helpful
  3. hmmm check the app service right after a 502: systemctl status php*-fpm, systemctl status yourapp, or docker compose ps. if it shows restarts or exited state, read its own logs with journalctl -u yourapp -n 200 or docker compose logs app. The real error lives there, not in teh proxy log. lmk how it goes

    1 0 helpful
  4. have u checked your 502 settings? mine were reset after an update. came here to say exactly this

    0 1 helpful
  5. ps unrelated but anyone elses coffee machine also judging them today :)

    8 4 helpful
  6. help!! reading this at 2am instead of sleeping, no regrets im about to throw this thing out the window lol

    6 11 helpful
  7. Look for teh kernel's OOM killer: dmesg -T | grep -i -E 'killed process|out of memory'. small VPSes kill PHP-FPM workers, Node or database processes under load, which produces 502s during traffic spikes. add swap, reduce worker counts, or move to a bigger plan. fingers crossed 🤞

    5 3 helpful
  8. oh ive been there lol make a backup before u touch teh fan curve, learned that teh hard way good luck man!

    2 1 helpful
  9. wait what everyone keeps saying this but nobody posts proof but hey ymmv

    5 0 helpful
  10. Make the app restart itself: Restart=always in the systemd unit or restart: unless-stopped in Compose. That's not a fix for the root cause, but it turns minutes of downtime into seconds while you investigate.

    2 0 helpful
  11. hmmm for PHP-FPM, check its log for server reached pm.max_children setting... when all workers are busy, new requests queue and eventually fail, tune pm.max_children based on available RAM divided by average worker memory, not by guesswork. hope that helps :)

    0 1 helpful
  12. my moms pc is officially haunted... honestly just reset the fan curve and save yourself the pain

    2 0 helpful
  13. A 502 means the proxy reached out to the app behind it and got a broken or no response, read teh proxy's error log first: sudo tail -n 100 /var/log/nginx/error.log for Nginx. connect() failed (111: Connection refused) means nothing is listening; upstream prematurely closed connection means the app crashed or killed the request mid-way.

    4 0 helpful
  14. ok update: did what @viktorz766 said and its fine now

    1 0 helpful
  15. oh ive been there lol exact same thing happened to my mac last month hope that helps :)

    8 5 helpful
  16. dumb question but how do i even check the router

    1 1 helpful

Reply to this topic

Sign in or create an account to reply.