Traefik无法代理至内部Docker容器问题求助
问题:Traefik代理mydomain.com到内部容器失败,无法通过域名访问面板
我正尝试配置Traefik,使访问mydomain.com时能将请求代理至内部Docker容器,但目前无法实现。内部容器admin-webui通过Nginx托管HTML页面,暴露端口4200。服务已被正常发现,进入Traefik容器内执行curl 172.20.0.3能得到正确响应(网络正常),但无法通过mydomain.com/dashboard访问内部面板,仅能通过IP_ADDRESS:8080/dashboard访问。
当前Docker Compose配置
version: "3.5" services: ingress: # redacted volumes: - type: bind source: /var/run/docker.sock target: /var/run/docker.sock # redacted certificates ports: - "8080:8080" - "80:80" - "443:443" labels: - traefik.enable: true - traefik.http.routers.traefik.entrypoints: websecure - traefik.http.routers.traefik.rule: Host(`mydomain.com`) - traefik.http.routers.traefik.service: api@internal networks: - inbound admin_webui: #redacted ports: - "4200:80" labels: - traefik.enable=true - traefik.http.routers.admin_webui.entrypoints=web - traefik.http.routers.admin_webui.entrypoints=websecure - traefik.http.routers.admin_webui.rule=Host(`mydomain.com`) - traefik.http.routers.admin_webui.service=admin_webui - traefik.http.services.admin_webui.loadbalancer.server.port=4200 - traefik.docker.network=inbound networks: - inbound networks: inbound: driver: bridge
问题原因
- 路由规则冲突:Traefik自身路由与
admin_webui路由都绑定了Host(mydomain.com),导致请求被错误转发到Traefik的API服务(而非目标容器)。 - 容器端口配置错误:
admin_webui内部Nginx监听80端口,你配置的loadbalancer.server.port=4200是宿主机映射端口,容器间通信应使用容器内部端口。 - Dashboard路径混淆:Traefik默认API服务对应
/dashboard路径,当前配置将其绑定到主域名,与你期望的admin_webui面板路径冲突。
解决办法
1. 修正路由规则,避免冲突
给Traefik Dashboard和admin_webui配置不同的路由标识,比如:
- Traefik Dashboard使用子域名:
Host(traefik.mydomain.com) admin_webui绑定主域名:Host(mydomain.com)
2. 修正容器端口配置
将admin_webui的负载均衡端口改为容器内部的80端口:
- traefik.http.services.admin_webui.loadbalancer.server.port=80
3. 修正后的完整配置示例
version: "3.5" services: ingress: # redacted volumes: - type: bind source: /var/run/docker.sock target: /var/run/docker.sock # redacted certificates ports: - "8080:8080" - "80:80" - "443:443" labels: - traefik.enable=true - traefik.http.routers.traefik.entrypoints=websecure # 改用子域名访问Traefik Dashboard - traefik.http.routers.traefik.rule=Host(`traefik.mydomain.com`) - traefik.http.routers.traefik.service=api@internal networks: - inbound admin_webui: #redacted ports: - "4200:80" labels: - traefik.enable=true # 合并entrypoints配置 - traefik.http.routers.admin_webui.entrypoints=web,websecure - traefik.http.routers.admin_webui.rule=Host(`mydomain.com`) - traefik.http.routers.admin_webui.service=admin_webui # 修正为容器内部端口 - traefik.http.services.admin_webui.loadbalancer.server.port=80 - traefik.docker.network=inbound networks: - inbound networks: inbound: driver: bridge
4. 验证步骤
- 重启服务:
docker-compose down && docker-compose up -d - 通过
traefik.mydomain.com访问Traefik Dashboard,确认admin_webui路由和服务已正确注册 - 访问
mydomain.com测试是否能正常代理到admin_webui的页面
内容的提问来源于stack exchange,提问作者user3353167
相关产品推荐
相关产品推荐

