Nginx 502 after every reboot

Hey all,

I am using serving HomeAssistant on my RPi3 through the recommending nginx config with my LetsEncrypt cert. I’ve been using this setup for quite a while and it works great with one exception – after every reboot, nginx gives me a 502 error until I restart nginx. All I have to do is systemctl restart nginx – no other changes – and it is then fine until the next reboot. When I check the nginx logs, I see a bunch of: connect() failed (111: Connection refused) while connecting to upstream, client

Does anybody else have this problem? How have you solved it?

What I think is going on is that nginx is starting up before hass is ready. I tried to fix it by delaying nginx startup until Wants=network-online.target and After=network-online.target where my HomeAssistant service file starts up at After=network.target, but no luck.

I suppose I could just delay nginx startup until 5 minutes after a reboot or something, but this seems sloppy, and would be a pain when I’m working on something and rebooting / testing frequently.

nginx is starting faster than HA. IMO, this isn’t a true issue…

You shouldn’t need to do anything at all with nginx. Just wait until HA is finished starting. If you do nothing with nginx and refresh your browser, you should see that the 502 error is no longer displayed once HA is finished starting.

Unfortunately it is an issue in my case, because it doesn’t resolve once HA is done starting up. As I said above, it gives me the 502 until I manually restart nginx, at which point it is fixed until the next reboot.

That seems very strange. nginx shouldn’t be caching a 502 and every attempt to connect should try again.

Are you sure that you are waiting long enough for HA to start?

What OS are you running on? Are there any other site’s enabled?

You could try creating a custom nginx.service file with some settings to start after your home-assistant.service.

Create a custom nginx.service file that looks something like this:

# Ensure nginx starts after home-assistant starts
 
 .include /lib/systemd/system/nginx.service

 [Unit]
 After=home-assistant.service

Stick the file in /etc/systemd/system and run sudo systemctl daemon-reload

Then when you systemctl reload nginx, it will load the custom nginx.service that you just installed.

Had the issue for months with Raspbian Jessie but fixed it somehow (think by specifying ipv4 address in the nginx server stanza e.g. 0:0:0:0:8180 instead of just 8180 or localhost 8180, thinking it had something to do with dns at the time?). Now back in raspbian stretch.

Pretty sure I’m waiting long enough, usually notice a day or so after a reboot (don’t access the hass web interface or iOS app very frequently).

Uptime says 10 mins since reboot, getting 502s on both my https port and my local http (via nginx) port. Curling the API on localhost works fine.

I’m running on Ubuntu, so not sure about Raspian. It sounds like nginx is binding to ports and IP addresses just fine, but somehow the proxy is not working…

Maybe you can post your nginx config and I can look at it?

I don’t think the 502 is getting cached, I think it is giving up. Looking at configuring the proxy upstream timeout (or retries if available?). Will post config if shortly.

If you’re sure nginx’s the culprit (I really doubt this), you should modify nginx’s systemd Unit file to wait for hass start.

I suggest you make sure IPv6 is not the one to blame here. Specify 127.0.0.1:8123 as the address of the proxy endpoint.

I’m not running hass behind nginx proxy, but I have experience using nginx as a reverse proxy in production environment. What you’re describing (nginx giving up) is not something I’ve ever seen happen. Once the proxied server is back online nginx is happy to serve it, until then - it does show a 502 error.

This was it. Thank you, as I mentioned above I thought the fix last time was something about specifying an ipv4 specific address instead of localhost – I was just changing the wrong one.

2 reboots and going strong.

Anyone know why official docs specify the other way (proxy_pass http://localhost:8123;)?

Because no one ran into this kind of problem or had only IPv4 enabled. In my experience IPv6 is a major culprit in network-related issues. Switching it off is one of the first things I do :slight_smile:

Another way to fix this (and maybe the more “go future” way) is to enable IPv6 in Homeassistant.

Set this in your configuration.yaml:

http:
  server_host: "::0"

I don’t understand why HASS is not listening on an IPv6 Socket by default. This should be default by now…