Traefik SignalR docker

Hello,

i want to set up SignalR websocket behind Traefik.
Everything is running in docker containers.
Can somebody point me in the right direction, or to a working example?
I tried everything what i could find on the internet.
The best i can get is a Bad Gateway 502 response.

Portainer Configuration

Share your Traefik static and dynamic config, and docker-compose.yml if used.

For code use 3 backticks in front and after, or select it and press the </> button.

Check simple Traefik example.

1 Like

thank you for message.
here is my configuration.
i also have apache2 container running, maybe that causes some problems?

traefik docker-compose.yml

services:
  traefik:
    image: traefik:2.10.4
    container_name: traefik    
    restart: always
    labels:
      - "traefik.http.routers.traefik.rule=Host(`twitchswitch.de`)"
      # - "traefik.http.routers.traefik.middlewares=websocket-headers"
      # - "traefik.http.middlewares.websocket-headers.headers.customRequestHeaders.X-Forwarded-Proto=https"      
      # - "traefik.http.middlewares.websocket-headers.headers.customRequestHeaders.Connection=keep-alive, Upgrade"
      # - "traefik.http.middlewares.websocket-headers.headers.customRequestHeaders.Upgrade=websocket"  
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
      - "6000:6000"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./letsencrypt:/letsencrypt
      - ./config/traefik.yml:/traefik.yml
    networks:
      - web

networks:
  web:
    name: traefik_web

config traefik.yml

log:
  level: DEBUG

global:
  checkNewVersion: true
  sendAnonymousUsage: true
  
api:
  dashboard: true
  insecure: true

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
  
  websecure:
    address: ":443"
    http:
      tls:
        certResolver: staging

  # websocket:
  #   address: ":6000"    

tls:
  certificates:
    - certFile: /etc/letsencrypt/live/twitchswitch.de/fullchain.pem
      keyFile: /etc/letsencrypt/live/twitchswitch.de/privkey.pem

certificatesResolvers:
  staging:
    acme:
      email: mymail@hotmail.com
      storage: /letsencrypt/acme.json
      caServer: "https://acme-staging-v02.api.letsencrypt.org/directory"
      httpChallenge:
        entrypoint: web

  production:
    acme:
      email: mymail@hotmail.com
      storage: /letsencrypt/acme.json
      caServer: "https://acme-v02.api.letsencrypt.org/directory"
      httpChallenge:
        entryPoint: web

providers:
  docker:
    exposedByDefault: false
    network: traefik_web

signalr docker-dompose.yml

services:
  singlarwebsocket:
    image: discostoff/singlarwebsocket:latest
    container_name: singlarwebsocket
    labels:
      - "log.level=debug"
      - "traefik.enable=true"
      - "traefik.http.routers.singlarwebsocket.rule=Host(`twitchswitch.de`) && PathPrefix (`/ws/communityhub`) || Host(`twitchswitch.de`) && PathPrefix (`/ws`)"
      - "traefik.http.routers.singlarwebsocket.middlewares=websocket-headers"
      - "traefik.http.routers.singlarwebsocket.entrypoints=websecure"
      - "traefik.http.services.singlarwebsocket.loadbalancer.sticky=true"
      - "traefik.http.services.singlarwebsocket.loadbalancer.server.url=http://localhost:6000"
      - "traefik.http.services.singlarwebsocket.loadbalancer.server.port=6000"
      - "traefik.http.middlewares.websocket-headers.headers.customRequestHeaders.X-Forwarded-Proto=https"      
      # - "traefik.http.middlewares.websocket-headers.headers.customRequestHeaders.Connection=keep-alive, Upgrade"
      # - "traefik.http.middlewares.websocket-headers.headers.customRequestHeaders.Upgrade=websocket"
    networks:
      - traefik_web

networks:
  traefik_web:
    external: true

Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
EXPOSE 6000
EXPOSE 6001

FROM mcr.microsoft.com/dotnet/sdk:7.0 as build
WORKDIR /src
COPY ["SinglarWebsocket/SinglarWebsocket.csproj", "SinglarWebsocket/"]
RUN dotnet restore "SinglarWebsocket/SinglarWebsocket.csproj"
WORKDIR "/src/SinglarWebsocket"
COPY . .
RUN dotnet build "SinglarWebsocket/SinglarWebsocket.csproj" -c Release -o /app/build

FROM build as publish
RUN dotnet publish "SinglarWebsocket/SinglarWebsocket.csproj" -c Release -o /app/publish

FROM base as final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "SinglarWebsocket.dll"]

Asp.net 7.0 app

using SinglarWebsocket;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddSignalR();

var app = builder.Build();

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.MapHub<FriendshipHub>("ws/communityhub");

app.Run();

loadbalancer.servers.url is not available on Docker provider with labels, you would need to use a dynamic config file and load via providers.file.

Also there is a spelling mistake in the line. Did you check the Traefik logs or Traefik dashboard, if the service is recognized despite the config error?

okay, i basically show you everything i tried. I still dont understand what i really need. From what i can hear. ppl say, you dont have to change much in the configuration.
I checked everything what i can. Traefik dashboard, the logs, but i get not much information from the logs. Only Bad Gateway 502.

When i remove

- "traefik.http.services.singlarwebsocket.loadbalancer.server.port= 6000"
- "traefik.http.middlewares.websocket-headers.headers.customRequestHeaders.X-Forwarded-Proto=https"      

I get 404 not found.
Do you think the url is important?

For sure remove this line:

For sure keep this line, so Traefik knows which port to use.

Check your Traefik log for error and check Traefik dashboard.

Your Dockerfile exposes 4 ports, I don't know if it works when just making one available.

I get this error once when i restart the app and send the handshake with postman.

this is the url iam using in postman

wss://twitchswitch.de/ws/communityhub

[+] Running 1/1
 ✔ Container singlarwebsocket  Removed                                                                                                                        0.3s
[+] Running 1/1
 ✔ Container singlarwebsocket  Started                                                                                                                        0.6s
singlarwebsocket  | info: Microsoft.Hosting.Lifetime[14]
singlarwebsocket  |       Now listening on: http://[::]:80
singlarwebsocket  | info: Microsoft.Hosting.Lifetime[0]
singlarwebsocket  |       Application started. Press Ctrl+C to shut down.
singlarwebsocket  | info: Microsoft.Hosting.Lifetime[0]
singlarwebsocket  |       Hosting environment: Production
singlarwebsocket  | info: Microsoft.Hosting.Lifetime[0]
singlarwebsocket  |       Content root path: /app
singlarwebsocket  | warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
singlarwebsocket  |       Failed to determine the https port for redirect.