Recovering from persistent 502 Bad Gateway in Node-RED

Recently I experienced the dreaded 502 Bad Gateway problem in Node-RED. It happened after a minor edit in an existing Node-RED flow. The common fixes of changing SSL and credential_secret settings didn’t help, nor did clearing browser caches. Restoring backups from well before the problem occurred did not help. The log file showed a strange UnhandledPromiseRejection error.

If I uninstalled Node-RED and rebooted host, Node-RED would load up but of course without any of my flows or custom nodes. About 95% of my automations are in Node-RED, and I have quite a few, so re-creating all my flows was not a welcome idea. I knew from posts that I could re-import my flows but didn’t find any clear directions to do so, or indications of how complete a rebuild an import provides. Anyway, in case it helps anyone in a similar situation here’s the steps I took:

  1. Remove Node-RED addon.
  2. Rename node-red folder to node-red-old.
  3. Reboot host (not just restart Home Assistant). Currently in Settings, System, Hardware, three dots in the upper right corner, Reboot host.
  4. Install Node-RED addon.
  5. Restart Home Assistant. Currently in Settings, System, Restart in upper right corner.
  6. Configure Node-RED addon with startup toggle settings, credential_secret and proper SSL setting, and Save.
  7. Start Node-RED addon and check on UI that it is running properly.
  8. Import your old flows from the flows.json file in node-red-old folder.
  9. Use text editor to open package.json in node-red-old folder. Read the list of dependencies which are the node names and versions that you will need to add in the Node-RED pallette. Install those using Manage Pallette in Node-RED.
  10. Restart Home Assistant.
  11. Load Node-RED UI to verify all is well.
  12. Delete or preserve in a backup location the node-red-old folder.

Further lessons:

  • After importing my flows and reinstalling ALL the nodes I had previously loaded I still had a 502 Bad Gateway - ouch! Eight of the 36 nodes I had installed were “experimental” nodes for me to test how they worked but were not used in my working flows. So on my second try I loaded only the nodes that were being used. And the nodes I did load, I loaded in small groups and restarted in between groups to be sure I didn’t get the 502 again. My assumption is that one of those “experimental” nodes introduced instability, perhaps during an update of the node.
  • If you are missing a required node, Node-RED will give you a warning that flows cannot be fully loaded because of missing nodes. The warning will give you a good indication of which node is missing.
  • You will have to re-enter username/password for functions like email, text, pushbullet, MQTT. In some of those cases, re-entering in one of the flows will apply to all and in others you will have to find each flow instance and re-enter in each.
  • Importing flows preserves tabs you created previously.
  • Some of the graphical elements may be moved around in some tabs, but in my case it wasn’t too many and easily rearranged.
  • You may find that some of your re-loaded nodes have been updated relative to your old configuration. In my case the Shelly node had been updated and I had to edit those flows to work with the updated node.

All in all, re-importing your old flows is robust and fairly seamless. I didn’t have anything to lose because my old configuration was dead. But I was super happy to find that the process replicated my pre-crash Node-RED almost exactly. Hope this helps someone.

4 Likes

Thanks for this write up. My situation is a little different but this information may come in handy. After updating to Node-Red 13.0.0 it will crash and keep trying to restart but keeps crashing. I rolled back to 12.0.2 and it’s starts okay. I have been having the issue for some time now were if I make any changes and deploy the new flow it takes sometime before I can use it (502 Bad Gateway), but will start after a minute or so. I read through some posts but nothing that really helped. I also have a bunch of “experimental” nodes as well which I’m going to go through and probably get rid of.
Thanks again.

1 Like

Same problem here. I’m on 12.0.2 upgraded to 13.0 and got the 502 bad gateway. Restored the 12.0.2 and tried to upgrade again to 13.2.0 and still got the 502. Grrrr. Back to restoring again.

2 Likes

Yes I still haven’t figured this out yet either! Haven’t had a lot of time on my hands lately but did get rid of my “experimental” flows and that didn’t help. If you come up with a fix please let me know. I will probably start from scratch with Node-Red and reimport my flows one at a time if no other easier solution comes up. I will also go through the nodes under the manage palette to see if any of those are causing problems. I will post here if I find anything interesting. Unfortunately I’m not sure when I will get a chance. Everything this is still working okay with 12.0.2.

1 Like

Node-red crashes are always fun to troubleshoot.

TLDR; something seems less tolerant, or there are breaking changes in node-red. I’ve troublehooted, located an issue, removed it, and now have a stable system on the current (13.x) version.

I’ve posted in the add-on thread - but I see this here is similar. I too had a reliable node-red under 12.x and then upgraded to 13.x and it (node-red) started crashing all the time. Watchdog is enabled, but it didn’t restart it.

I ended up troubleshooting and found one node I’d written that was erroring and causing the crashing - odd that it ran fine for 2 years under previous versions, but not anymore.

My recommendation is to turn the logging up to debug or verbose under the addon config page - this is what showed me what node was causing my problem. Make sure you check the log before you restart node-red, as it seems that it’s cleared when you restart the addon.

Hi Mate. Could you advise me how to remove “bad” node in dead node-red (I’m suspecting it as bad, because after importing it and installing, my node-red became 502: bad gateway) ? Thanks in advance, Val.

It’s not straight-forward, and it’s not in the GUI (because you obviously don’t have one at the moment). You need to edit the flows JSON file which is in your node-red folder. Make a backup of it first. You need to carefully weed out the node object that is causing your issues and remove just that object. Hope that helps. :+1:

1 Like

Hi to all
Is it normal that in a new installation HAos with only NodeRed Add-on installed receive the error Bad Gateway 502?
Thank you in advance