I am migrating from Nginx as a reverse proxy to Traefik v2, and have run into some issues with PathPrefix and rewrite rules.
I use docker-compose to run home-assistant and am adding zwavejs2mqtt as a separate container (to replace home-assistant's deprecated zwave integration), and use balenaCloud to manage my docker containers. Due to some odd issues with websocket interference between the two containers, Traefik looks like it will be a better solution than nginx for my purposes. But I'm a newbie to Traefik, and I'm having some trouble getting the path forwarding to work with the zwavejs2mqtt container.
I'm getting errors in the console such as "Manifest: Line: 1, column: 4, Unexpected data after root element." (Which I'm guessing means that I may not have set up the stripprefix middleware correctly).
My docker-compose:
version: '2.1'
volumes:
store:
hass-config:
services:
whoami:
image: containous/whoami:latest
labels:
traefik.enable: true
traefik.http.routers.whoami.entrypoints: http
traefik.http.routers.whoami.rule: "PathPrefix(`/hello`)"
zwave:
container_name: zwave
build: ./zwavejs2mqtt
restart: always
tty: true
stop_signal: SIGINT
environment:
- SESSION_SECRET=mysupersecretkey
devices:
- "/dev/ttyACM0:/dev/ttyACM0"
volumes:
- "store:/usr/src/app/store"
ports:
- '8091:8091' # port for web interface
- '3000:3000' # port for zwave-js websocket server
labels:
- "traefik.enable=true"
- "traefik.http.routers.zwave.service=zwave"
- "traefik.http.routers.zwave.entrypoints=http"
- "traefik.http.routers.zwave.rule=PathPrefix(`/zwave`)"
- "traefik.http.services.zwave.loadbalancer.server.port=8091"
- "traefik.http.middlewares.zwave-strip.stripprefix.prefixes=/zwave"
- "traefik.http.routers.zwave.middlewares=zwave-strip"
- "traefik.http.middlewares.zwave.headers.customrequestheaders.X-External-Path=/zwave"
homeassistant:
image: ghcr.io/home-assistant/raspberrypi3-homeassistant:2021.7.4
restart: unless-stopped
volumes:
- 'hass-config:/config'
ports:
- '8123:8123'
- '1883:1883'
labels:
- "traefik.enable=true"
- "traefik.http.routers.homeassistant.entrypoints=http"
- "traefik.http.routers.homeassistant.rule=PathPrefix(`/`)"
- "traefik.http.services.homeassistant.loadbalancer.server.port=8123"
- "traefik.http.routers.homeassistant.service=homeassistant"
traefik:
image: traefik:latest
network_mode: host
environment:
TRAEFIK_LOG_LEVEL: INFO
TRAEFIK_ENTRYPOINTS_HTTP_ADDRESS: :80
TRAEFIK_PROVIDERS_DOCKER: true
TRAEFIK_PROVIDERS_DOCKER_EXPOSEDBYDEFAULT: false
TRAEFIK_PROVIDERS_DOCKER_ENDPOINT: unix:///var/run/balena.sock
ports:
- "80:80"
labels:
io.balena.features.balena-socket: true
traefik.http.routers.http.entrypoints: http
My old nginx configuration (which did handle the path routing as expected, but ran into websocket problems):
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream homeassistant {
server sh_homeassistant:8123;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name *.balena-devices.com;
location / {
proxy_pass http://homeassistant;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /api/websocket {
proxy_pass http://homeassistant/api/websocket;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /zwavejs2mqtt {
rewrite ^ $request_uri;
rewrite '^/zwavejs2mqtt(/.*)$' $1 break;
proxy_pass https://zwavejs2mqtt:8091;
proxy_set_header X-External-Path /zwavejs2mqtt;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
My current supposition is that I've not correctly translated the rewrite rule in my nginx config (which I set up as the zwavejs2mqtt guide instructs - ZwaveJS2Mqtt) to the traefik labels, but I've not been able to find the solution, and the issue certainly could be something else.
I'd appreciate any help in resolving this issue. Thanks!