Reverse proxy with PathPrefix (and a service changing paths)

Hi, I've chosen a web service like Grafana to test a reverse proxy configuration in which there is a router rule PathPrefix.
What I would like to accomplish:

This is the (not working) config I've put together:

docker-compose.yml

version: "3.3"

services:

  traefik:
    image: "traefik:latest"
    container_name: "traefik"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "$PWD/traefik.yml:/etc/traefik/traefik.yml"
      - "$PWD/providers.yml:/etc/traefik/providers.yml"

  grafana:
    image: "grafana/grafana"
    container_name: "grafana"
    labels:
      - "traefik.enable=true"
    volumes:
      - "$PWD/grafana.ini:/etc/grafana/grafana.ini"

traefik.yml

log:
  level: DEBUG

api:
  dashboard: true
  insecure: true

providers:
  docker:
    exposedByDefault: false
  file:
    filename: "/etc/traefik/providers.yml"
    watch: true

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
  websecure:
    address: ":443"

providers.yml

http:

  routers:
    to-secure-grafana:
      rule: "PathPrefix(`/grafana`)"
      entryPoints: websecure
      service: grafana
      tls: {}
    
  services:
    grafana:
      loadBalancer:
        servers:
          - url: "http://grafana:3000"

What happens here (I think) is that the redirection occurs but then the path is no more with the prefix "grafana". I've also tried some configurations adopting middlewares with ReplacePath and StripPrefix, but with no luck.
Do you think there is a way to accomplish my need without touching the configuration of the service itself?

The service will likely need to be reconfigured if you want to use a path prefix.

Alternatively use a subdomain.

1 Like

Thank you so much.

I tried to configure Grafana in order to work with a subpath as specified in their documentation.
If I expose directly the grafana container port 3000 (hence, without passing through Traefik), everything works correctly. I point to the URL http://myserverip:3000/grafana and I'm being redirected to http://myserverip:3000/grafana/login

Also the logs from Docker seems to be OK:
grafana | t=2021-04-09T15:58:30+0000 lvl=info msg="HTTP Server Listen" logger=http.server address=[::]:3000 protocol=http subUrl=/grafana socket=

Therefore, I suppose that the configuration of the service from this point of view is now correct.

Now with this configuration in place for the service, I've modified the providers.yml as follows:

http:

  routers:
    to-secure-grafana:
      rule: "PathPrefix(`/grafana`)"
      entryPoints: websecure
      service: grafana
      tls: {}
    
  services:
    grafana:
      loadBalancer:
        servers:
          - url: "http://grafana:3000/grafana"

And this appears to be a working configuration :slight_smile:

If you have any observation, you're welcome

1 Like