70p4z / ha-reverse-proxy-path

Home assistant reverse proxy script to allow for sub path chrooting (http://acme.com/ha/). Which was not possible with bare home assistant
11 stars 3 forks source link

Apache config? #3

Open spicysomtam opened 1 year ago

spicysomtam commented 1 year ago

My webserver is running apache and I have several paths working quite nicely (Jenkins, roundcube, gitea, gitweb). However with your script, I get this error:

----------------------------------------
Exception occurred during processing of request from ('192.168.2.2', 34674)
Traceback (most recent call last):
  File "/usr/lib/python3.10/socketserver.py", line 683, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python3.10/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.10/socketserver.py", line 747, in __init__
    self.handle()
  File "/usr/lib/python3.10/http/server.py", line 432, in handle
    self.handle_one_request()
  File "/usr/lib/python3.10/http/server.py", line 420, in handle_one_request
    method()
  File "/home/amunro/home-assistant/./hacs-subpath-reverseproxy.py", line 188, in do_GET
    resp.raise_for_status()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://localhost:8123/api/websocket

My apache is using simply the following; maybe I need a ProxyPassReverse line as well?:

ProxyPass /ha http://192.168.2.253:8124

My apache and ha are running on different hosts; with ha running as a docker container. I can access locally using the ip address.

I get the login page, am able to login, and then just get a big home-assist icon in the middle of the web page and a url of https://my-domain/ha/?auth_callback=1&code=681... .

Thanks in advance.

spicysomtam commented 1 year ago

I managed to work around the issue by creating an addition dns name (cname) and virtual host in Apache. Hopefully this will help someone:

<VirtualHost _default_:443>
  ServerName ha.mydomain.com
  ErrorLog ${APACHE_LOG_DIR}/ha-error.log
  CustomLog ${APACHE_LOG_DIR}/ha-access.log combined
  ProxyPreserveHost On
  ProxyRequests off

  # Use a variable.
  Define hass localhost:8123
  ProxyPass /api/websocket ws://${hass}/api/websocket
  ProxyPassReverse /api/websocket ws://${hass}/api/websocket
  ProxyPass / http://${hass}/
  ProxyPassReverse / http://${hass}/

  RewriteEngine on
  RewriteCond %{HTTP:Upgrade} =websocket [NC]
  RewriteRule /(.*)  ws://${hass}/$1 [P,L]  
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule /(.*)  http://${hass}/$1 [P,L]

  SSLCertificateFile    /etc/letsencrypt/live/mydomain.com/fullchain.pem
  SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.com/privkey.pem
  Include /etc/letsencrypt/options-ssl-apache.conf 
</VirtualHost>