Reverse proxy from the path to the desired address

reverse proxy from the path to the desired address
I am going to reverse proxy domainone.com/blog/ to https://domaintwo.com. The goal is to transform incoming requests as follows:

domainone.com/blog/	=>	https://domaintwo.com
domainone.com/blog/seo/	=>	https://domaintwo.com/seo/
domainone.com/blog/article/product/	=>	https://domaintwo.com/article/product/	

But the settings I applied in config.yml are not working properly.

http:
  routers:
    novu_api_router:
      rule: "Host(`domainone.com`) && Path(`/blog/`)"
      service: "novu_api_service"
      entryPoints:
        - "https"
        - "http"
      tls:
        options: default
        certresolver: mycert

  services:
    novu_api_service:
      loadBalancer:
        servers:
          - url: "https://domaintwo.com"



tls:
  options:
    default:
      minVersion: VersionTLS12
      maxVersion: VersionTLS13
      cipherSuites:
        - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
        - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
        - TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 
        - TLS_AES_256_GCM_SHA384               
        - TLS_CHACHA20_POLY1305_SHA256
  certificates:
    - certFile: /traefik/certs/fullchain.pem
      keyFile: /traefik/certs/privkey.pem

Currently, the requests are incorrectly based on the config.yml above as follows:

domainone.com/blog/	=>	https://domaintwo.com/blog/
domainone.com/blog/seo/	=>	https://domaintwo.com/blog/seo/
domainone.com/blog/article/product/	=>	https://domaintwo.com/blog/article/product/	

Please guide me. I have been searching on Google and asking questions from chatgpt for almost 4 days, but my problem is not solved.

For starters, you should fix your setup, as you currently create routers using http+https entrypoint, and then enable TLS on both.

Check simple Traefik example.

You need to create a router:

rule: Host(`example.com`) && PathPrefix(`/blog`)

Create a middleware to remove the path before forwarding the request:

http:
  middlewares:
    myStripprefix:
      stripPrefix:
        prefixes:
          - "/blog" 

Don’t forget to assign the middleware to the router.

Be aware that most GUI web apps don’t like to be placed under a path prefix. They usually respond with fixed paths / for redirect, images and scripts. You usually need to be able to set a "base path" for this to work.

1 Like

Thank you for your reply. Your way worked, but do you think it is reasonable to set the path and then put stripPrefix again to remove the same path?
The second question is whether adding stripPrefix does not cause the redirection operation to take place? Although I did not test, I did not see a redirect.

The rule is matching the incoming request to Traefik, with host and path. By default the request is forwarded with the same path. So if you want to change the path in the target request, you need to use middlewares.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.