Search code examples
traefikgatsby

Why does PathPrefixStrip work when PathPrefix won't?


I have a GatsbyJS static site built with --prefix-paths. The pathPrefix is set to /environment/test in gatsby-config.js. It is deployed to a docker swarm running Traefik.

Adding the following label to the service makes everything run ok: traefik.frontend.rule=PathPrefixStrip:/environment/test

I can then browse to /environment/test and click around in my GatsbyJs site.

However I find it strange since the backend is build with the path prefix.

Adding the following label does not work: traefik.frontend.rule=PathPrefix:/environment/test

Shouldn't it work with PathPrefix instead of PathPrefixStrip?


Solution

  • The pathPrefix configuration in GatsbyJS means that every link of your website will be prepended with /environment/test (see documentation), but this does not mean that on the container running the website, the page is actually hosted on this path. In your situation, it seems it is not the case.

    This means that when connecting via Traefik with the PathPrefixStrip, when in your browser you click on a link:

    1. The browser requests /environment/test/page
    2. Traefik translates this to a request to the container for /page
    3. The container is actually serving files on / so it answers with the page

    When connecting via Traefik with the PathPrefix:

    1. The browser requests /environment/test/page
    2. Traefik translates this to a request to the container for /environment/test/page
    3. The container is actually serving files on / so it doesn't find the page.

    Hence, you are confusing the pathPrefix setting of a website with the path from which the very same website is served.

    The alternative to the current situation would thus be to serve the website under /environment/test/ and use PathPrefix with traefik.