Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traefik: Dynamic routing by subdomain

Tags:

routes

traefik

Problem,

I have multiple services that are deployed on Docker containers. Each set of our clients should be routed to a specific service. Example:

  • Client1 -> service1
  • Client2 -> service1
  • client3 -> service2

This should done using subdomain: client1= client1.example.com client2 = client2.example.com

Question:

I would like this config to be dynamic meaning that Traefik needs to get these routing rules dynamically from another service:

  • I can for example create a service that giving a hostname return the name of service that Traefik should route to.
  • Or maybe when a client is added I can push this rule to Traefik.

Details:

This feature is useful when I create a saas application.

Thank you very much.

like image 201
Mohamed Amine Ouali Avatar asked Oct 23 '25 02:10

Mohamed Amine Ouali


1 Answers

One easy way to achieve this is to use regex for the defined routers for each of the services and update the regex once you onboarded a new client

For instance, add this label to the service 1

 - traefik.http.routers.app-backend.rule=HostRegexp(`{subdomain:(clinet1)}.example.com`)

and when you add another client that should be served by service 1, just update the label to be

 - traefik.http.routers.app-backend.rule=HostRegexp(`{subdomain:(clinet1|clientX)}.example.com`)

like image 122
Al-waleed Shihadeh Avatar answered Oct 26 '25 04:10

Al-waleed Shihadeh