404 page not found when using with PathPrefix

Hi!
Sorry I'm back again with Path based routing doubt :melting_face:, you told me that to use PathPrefix there should be a route specified to that particular path. So I took an sample container like grafana and if I goto localhost:6061 it redirects me to localhost:6061/login so there is a route specified for login, So I tried setting the rule like this
Host(localhost) && PathPrefix(/login)
which needs to redirect me to login page right? but I'm getting 404 why is that? and I recently read an article where he used PathPrefix to multiple paths and published his app without any routes configured to that path, can you explain why it is working for him and not for us?
the below is my docker-compose.yml:

version: "3"
services:
  traefik:
    image: 'traefik:latest'
    container_name: 'traefik'
    # network_mode: 'host'
    command: 
      - "--api.insecure=true"
      - '--accesslog=true'
      - "--providers.docker"
      - "--log.level=DEBUG"
      - "--entrypoints.web.address=:80"
      - "--entrypoints.websecure.address=:443"
      - "--providers.docker.exposedByDefault=false" 
      # - "--providers.docker.endpoint=unix:///var/run/docker.sock"
      # - "--providers.docker.useBindPortIP=true"
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
      
  grafana:
    image: test:latest
    container_name: grafana
    ports: 
     - "6060:80"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.grafana.rule=Host(`vnc.localhost`) && PathPrefix(`/user`)"
      - "traefik.http.routers.grafana.entrypoints=web"
      - "traefik.http.routers.grafana.service=grafana"
      - "traefik.http.middlewares.grafana-stripprefix.stripprefix.prefixes=/user"
      - "traefik.http.routers.grafana.middlewares=grafana-stripprefix"
      - "traefik.http.services.grafana.loadbalancer.server.port=80"

  monitor:
    image: grafana/grafana
    container_name: monitor
    ports: 
     - "6061:3000"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.monitor.rule=Host(`localhost`) && PathPrefix(`/login`)"
      - "traefik.http.routers.monitor.entrypoints=web"
      - "traefik.http.routers.monitor.service=monitor"
      # - "traefik.http.middlewares.monitor-stripprefix.stripprefix.prefixes=/login"
      # - "traefik.http.routers.grafana.middlewares=grafana-stripprefix"
      - "traefik.http.services.grafana.loadbalancer.server.port=3000"

The monitor service is my grafana


The article is using self-developed source code that simply respond with "hello" to any request, path doesn't matter. This is not how real world apps behave :wink:

1 Like

Can you solve my doubt for the grafana thing?

Grafana supports setting a base path (link):

root_url

This is the full URL used to access Grafana from a web browser. This is important if you use Google or GitHub OAuth authentication (for the callback URL to be correct).

NOTE

This setting is also important if you have a reverse proxy in front of Grafana that exposes it through a subpath. In that case add the subpath to the end of this URL setting.

But that does not mean you can run other GUI web apps with a path.

I have a certificate from AWS Certificate manager and my domain is *.abc.xyz.com and I dont know about lets encrypt and acme lables and I want to know what are the correct labels that I have to use, Can you please guide me?

Load certs in dynamic config file (doc), load the file in static config with providers.file. Enable TLS globally on websecure entrypoint or individually on router (yaml config .tls={} or labels .tls=true).

See simple Traefik example for minimum config. Traefik will use the domain from Host() to find a matching loaded cert.

  whoami:
    image: traefik/whoami:v1.10
    networks:
      - proxy
    labels:
      - traefik.enable=true
      - traefik.http.routers.mywhoami.entrypoints=websecure
      - traefik.http.routers.mywhoami.tls=true
      - traefik.http.routers.mywhoami.rule=Host(`whoami.example.com`) 
      - traefik.http.services.mywhoami.loadbalancer.server.port=80

Can you explain why do we need certificate needs to be in the config file for dynamic subdomain routing, I just want to know how this is working.

I have a amazon certificate for my domain *.abc.xyz.com and I will attach this certificate to my amazon load balancer , so if I route traffic from abc1.abc.xyz.com which comes to my server is http (because the ssl termination happens at loadbalancer and internal traffic that comes is http) and why do I have to give the entrypoint as https in traefik? and I just want to how dynamic subdomain routing works in traefik and if I have attached my certificate to my load balancer will this( abc1.abc.xyz.com) directly route to my server?

If you terminate the TLS connection already at an external load balancer (and forward all requests with plain http), then you don't need any TLS in Traefik.

whoami:
    image: traefik/whoami:v1.10
    networks:
      - proxy
    labels:
      - traefik.enable=true
      - traefik.http.routers.mywhoami.rule=Host(`whoami.example.com`) 
      # next line is the port the application is listening to inside the container
      - traefik.http.services.mywhoami.loadbalancer.server.port=80

(We have load balancer on TCP level, they don't have any TLS certs, so the external provider can't look into our traffic, we terminate TLS only on our machines with Traefik.)

Then I don't need to load the certs in traefik?

I'm getting the below error while setting up
sudomain1.subdomain.example.com as a host in traefik lables

Your connection is not private

Attackers might be trying to steal your information from sudomain1.subdomain.example.com (for example, passwords, messages or credit cards). Learn more

NET::ERR_CERT_COMMON_NAME_INVALID

Sorry I didn't attached the certificate to loadbalancer thats why I was getting this error, So everything is solved now, Now I'm able to do the dynamical subdomain routing.
Thanks @bluepuma77 for solving every basic doubt about the traefik, I hope our discussion might be helpful for many beginners who are starting to work with traefik.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.