添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Hi there,

I'm trying to create a redirection to my traefik-docker.
I have an API at https://domain.com/api and I would like to redirect all other to https://anotherdomain.com .
For example, if I hit https://domain.com , or https://domain.com/not-an-api , Traefik would redirect it.
How can I do that? I tried to search for these redirections, via middlewares, but I couldnt find the right answer.

Thanks!

Thanks for the reply cakiwi!

I tried Your way, but I'm not sure what I'm doing wrong.
This is the labels section for my container where the redirection needed:

labels:
      - "traefik.enable=true"
      - "traefik.http.routers.test.rule=Host(`domain.com`) && PathPrefix(`/api`)"
      - "traefik.http.routers.test.entrypoints=websecure"
      - "traefik.http.routers.test.tls.certresolver=myresolver"
      - "traefik.http.routers.test-redirect.priority=20"
      - "traefik.http.middlewares.redirectscheme.redirectscheme.scheme=https"
      - "traefik.http.routers.test-redirect.rule=Host(`domain.com`) && PathPrefix(`/`)"
      - "traefik.http.routers.test-redirect.priority=10"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://domain.com/(.*)"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://anotherdomain.com/"

It only throws back 404 error, and it wont redirect me.
Thanks for helping me out!

Your middleware is not attached to the router, so the request will be going to the container.

      - "traefik.http.routers.test-redirect.middlewares=test-redirectregex"

You also have two competing labels, but I do not think that will matter in this case:

traefik.http.routers.test-redirect.priority=10
traefik.http.routers.test-redirect.priority=20

First check traefik logs for errors and warnings.
Then check the traefik dashboard to make sure it all looks good.
My advice then would be to enable accesslog and use accesslog.format=json format option too. Try some more requests and make sure the requests are using the expected router.

Thanks for helping me cakiwi!
I have this configuration now, I fixed the competing labels so now this is the Traefik config for my container:

 labels:
      - "traefik.enable=true"
      - "traefik.http.routers.test.rule=Host(`domain.com`) && PathPrefix(`/api`)"
      - "traefik.http.routers.test.entrypoints=websecure"
      - "traefik.http.routers.test.tls.certresolver=myresolver"
      - "traefik.http.routers.test.priority=20"
      - "traefik.http.middlewares.redirectscheme.redirectscheme.scheme=https"
      - "traefik.http.routers.test-redirect.rule=Host(`domain.com`) && PathPrefix(`/`)"
      - "traefik.http.routers.test-redirect.priority=10"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^https://domain.com/(.*)"
      - "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=https://anotherdomain.com/"
      - "traefik.http.routers.test-redirect.middlewares=test-redirectregex"

Its still not working. As I can see in the debug log, nothing catches my request. I have no warnings or errors. In the Traefik dashboard, I can see that this router exists, and the middleware connected to this router, so I cant see why it is not working properly. :frowning_face:

This is the debug log:

time="2021-10-26T14:38:31Z" level=debug msg="Creating middleware" entryPointName=web middlewareType=RedirectRegex middlewareName=test-redirectregex@docker routerName=test-redirect@docker
time="2021-10-26T14:38:31Z" level=debug msg="Setting up redirection from ^https://domain.com/(.*) to https://anotherdomain.com/" routerName=test-redirect@docker entryPointName=web middlewareType=RedirectRegex middlewareName=test-redirectregex@docker
time="2021-10-26T14:38:31Z" level=debug msg="Adding tracing to middleware" entryPointName=web routerName=test-redirect@docker middlewareName=test-redirectregex@docker
              

And this is the access log when I try to reach https://domain.com/

{"ClientAddr":"-:6045","ClientHost":"-","ClientPort":"6045","ClientUsername":"-","DownstreamContentSize":19,"DownstreamStatus":404,"Duration":97410,"OriginContentSize":19,"OriginDuration":12602,"OriginStatus":404,"Overhead":84808,"RequestAddr":"domain.com","RequestContentSize":0,"RequestCount":1,"RequestHost":"domain.com","RequestMethod":"GET","RequestPath":"/","RequestPort":"-","RequestProtocol":"HTTP/2.0","RequestScheme":"https","RetryAttempts":0,"StartLocal":"2021-10-26T14:51:30.526965969Z","StartUTC":"2021-10-26T14:51:30.526965969Z","entryPointName":"websecure","level":"info","msg":"","time":"2021-10-26T14:51:30Z"}

And this is the dashboard:
image1429×861 55.3 KB