How to config Home Assistant OS runs on VM for SSL/HTTPS access. I am getting 502 Bad Gateway nginx

I don’t know if this will help, but when I checked the logs it said:

2024/01/19 00:49:20 [error] 592#592: *2496 homeassistant.local could not be resolved (3: Host not found), client: xxx.xxx.x.xx , server: domain name request: "GET /favicon.ico HTTP/1.1", host: "domain name" referrer: "domain name"

I am using duck dns for my domain name. xxx.xxx.x.xx is my VM ip. I use hyper-v.
This might have not been what was causing it. It might be when i was setting up.

Have you been able to solve it? I have a similar issue where I’m getting a 502 Bad Gateway with the following error in NGINX logs:

[error] 807#807: *1 connect() failed (111: Connection refused) while connecting to upstream, client: <ip>, server: homeassistant.*, request: "GET / HTTP/2.0", upstream: "http://192.168.1.53:8123/", host: "homeassistant.<domain>"

I am running HAOS in a VM along with NGINX in a docker container on the same host machine (via swag).

This is my nginx conf file:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name homeassistant.*;

    include /config/nginx/ssl.conf;

    client_max_body_size 0;

    location / {
        include /config/nginx/proxy.conf;
        include /config/nginx/resolver.conf;
        set $upstream_app 192.168.1.53;
        set $upstream_port 8123;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;
    }

    location /api/websocket {
        proxy_pass http://192.168.1.53:8123/api/websocket;
        proxy_set_header Host $host;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

I’ve got many other services running through nginx that work flawlessly.

I’ve got the following in my HAOS configuration file:

http:
  use_x_forwarded_for: true
  trusted_proxies:
    - 192.168.1.16

I’ve gone through every link available, tried all suggestions, to no avail.

I am able to ping the HAOS VM from the host machine no problem.

I’ve spent many hours trying to get this working so now I’m asking you guys if you have any ideas on what might be the problem.

Seems you are missing the location /api/websocket part?

Added it in, still same errors unfortunately.

What happens if you try copying my working config in post 16? Skip the SSL part until you have it up and running.

And when you add
proxy_http_version 1.1;
into the “/”-location ( like you did for /api/websocket )
Based on the error message you seem to pass HTTP/2 to the upstream server
Or remove “http2” from the listen configuration? I’m not sure why you want to make it more complex and run the nginx with HTTP/2 ?

A bit of progress. Although instead of an error, I am now reaching the SWAG landing page instead of homeassistant. Here is the config:

server {
    server_name  homeassistant.<domain>;

    location / {
        proxy_pass http://192.168.1.53:8123/;
        proxy_set_header Host $host;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /api/websocket {
        proxy_pass http://192.168.1.53:8123/api/websocket;
        proxy_set_header Host $host;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}
server {
    if ($host =  homeassistant.<domain>) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen       80;
    server_name   homeassistant.<domain>;
    return 404; # managed by Certbot


}

I tried adding proxy_http_version 1.1; as well as removing “http2” from that config and it didn’t change anything. Http2 was there only because I’ve been trying to follow different templates/suggestions/guides to try get it working.

Also, here is a working config file that I use for Plex hosted in a Docker container:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name plex.*;

    include /config/nginx/ssl.conf;

    client_max_body_size 0;
    proxy_redirect off;
    proxy_buffering off;

    location / {
        include /config/nginx/proxy.conf;
        include /config/nginx/resolver.conf;
        set $upstream_app plex;
        set $upstream_port 32400;
        set $upstream_proto http;
        proxy_pass $upstream_proto://$upstream_app:$upstream_port;

        proxy_set_header X-Plex-Client-Identifier $http_x_plex_client_identifier;
        proxy_set_header X-Plex-Device $http_x_plex_device;
        proxy_set_header X-Plex-Device-Name $http_x_plex_device_name;
        proxy_set_header X-Plex-Platform $http_x_plex_platform;
        proxy_set_header X-Plex-Platform-Version $http_x_plex_platform_version;
        proxy_set_header X-Plex-Product $http_x_plex_product;
        proxy_set_header X-Plex-Token $http_x_plex_token;
        proxy_set_header X-Plex-Version $http_x_plex_version;
        proxy_set_header X-Plex-Nocache $http_x_plex_nocache;
        proxy_set_header X-Plex-Provides $http_x_plex_provides;
        proxy_set_header X-Plex-Device-Vendor $http_x_plex_device_vendor;
        proxy_set_header X-Plex-Model $http_x_plex_model;
    }
}

Not sure if this helps.

EDIT: I noticed I could ping my homeassistant ip 192.168.1.53 from inside the SWAG container, but running wget 192.168.1.53:8123 results in a connection refused error.

EDIT 2: I really want to learn and figure out what the problem is, I’d be willing to set up a remote stream for anyone who’d be willing to have a look.