sanderdw / hassio-addons

DSMR Reader Datalogger and Metabase Home Assistant Add-ons
https://community.home-assistant.io/search?q=%40sanderdw%20%23home-assistant-os
Other
54 stars 14 forks source link

Reverse proxy support #24

Closed rickk88 closed 2 years ago

rickk88 commented 2 years ago

Would it be possible to support the environmental variables for reverse proxy support?

https://dsmr-reader.readthedocs.io/en/v4/reference/env-settings.html#django-use-x-forwarded-host https://dsmr-reader.readthedocs.io/en/v4/reference/env-settings.html#django-static-url

https://github.com/dsmrreader/dsmr-reader/issues/1080

sanderdw commented 2 years ago

Hi @rickk88, just wanted to let you now that i missed some Github notifications including this one. I tried this a while back but ran into different problems. I can look into it again if you want me too, what's your goal?

rickk88 commented 2 years ago

Hi Sander, I want to put the unencrypted port 7777 behind a reverse proxy that uses SSL, as non-HTTPS connections might be flagged in the future by the browsers and to not pass passwords over the network in plain text :)

sanderdw commented 2 years ago

But that already works right? So you have internet <-ssl-> reverseproxy <-non-ssl-> dsmrreader-ip:7777? Or do you refer to Ingress support (https://developers.home-assistant.io/docs/add-ons/presentation#ingress)

rickk88 commented 2 years ago

No, I want to publish https://proxy.domain.ext/dsmr -> dsmrreader-ip:7777 But because of the /dsmr I get error "Not Found. The requested resource was not found on this server"

sanderdw commented 2 years ago

Don't know how to properly configure this, it should be the right combination of: https://github.com/sanderdw/hassio-addons/blob/ingress_test/dsmr_reader/nginx/dsmr-webinterface https://github.com/sanderdw/hassio-addons/blob/ingress_test/dsmr_reader/rootfs/etc/services.d/nginx/run#L39-L43

I think we need to find someone who can help.

rickk88 commented 2 years ago

Not very knowledgeable in all of this, but I think you're almost there :)

In https://github.com/sanderdw/hassio-addons/blob/master/dsmr_reader/run.sh

Would it be possible to show the options for these settings in the package: DJANGO_STATIC_URL DJANGO_FORCE_SCRIPT_NAME DJANGO_USE_X_FORWARDED_HOST

Each of these should only be set as an environmental variable when the value isn't empty, or otherwise have a default: DJANGO_STATIC_URL="None" DJANGO_FORCE_SCRIPT_NAME="None" DJANGO_USE_X_FORWARDED_HOST="False"

For my environment it would be: DJANGO_STATIC_URL="/dsmr/" DJANGO_FORCE_SCRIPT_NAME="/dsmrreader" DJANGO_USE_X_FORWARDED_HOST="True"

At least that's what I think..

sanderdw commented 2 years ago

Hi @rickk88 , can you try the new Ingress support? See https://github.com/sanderdw/hassio-addons/issues/42#issuecomment-1121577693

rickk88 commented 2 years ago

Hi Sander, Ingress works like a charm! Will you still work on allowing manual configuration of some environmental variables for those not using Ingress? (That way we can choose to use Ingress or something like Nginx Proxy Manager as a reverse proxy)

sanderdw commented 2 years ago

Should not be that hard, in order for that to work you only want to be able to configure DJANGO_STATIC_URL and DJANGO_FORCE_SCRIPT_NAME right?

rickk88 commented 2 years ago

I think so yeah.

sanderdw commented 2 years ago

I have a test version ready in the develop branch, can you test it?

  1. Uninstall current addon
  2. Go to https://github.com/sanderdw/hassio-addons/tree/develop - find Code button -> Download ZIP
  3. From that zip file, only copy the 'dsmr_reader' folder and place it in your \hassioip\addons\ directory (you can use the Samba share addon).
  4. In Home Assistant -> Add-on store -> In the top right press 'Check for updates' and DSMR Reader should appear right on the top under 'Local add-ons'.
  5. Install and configure the addon, choose the 'Custom' option under webserver and make sure you fill the DJANGO_FORCE_SCRIPT_NAME and DJANGO_STATIC_URL options.
rickk88 commented 2 years ago

Hi Sander, I haven't figured out a working configuration with my proxy manager, maybe it's missing "DJANGO_USE_X_FORWARDED_HOST" ?

sanderdw commented 2 years ago

What proxy manager are you using and how do you want to set it up?

DJANGO_USE_X_FORWARDED_HOST is always set to True

rickk88 commented 2 years ago

Trying to use Nginx Proxy Manager (HA Addon) forwarding https://ha.domain.ext/dsmr to http://ha-ip:7777

sanderdw commented 2 years ago

It should work, i created a test setup myself:

image image

So i think the only thing you need to do is configure the proxy.domain.ext entry that you already have in nginx proxy manager. In the Advanced tab put something like:

location /dsmr{
    rewrite /dsmr(.*) $1 break;
    proxy_pass http://192.168.100.60:7777/;
}

After this configure the addon with this script name (so the entire url):

https://proxy.domain.ext/dsmr/
rickk88 commented 2 years ago

Hi Sander, that helped me in the right direction. I configured: DJANGO_FORCE_SCRIPT_NAME as /dsmr DJANGO_STATIC_URL as /dsmr/static/

And the proxy like you provided. Had to restart my proxy manager before it started working, which I might have not done enough or was impatient. You're the man!

sanderdw commented 2 years ago

Released: https://community.home-assistant.io/t/dsmr-reader-add-on-for-home-assistant/279087/268?u=sanderdw