# PathPrefix and strip

**URL:** <https://community.traefik.io/t/pathprefix-and-strip/2122>\
**Category:** Traefik v2\
**Tags:** docker\
**Created:** [October 3, 2019, 11:44am UTC](https://community.traefik.io/t/pathprefix-and-strip/2122 "2019-10-03T11:44:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![przemas75](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@przemas75](https://community.traefik.io/u/przemas75)\
**Post date:** [October 3, 2019, 11:44am UTC](https://community.traefik.io/t/pathprefix-and-strip/2122/1 "2019-10-03T11:44:25Z")

</div>

Hi,  
I'd like to route requests based on Host and Path (PathPrefix) for portainer service.  
Running traefik v2 of course.  
So i have docker-compose.yml for portainer with such entries:

```auto
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.portainer.rule=Host(`admin.domain.name`) && PathPrefix(`/portainer`)"
      - "traefik.http.middlewares.portainerpathstrip.stripprefix.prefixes=/portainer"
      - "traefik.http.routers.portainer.middlewares=portainerpathstrip@docker"
      - "traefik.http.routers.portainer.entrypoints=http"
      - "traefik.http.routers.portainer.service=portainer"
      - "traefik.http.services.portainer.loadbalancer.server.port=9000"

```

But unfortunately it does not work. As first request is fine, but next ones come with 404 error.  
The idea is to have one admin.domain.name host with a few /service1, /service2 /servicen routed to the appropriate containers.  
How to achive this?

> **[Traefik StripPrefix Documentation - Traefik](https://doc.traefik.io/traefik/v2.0/middlewares/stripprefix/)**
>
> In Traefik Proxy's HTTP middleware, StripPrefix removes prefixes from paths before forwarding requests. Read the technical documentation.

> Use a `StripPrefix` middleware if your backend listens on the root path ( `/` ) but should be routeable on a specific prefix.

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [October 3, 2019, 6:49pm UTC](https://community.traefik.io/t/pathprefix-and-strip/2122/2 "2019-10-03T18:49:07Z")

</div>

I posted some thoughts on a similar topic here:

> **[Traefik 2.0 Swarm not working with Services on Ports](https://community.traefik.io/t/traefik-2-0-swarm-not-working-with-services-on-ports/1877/14)**
>
> I don't know if it would work for traefik dashboard or not, but many applications are not written to support to be accessed on a different path than intended. In many cases, if apps expects to be found on /checkout and you serve it on /shop it...

In your case imaging that you are accessing portainer at [http://host/portainer/mainpage](http://host/portainer/mainpage) for the sake of an example. Portainer does not expect to be hosted at /portainer. So all references to other pages and/or assets might be on the page in form of `/asset/bla` that is absolute. In this case your browser will request from trafik `http://host/asset/bla` with portainer nowhere to be seen. Of course you will get 404 in that case.

This is not a traefik limitation. It's just that many web application are not written to be used this way. Not much you, or traefik can do about that.

> [@przemas75](#):
>
> The idea is to have one admin.domain.name host with a few /service1, /service2 /servicen routed to the appropriate containers.

For reasons outlined above I would personally not go for this approach, this is just inviting misery. One way when this is workable if all /service1 /service2 /servicen is written by you and/or your company, so you can make sure that they play nice and support being hosted on arbitrary URLs. Another way this is workable if application author is willing to support this which might be indicated in their documentation with examples of setting up reverse proxy for their app in this way.

---

<div class="post-metadata">

**Author:** ![przemas75](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@przemas75](https://community.traefik.io/u/przemas75)\
**Post date:** [October 3, 2019, 8:51pm UTC](https://community.traefik.io/t/pathprefix-and-strip/2122/3 "2019-10-03T20:51:24Z")

</div>

got it working with such config:

```auto
labels:
      - "traefik.enable=true"
      - "traefik.http.routers.portainer.rule=Host(`admin.domain.name`) && PathPrefix(`/portainer/`)"
      - "traefik.http.middlewares.portainerpathstrip.stripprefix.prefixes=/portainer"
      - "traefik.http.routers.portainer.middlewares=portainerpathstrip@docker"
      - "traefik.http.routers.portainer.entrypoints=http"
      - "traefik.http.routers.portainer.service=portainer"
      - "traefik.http.services.portainer.loadbalancer.server.port=9000"

```

Note the ending '/' in PathPrefix. Also, i need to connect (browser) to admin.domain.name/portainer/  
This way, it works.

EDIT:

> <https://github.com/containous/traefik/issues/5159>

---

<div class="post-metadata">

**Author:** ![albertinisg](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/albertinisg/32/2097_2.png) [@albertinisg](https://community.traefik.io/u/albertinisg)\
**Post date:** [May 21, 2020, 5:02pm UTC](https://community.traefik.io/t/pathprefix-and-strip/2122/4 "2020-05-21T17:02:03Z")

</div>

Thanks for the config! However, this is working with portainer, but if I configure some of my other services the same way, it doesn't work and the path gets stripped. As @przemas75 mentioned, I have the following portainer configuration:

```auto
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.portainer.rule=Host(`example.com`) && PathPrefix(`/portainer/`)"
      - "traefik.http.middlewares.portainerpathstrip.stripprefix.prefixes=/portainer"
      - "traefik.http.routers.portainer.middlewares=portainerpathstrip@docker"
      - "traefik.http.routers.portainer.entrypoints=web"
      - "traefik.http.routers.portainer.service=portainer"
      - "traefik.http.services.portainer.loadbalancer.server.port=9000"

```

That currently works. Adding some more services like:

```auto
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.loccmsapi.rule=Host(`example.com`) && PathPrefix(`/loccmsapi/`)"
      - "traefik.http.middlewares.loccmsapipathstrip.stripprefix.prefixes=/loccmsapi"
      - "traefik.http.routers.loccmsapi.middlewares=loccmsapipathstrip@docker"
      - "traefik.http.routers.loccmsapi.entrypoints=web"
      - "traefik.http.routers.loccmsapi.service=loccmsapi"
      - "traefik.http.services.loccmsapi.loadbalancer.server.port=80"

```

Receives the requests, but when I go to `example.com/loccmsapi/swagger` it loads the request but fails with the internal ones as it strips the path:

```auto
1. Request URL:http://example.com/swagger/v1/swagger.json
2. Request Method: GET
3. Status Code: 404 Not Found

```

But the file exists at `http://example.com/loccmsapi/swagger/v1/swagger.json`

What am I missing?

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [July 11, 2020, 1:46pm UTC](https://community.traefik.io/t/pathprefix-and-strip/2122/5 "2020-07-11T13:46:10Z")

</div>

Uhm, when you request `http://example.com/swagger/v1/swagger.json` why do you expect `http://example.com/loccmsapi/swagger/v1/swagger.json` to be hit?

---

<div class="post-metadata">

**Author:** ![Amleth](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/amleth/32/3218_2.png) [@Amleth](https://community.traefik.io/u/Amleth)\
**Post date:** [January 18, 2021, 10:59am UTC](https://community.traefik.io/t/pathprefix-and-strip/2122/6 "2021-01-18T10:59:36Z")

</div>

Is it possible, in Traefik, to add the prefix `loccmsapi`to each and every requests made by the loccmsapi service, so that it can find its internal resources?

---

<div class="post-metadata">

**Author:** ![cakiwi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cakiwi/32/3205_2.png) [@cakiwi](https://community.traefik.io/u/cakiwi)\
**Post date:** [January 18, 2021, 1:31pm UTC](https://community.traefik.io/t/pathprefix-and-strip/2122/7 "2021-01-18T13:31:28Z")

</div>


