简体   繁体   中英

How to pass Traefik subdomain to backend URL

I would like to route all subdomains of a domain to one backend service and append the respective subdomain in the backend URL.

For example, http://mysubdomain.example.com/api/hello-world becomes http://backend-service/api/mysubdomain/hello-world.

So the following scheme: http://backend-service/api/[subdomain]/[remaining-url]

Current configuration in docker.compose.yml (not working, regex does not match and replace):

- "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"

It seems like the regex doesn't match. What could be the reason or how could I solve my problem?

Try this:

- "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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM