Skip to content

Commit 9f53ea6

Browse files
committed
docs: fixup HA SSL reverse proxy instructions
1 parent 1db6d27 commit 9f53ea6

File tree

1 file changed

+76
-21
lines changed

1 file changed

+76
-21
lines changed

docs/Containers/Home-Assistant.md

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,19 @@ $ cd ~/IOTstack
222222
$ docker-compose up -d
223223
```
224224

225-
## Adding https access to your Home Assistant
225+
## HTTPS with a valid certificate
226226

227-
Some HA integration (e.g google assistant) require your HA to be accessible
228-
through https. This tells you how to use a [linuxserver swag container](https://docs.linuxserver.io/general/swag) ([Docker hub docs](https://hub.docker.com/r/linuxserver/swag)) to automatically generate a SSL-certificate and setup a reverse proxy.
227+
Some HA integrations (e.g google assistant) require your HA API to be
228+
accessible via https with a valid certificate. You can configure HA to do this:
229+
[docs](https://www.home-assistant.io/docs/configuration/remote/) /
230+
[guide](https://www.home-assistant.io/docs/ecosystem/certificates/lets_encrypt/)
231+
or use a reverse proxy container, as described below.
232+
233+
The linuxserver Secure Web Access Gateway container
234+
([swag](https://docs.linuxserver.io/general/swag)) ([Docker hub
235+
docs](https://hub.docker.com/r/linuxserver/swag)) will automatically generate a
236+
SSL-certificate, update the SSL certificate before it expires and act as a
237+
reverse proxy.
229238

230239
1. First test your HA is working correctly: `http://raspberrypi.local:8123/` (assuming
231240
your RPi hostname is raspberrypi)
@@ -253,31 +262,77 @@ your RPi hostname is raspberrypi)
253262
- 443:443
254263
restart: unless-stopped
255264
```
256-
5. Start the swag container (creates the file to be edited in the next step): `cd ~/IOTstack && docker-compose up -d` and check it started OK `docker-compose logs -f swag`
257-
6. Rename the file volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf.sample to remove .sample from the filename.
258-
7. Enable reverse proxy to `raspberrypi.local` and fix homeassistant container name:
265+
Replace the bracketed values. Do NOT use any "-characters to enclose the values.
266+
267+
5. Start the swag container, this creates the file to be edited in the next step:
268+
```
269+
cd ~/IOTstack && docker-compose up -d
270+
```
271+
272+
Check it starts up OK: `docker-compose logs -f swag`. It will take a minute or two before it finally logs "Server ready".
273+
274+
6. Enable reverse proxy for `raspberrypi.local`. `homassistant.*` is already by default. and fix homeassistant container name ("upstream_app"):
259275
```
260-
sed -i -e 's/server_name/server_name *.local/' \
261-
-e 's/upstream_app homeassistant/upstream_app home_assistant/' \
262-
volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf
276+
sed -e 's/server_name/server_name *.local/' \
277+
volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf.sample \
278+
> volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf
263279
```
264-
8. Add password protection:
280+
281+
7. Forward to correct IP when target is a container running in "network_mode:
282+
host" (like Home Assistant does):
283+
```
284+
cat << 'EOF' | sudo tee volumes/swag/config/custom-cont-init.d/add-host.docker.internal.sh
285+
#!/bin/sh
286+
DOCKER_GW=$(ip route | awk 'NR==1 {print $3}')
287+
288+
sed -i -e "s/upstream_app .*/upstream_app ${DOCKER_GW};/" \
289+
/config/nginx/proxy-confs/homeassistant.subdomain.conf
290+
EOF
291+
sudo chmod u+x volumes/swag/config/custom-cont-init.d/add-host.docker.internal.sh
292+
```
293+
(This needs to be copy-pasted/entered as-is, ignore any "> "-prefixes printed
294+
by bash)
295+
296+
8. (optional) Add reverse proxy password protection if you don't want to rely
297+
on the HA login for security, doesn't affect API-access:
265298
```
266-
sed -i 's/#auth_basic/auth_basic/' volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf
299+
sed -i -e 's/#auth_basic/auth_basic/' \
300+
volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf
267301
docker-compose exec -it swag htpasswd -c /config/nginx/.htpasswd anyusername
268302
```
269-
8. Add `use_x_forwarded_for` and `trusted_proxies` to your homeassistant [http config](https://www.home-assistant.io/integrations/http). For a default install the result will be:
303+
9. Add `use_x_forwarded_for` and `trusted_proxies` to your homeassistant [http
304+
config](https://www.home-assistant.io/integrations/http). The configuration
305+
file is at `volumes/home_assistant/configuration.yaml` For a default install
306+
the resulting http-section should be:
307+
```
308+
http:
309+
use_x_forwarded_for: true
310+
trusted_proxies:
311+
- 172.16.0.0/12
312+
- 10.77.0.0/16
313+
```
314+
10. Refresh the stack: `cd ~/IOTstack && docker-compose stop && docker-compose
315+
up -d` (again may take 1-3 minutes for swag to start if it recreates
316+
certificates)
317+
11. Test homeassistant is still working correctly:
318+
`http://raspberrypi.local:8123/` (assuming your RPi hostname is
319+
raspberrypi)
320+
12. Test the reverse proxy https is working correctly:
321+
`https://raspberrypi.local/` (browser will issue a warning about wrong
322+
certificate domain, as the certificate is issued for you duckdns-domain, we
323+
are just testing)
324+
325+
Or from the command line in the RPi:
270326
```
271-
http:
272-
use_x_forwarded_for: true
273-
trusted_proxies:
274-
- 172.16.0.0/12
275-
- 10.77.0.0/16
327+
curl --resolve homeassistant.<yourdomain>.duckdns.org:443:127.0.0.1 \
328+
https://homeassistant.<yourdomain>.duckdns.org/
276329
```
277-
9. Refresh the stack: `cd ~/IOTstack && docker-compose stop && docker-compose up -d`
278-
10. Test homeassistant is still working correctly: `http://raspberrypi.local:8123/` (assuming your RPi hostname is raspberrypi)
279-
11. Test the reverse proxy is working correctly: `https://raspberrypi.local/` (note: https)
280-
12. And finally test your router forwards correctly by accessing it from outside your LAN(e.g. using a mobile phone): `https://homeassistant.<yourdomain>.duckdns.org/`
330+
(output should end in `if (!window.latestJS) { }</script></body></html>`)
331+
332+
13. And finally test your router forwards correctly by accessing it from
333+
outside your LAN(e.g. using a mobile phone):
334+
`https://homeassistant.<yourdomain>.duckdns.org/` Now the certificate
335+
should work without any warnings.
281336
282337
## Deactivating Hass.io
283338

0 commit comments

Comments
 (0)