Traefik无法将部分容器路由至正确后端的问题排查
Alright, let's figure out why HomeAssistant is throwing a 404 while Portainer works perfectly with Traefik. I've dealt with this exact issue a few times, so here are the key areas to check, ordered by likelihood:
1. Double-Check HomeAssistant's Traefik Labels
Since Portainer is routing correctly, its labels are set up properly—so the problem is almost certainly a mismatch in HomeAssistant's labels. Here's what to verify:
- Missing or incorrect
traefik.enable=true: If Traefik hasexposedByDefault=falsein its config (which is a best practice), you need to explicitly enable routing for each container. Portainer has this label, but HomeAssistant might not. - Wrong host rule: Make sure
traefik.http.routers.homeassistant.ruleuses the correct domain (e.g.,Host(homeassistant.yourdomain.com)). Typos here (like a missing dot or misspelled domain) will cause Traefik to ignore the request, leading to a 404. - Incorrect backend port: HomeAssistant runs on port 8123 by default—confirm
traefik.http.services.homeassistant.loadbalancer.server.port=8123is set. If you put the wrong port here, Traefik can't reach the service.
Here's an example of properly formatted labels for HomeAssistant:
labels: - "traefik.enable=true" - "traefik.http.routers.homeassistant.rule=Host(`homeassistant.yourdomain.com`)" - "traefik.http.routers.homeassistant.entrypoints=websecure" - "traefik.http.routers.homeassistant.tls.certresolver=letsencrypt" - "traefik.http.services.homeassistant.loadbalancer.server.port=8123" - "traefik.docker.network=traefik-proxy"
2. Verify Docker Network Alignment
Traefik can only route to containers on the same Docker network. Portainer works, so it's already connected to Traefik's network—check if HomeAssistant is too:
- Look at your
docker-compose.ymlfor both services: ensure HomeAssistant uses the same network as Traefik (e.g., a dedicatedtraefik-proxynetwork). - Run
docker inspect homeassistantanddocker inspect traefikto confirm they share at least one network. If not, add HomeAssistant to the correct network in your compose file.
3. Check Traefik's TOML Config for Conflicts
exposedByDefaultsetting: If your Traefik config has[providers.docker] exposedByDefault = false, any container withouttraefik.enable=truewill be ignored. This is likely why Portainer works (you added the label) but HomeAssistant doesn't.- Route conflicts: Make sure no other Traefik router has a rule that overlaps with HomeAssistant's domain (e.g., a wildcard rule like
Host(*.yourdomain.com)that's prioritized higher). Check the order of your routers—Traefik uses the most specific rule first, but if a wildcard catches the request before your HomeAssistant rule, it might route to the wrong service.
4. Dig Into the Logs
Compare the successful Portainer logs with the failed HomeAssistant logs to spot differences:
- In Traefik's logs, look for lines like
no route found for GET /when accessing HomeAssistant—this confirms Traefik doesn't recognize the request as matching any router rule. - Check HomeAssistant's container logs with
docker logs homeassistantto ensure the service is actually running and listening on port 8123. If HomeAssistant isn't started, even direct access would fail, which would explain the 404.
5. Test Direct Access to HomeAssistant
Bypass Traefik entirely to rule out container-level issues:
- Access
http://your-server-ip:8123directly. If this works, the problem is definitely in Traefik's routing configuration. If it doesn't, fix HomeAssistant's container setup first (e.g., ensure the port is mapped correctly, or the service is starting properly).
Once you check these areas, you should be able to pinpoint the exact cause of the 404. If you share your actual configs/logs, I can help narrow it down further!
内容的提问来源于stack exchange,提问作者uav2608

