'Traefik V2 docker provider path /ws routing to external websocket server

Stack:

  • serverX
    • docker
    • traefik
    • x-site.com
      • redirect :80->:443
      • https://x-site.com/* -> x-site docker container
      • wss://x-site.com/ws proxy -> ws://external.websocket.com:8083/ws HOW TO?
    • y-site.com
    • ...
  • external.websocket.com - works perfectly with all kinds of mqtt,ws clients, except web
    • software: emqx
    • listens: ws:8083 mqtt:1883
    • protocol: ws, mqtt
    • proxy protocol support: on

docker-compose.yml

version: '3.7'
services:
  router:
    container_name: router
    image: traefik:v2.6
    restart: on-failure:5
    ports:
      - target: 80
        published: 80
        mode: host
      - target: 443
        published: 443
        mode: host
      - target: 8080
        published: 8080
        mode: host
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./router:/etc/traefik
  x-site:
    container_name: x-site
    image: backend
    restart: on-failure:5
    env_file:
      - /.default.env
      - /.server.env
      - /root/env/x-site.env
    labels:
      traefik.enable: true
      traefik.http.routers.x-site.rule: Host(`x-site.com`)
      traefik.http.routers.x-site.entrypoints: web,websecure
      traefik.http.routers.x-site.tls: true
      # tried service loadbalancer and etc
      # traefik.http.routers.ws_x-site.rule: Host(`x-site.com`) && PathPrefix(`/ws`)
      # traefik.http.routers.ws_x-site.entrypoints: websecure
      # traefik.http.routers.ws_x-site.service: websocket_x-site
      # traefik.http.services.websocket_x-site.loadBalancer.server.url: ws://external.websocket.com:8083/ws

traefik.yaml

api:
  insecure: true
  dashboard: true

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
          permanent: true
    transport:
      respondingTimeouts:
        readTimeout: 10
        writeTimeout: 10
        idleTimeout: 10
      lifeCycle:
        requestAcceptGraceTimeout: 5
        graceTimeOut: 5
  websecure:
    address: ":443"
    transport:
      respondingTimeouts:
        readTimeout: 300
        writeTimeout: 3600
        idleTimeout: 180
      lifeCycle:
        requestAcceptGraceTimeout: 30
        graceTimeOut: 30

providers:
  docker:
    endpoint: unix:///var/run/docker.sock
    exposedByDefault: false
  file:
    filename: /etc/traefik/cert.yaml

log:
  level: ERROR


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source