如何将Traefik子域传递到后端URL



我想将一个域的所有子域路由到一个后端服务,并在后端URL中附加相应的子域。

例如,http://mysubdomain.example.com/api/hello-world变成http://backend-service/api/mysubdomain/hello-world。

所以下面的方案:http://backend-service/api/[subdomain]/[remaining-url]

docker.compose.yml中的当前配置(不工作,regex不匹配和替换):

- "traefik.http.routers.registry.rule=HostRegexp(`{subdomain:[a-zA-Z0-9]+}.exmaple.com`) && PathPrefix(`/api`)" 
- traefik.http.middlewares.registrypath.replacepathregex.regex=^http://(.*).example.com/api/(.*)$$
- traefik.http.middlewares.registrypath.replacepathregex.replacement=/api/$$1/$$2
- "traefik.http.routers.registry.middlewares=registrypath@docker"

似乎正则表达式不匹配。原因是什么?我该如何解决我的问题?

试试这个:

- "traefik.http.routers.registry.rule=HostRegexp(`{subdomain:[a-zA-Z0-9]+}.exmaple.com`) && PathPrefix(`/api`)" 
- "traefik.http.middlewares.registrypath.replacepathregex.regex=^http://(.*).example.com/api/(.*)"
- "traefik.http.middlewares.registrypath.replacepathregex.replacement=http://example.com/api/$${1}/$${2}"
- "traefik.http.routers.registry.middlewares=registrypath@docker"

最新更新