Target Service Port Depending on Path Prefix

Hello everyone, I got the following scenario:

My service is exposing two ports, one for regular http requests and the other one for long polling stuff, also http. The frontend sends all requests dedicated to the long polling port via a path prefixed with /lp/. under the same domain. So basically:

  • https://example.com/foobar -> websecure -> ??? -> myservice:8080
  • https://example.com/lp/foobar -> websecure -> ??? -> myservice:8081

My intuition was to solve this with router rules (Host + PathPrefix) but I don't know how to target the different ports of the service. Maybe by somehow attaching two load balancers to the same service as the port can be specified there?

Thanks in advance!

Well, I stumbled over the solution by chance while working on another project :smiley:

Here's what I've done (roughly):

myservice:
  # ...
  labels:
    # ...
    # Web interface
    - "traefik.http.routers.my-router.rule=Host(`example.com`)"
    - "traefik.http.routers.my-router.entrypoints=web"
    - "traefik.http.routers.my-router.service=my-service"
    - "traefik.http.services.my-service.loadbalancer.server.port=8080"
    # Long polling interface
    - "traefik.http.routers.my-router-lp.rule=Host(`example.com`) && PathPrefix(`/lp/`)"
    - "traefik.http.routers.my-router-lp.entrypoints=web"
    - "traefik.http.routers.my-router-lp.service=my-servic-lp"
    - "traefik.http.services.my-service-lp.loadbalancer.server.port=8081"

By default, docker creates one services named <parent-folder>-myservice and all routers refer to that, especially the routers defined.

The trick is to assign (and thereby declare) a service to each router explicitly.
Then, we can configure each service's load balancer separately.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.