External URL with subdirectory doesn't work behind nginx reverse proxy

Hello,

I have a reverse proxy setup on Kubernetes on machines A, B and C (192.168.5.10, 192.168.5.11, 192.168.5.12) that’s being load balanced, and acting as the SSL termination for mydomain.xyz.

I have Home Assistant setup on a Raspberry Pi (192.168.5.20)

In my reverse proxy config, I have a whole bunch of Authelia stuff, but the main section in regards to Home Assistant is:


  location ~/homeassistant/?(.*)?$ {
    if ($request_uri ~ /homeassistant$ ) {
      add_header Content-Type text/html;
      return 200 '<meta http-equiv="refresh" content="0; URL=$uri/" />';
    }
    proxy_pass http://192.168.5.20:8123;
    rewrite /homeassistant/(.*) /$1 break;
    include conf.d/proxy.config;
    include conf.d/auth.config;
  }

In my Home Assistant config, I have the following:

http:
  server_port: 8123
  use_x_forwarded_for: true
  trusted_proxies:
    - 192.168.5.10
    - 192.168.5.11
    - 192.168.5.12

homeassistant:
  external_url: "https://mydomain.xyz/homeassistant/"
  internal_url: "http://homeassistant.:8123/"

I want to access homeassistant at mydomain.xyz/homeassistant from the internet, and also homeassistant.:8123 (or mydomain.xyz/homeassistant) from when within the LAN.

Right now when navigating to mydomain.xyz/homeassistant I can see that it is loading the Home Assistant index.html page, but it is not adding on the homeassistant directory in the requests. Ie, the first javascript file trying to load is:

https://mydomain.xyz/frontend_latest/core.8b15dd5b.js

and NOT:

https://mydomain.xyz/homeassistant/frontend_latest/core.8b15dd5b.js

I saw that base_url is deprecated, and I’m unsure why Home Assistant is not adding a subpath.

Look at location info in example at link. I forget why but much of that is needed for HA

Hey @tmjpugh I checked that thread, some users reported the same issue as me, but I couldn’t see a solution. A workaround was to just home assistant on its own subdomain, but that’s not an option for me. Should I post my question in that thread instead?

Can you try using a sub_filter directive in your location config?
That will parse the response from HA and replace every value of https://mydomain.xyz/ with https://mydomain.xyz/homeassistant/
set sub_filter_once off; such that the filter does not stop after the first instance.

sub_filter https://mydomain.xyz https://mydomain.xyz/homeassistant;
sub_filter_once off;

See also the nginx docs:
https://nginx.org/en/docs/http/ngx_http_sub_module.html

Hmmm, this seems to break everything. I think Home Assistant doesn’t support subdirectories and must be hosted in a top level directory on a domain.

Well, it was worth a shot :slight_smile:.
Not sure how home assistant handles the hosting.
Maybe one of the devs or moderators can chime in for a better understanding on why it would not work as a subdirectory.

Same issue here.

I would love to see this work. I have everything in subfolders except HA.

This is still an issue as of version 2023.11.3 and 2023.12.4.