Two services on same domain with diferent path

I am trying to have two web services on the same domain but I am not succeeding. The idea is:

  • service_1 is running in container_1 on port 11111
  • Service_2 is running in the container_2 on port 2222
  • I have the domain abc .com

I want that:

  • When accessing abc .com traefik should redirect me to service_1
  • When I access abc .com/service2 traefik should redirect me to service_2

For this, this is my current configuration:

entryPoints:
  web:
    address: ":80"
  websecure:
    address: ":443"
http:
  routers:
    service_1_http_to_https:
      rule: "Host(`abc .com`)"
      entryPoints:
        - web
      middlewares:
        - redirect-to-https
      service: noop@internal

    service_1:
      rule: "Host(`abc .com`) && PathPrefix(`/`)"
      entryPoints:
        - websecure
      tls:
        certResolver: le
      service: service_1

    service_2:
      rule: "Host(`abc .com`) && PathPrefix(`/service2`)"
      entryPoints:
        - websecure
      tls:
        certResolver: le
      middlewares:
        - strip-service2
      service: service_2

  middlewares:
    redirect-to-https:
      redirectScheme:
        scheme: https

    strip-service2:
      stripPrefix:
        prefixes:
          - "/service2"

  services:
    service_1:
      loadBalancer:
        servers:
          - url: "htt p://se rvice_1:11111"

    service_2:
      loadBalancer:
        servers:
          - url: "htt p://se rvice_2:22222"

The current behaviour:

  • When I try to access abc .com it works fine loading service_1.
  • When I try to access abc .com/service2 it loads only the index of service_2.

The problem:

  • It turns out that when I access abc .com/se rvice2 and it requests a module from service_2, it is requesting it from service one, since for example, to import myjavamodule.js it requests it to abc .com/myjavamodule.js which looks for it in service_1 totally different when it should do it to abc .com/se rvice2/myjavamodule.js which should call htt p://se rvice_2:22222/myjavamodule.js

How can I in this case keep the two services totally separate using the same domain?

Your approach with Host() && PathPrefix() is correct. But most GUI web apps do not support a different path than /. StripPrefix middleware might solve the initial page load, but it can not handle links within the page with fixed path dependencies. Either the app supports setting another "base path" or you need to use a sub-domain, which is best practice.

Check simple Traefik example to place http-to-https redirect and TLS globally on entrypoint, that reduces dynamic config repetition. (You need to "translate" from command: to traefik.yml)

I solved the problem by adding ‘/’ to the end of the path,
i.e.: abc .com/se rvice2/

The question is why can't traefik handle that / without having to add it to the route by hand?

Where did you add a /? In PathPrefix()? That should not make a difference.

Writing in client web browser https://abc.com/service2/ instead of https://abc.com/service2