# Traefik as Reverse Proxy to backend services

**URL:** <https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003>\
**Category:** Traefik v2\
**Tags:** kubernetes-crd, middleware, kubernetes-ingress\
**Created:** [January 4, 2023, 10:06am UTC](https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003 "2023-01-04T10:06:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike818148](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mike818148/32/6435_2.png) [@mike818148](https://community.traefik.io/u/mike818148)\
**Post date:** [January 4, 2023, 10:06am UTC](https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003/1 "2023-01-04T10:06:05Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![kevinpollet](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/kevinpollet/32/1755_2.png) [@kevinpollet](https://community.traefik.io/u/kevinpollet)\
**Post date:** [January 4, 2023, 10:38am UTC](https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003/2 "2023-01-04T10:38:15Z")

</div>

Hello @mike818148 and thanks for your interest in Traefik,

I think you can use the [replacepathregexp](https://doc.traefik.io/traefik/middlewares/http/replacepathregex/) to replace the `/sampleAppN` path by `/sampleApp` (this could also work by using the [stripprefix](https://doc.traefik.io/traefik/middlewares/http/stripprefix/) and [addprefix](https://doc.traefik.io/traefik/middlewares/http/addprefix/) middlewares).

Hope this helps!

---

<div class="post-metadata">

**Author:** ![mike818148](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mike818148/32/6435_2.png) [@mike818148](https://community.traefik.io/u/mike818148)\
**Post date:** [January 4, 2023, 10:45am UTC](https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003/3 "2023-01-04T10:45:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinpollet](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/kevinpollet/32/1755_2.png) [@kevinpollet](https://community.traefik.io/u/kevinpollet)\
**Post date:** [January 4, 2023, 10:57am UTC](https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003/4 "2023-01-04T10:57:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mike818148](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mike818148/32/6435_2.png) [@mike818148](https://community.traefik.io/u/mike818148)\
**Post date:** [January 4, 2023, 11:21am UTC](https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003/5 "2023-01-04T11:21:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinpollet](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/kevinpollet/32/1755_2.png) [@kevinpollet](https://community.traefik.io/u/kevinpollet)\
**Post date:** [January 4, 2023, 1:06pm UTC](https://community.traefik.io/t/traefik-as-reverse-proxy-to-backend-services/17003/6 "2023-01-04T13:06:32Z")

</div>

> 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](https://doc.traefik.io/traefik/routing/routers/#rule) and/or [Ingress | Kubernetes](https://kubernetes.io/docs/concepts/services-networking/ingress/#name-based-virtual-hosting)) instead of path routing. Thus, by using subdomains it is not required to configure subpath in applications (i.e. [kubernetes.dashboard.foo.com](http://kubernetes.dashboard.foo.com), [traefik.dashboard.foo.com](http://traefik.dashboard.foo.com)).
