Traefik as Reverse Proxy to backend services

Hello experts,

I am relative new to Traefik Proxy here. We are setting up a k3s test environment and wants to utilize Traefik Proxy for Ingress implementation. Our use case are below:

  1. We have multiple tomcat services (which deployed the same web application package, e.g. sampleApp); so normally we can access the service by (http://[host]:[port]/sampleApp), I have retried to define the ingress to forward different path to different tomcat services, e.g.
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
  name: sampleApps
  namespace: default
  annotations:
    traefik.ingress.kubernetes.io/router.entrypoints: web

spec:
  rules:
    - http:
        paths:
          - path: /sampleApp1
            pathType: Prefix
            backend:
              service:
                name: sampleApp1
                port:
                  number: 8080
          - path: /sampleApp2
            pathType: Prefix
            backend:
              service:
                name: sampleApp2
                port:
                  number: 8080

Problem is that when I access URL http://..../sampleApp1, it returns 404 page not found. This is because Traefik proxy simpley forward the same URL request to the backend, and as tomcat is expecting a sub-path like sampleApp only. Is there any solution (e.g. middleware) can be used, that for end-user, we are still requesting to /sampleApp1, but while traefik proxy communicate with relative backend services it's sending /sampleApp instead?

Thanks and Regards,
Mike

Hello @mike818148 and thanks for your interest in Traefik,

I think you can use the replacepathregexp to replace the /sampleAppN path by /sampleApp (this could also work by using the stripprefix and addprefix middlewares).

Hope this helps!

Hello @kevinpollet

Thanks a lot for your reply. I did tried replace path and stripprefix middleward. However, this also replace the URL on user's browser. e.g.

  1. Request: /sampleApp1
  2. Replace the /sampleApp1 to /sampleApp
  3. Then I observe that browser starts to request the sub-sequent pages also with /sampleApp, e.g. /sampleApp/home.jsf which will leads to 404 page not found again.

So I am wondering the replace path (or stripprefix) will also reflect the changes on the sub-sequent requests on user's browser?

Thanks and Regards,
Mike

So I am wondering the replace path (or stripprefix) will also reflect the changes on the sub-sequent requests on user's browser?

That is because the proxied application does not know that it is exposed under a subpath. This means that the page rendered by the application should contain links with URLs containing /sampleApp1 not /sampleApp. This is something usually configurable. Maybe the app can also use the header sent by Traefik to obtain the exposed subpath and render the links correctly.

Hello Kevin,

Thanks for the hint. This is achievable for self-developed applications. But while we want to integrate with other applications this is not so feasible. For example, mainly dashboard container service (e.g. kubernetes dashboard, traefik dashboard, docker-registry dashboard...) they mostly just use / as the Base URL path. Which comes to similiar situation, that if I configured a path in the Ingress, and it will be recognized as a sub-path in the proxied applications. And we hit 404 again.

I believe there should be a better solution to play Traefik as Reverse Proxy instead of simply forwarding? As changing the behaviour in every application is not a perfect solution when we scale up.

Thanks and Regards,
Mike

I believe there should be a better solution to play Traefik as Reverse Proxy instead of simply forwarding? As changing the behaviour in every application is not a perfect solution when we scale up.

That is why it is better to do Host routing (see Traefik Routers Documentation - Traefik and/or Ingress | Kubernetes) instead of path routing. Thus, by using subdomains it is not required to configure subpath in applications (i.e. kubernetes.dashboard.foo.com, traefik.dashboard.foo.com).

1 Like