Skip to content

Commit b05029c

Browse files
committed
homeassistant: add docs for https reverse proxy setup
1 parent d38a122 commit b05029c

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

docs/Containers/Home-Assistant.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,119 @@ $ cd ~/IOTstack
222222
$ docker-compose up -d
223223
```
224224

225+
## HTTPS with a valid certificate
226+
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.
238+
239+
1. First test your HA is working correctly: `http://raspberrypi.local:8123/` (assuming
240+
your RPi hostname is raspberrypi)
241+
2. Make sure you have duckdns working.
242+
3. On your internet router, forward public port 443 to the RPi port 443
243+
4. Add swag to ~/IOTstack/docker-compose.yml beneath the `services:`-line:
244+
```
245+
swag:
246+
image: ghcr.io/linuxserver/swag
247+
cap_add:
248+
- NET_ADMIN
249+
environment:
250+
- PUID=1000
251+
- PGID=1000
252+
- TZ=Etc/UTC
253+
- URL=<yourdomain>.duckdns.org
254+
- SUBDOMAINS=wildcard
255+
- VALIDATION=duckdns
256+
- DUCKDNSTOKEN=<token>
257+
- CERTPROVIDER=zerossl
258+
- EMAIL=<e-mail> # required when using zerossl
259+
volumes:
260+
- ./volumes/swag/config:/config
261+
ports:
262+
- 443:443
263+
restart: unless-stopped
264+
```
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"):
275+
```
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
279+
```
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:
298+
```
299+
sed -i -e 's/#auth_basic/auth_basic/' \
300+
volumes/swag/config/nginx/proxy-confs/homeassistant.subdomain.conf
301+
docker-compose exec swag htpasswd -c /config/nginx/.htpasswd anyusername
302+
```
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+
- 192.168.0.0/16
312+
- 172.16.0.0/12
313+
- 10.77.0.0/16
314+
```
315+
10. Refresh the stack: `cd ~/IOTstack && docker-compose stop && docker-compose
316+
up -d` (again may take 1-3 minutes for swag to start if it recreates
317+
certificates)
318+
11. Test homeassistant is still working correctly:
319+
`http://raspberrypi.local:8123/` (assuming your RPi hostname is
320+
raspberrypi)
321+
12. Test the reverse proxy https is working correctly:
322+
`https://raspberrypi.local/` (browser will issue a warning about wrong
323+
certificate domain, as the certificate is issued for you duckdns-domain, we
324+
are just testing)
325+
326+
Or from the command line in the RPi:
327+
```
328+
curl --resolve homeassistant.<yourdomain>.duckdns.org:443:127.0.0.1 \
329+
https://homeassistant.<yourdomain>.duckdns.org/
330+
```
331+
(output should end in `if (!window.latestJS) { }</script></body></html>`)
332+
333+
13. And finally test your router forwards correctly by accessing it from
334+
outside your LAN(e.g. using a mobile phone):
335+
`https://homeassistant.<yourdomain>.duckdns.org/` Now the certificate
336+
should work without any warnings.
337+
225338
## Deactivating Hass.io
226339
227340
Because Hass.io is independent of IOTstack, you can't deactivate it with any of the commands you normally use for IOTstack.

0 commit comments

Comments
 (0)