transistor
transistor

Reputation: 31

Traefik: all subdirectories return 404

First, thank you in advance for taking a look. I think I have a very basic mistake somewhere, but I have searched for hours with no result. I am trying to run a proof of concept to expose a container behind a traefik 2.4 reverse proxy at a subdirectory. My DDNS does not allow for subdomains, so I am stuck with subdirectories until I can prove this works.

My problem is every container I stand up is dynamically picked up by traefik and shows up in the dashboard, but the subdirectory gives a 404 error. I have even used PathPrefix with a regex to prevent the ending / error.

Here is my configuration.

Traefik's docker-compose:

version: '3'

services:
        traefik:
                image: traefik:v2.4
                container_name: traefik
                restart: unless-stopped
                security_opt:
                        - no-new-privileges:true
                networks:
                        - t2_proxy
                ports:
                        - 80:80
                        - 443:443
                volumes:
                        - /etc/localtime:/etc/localtime:ro
                        - /var/run/docker.sock:/var/run/docker.sock:ro
                        - ./data/traefik.yml:/traefik.yml:ro
                        - ./data/acme.json:/acme.json
                        - ./data/log:/var/log

                labels:
                        - "traefik.enable=true"
                        - "traefik.http.routers.traefik.entrypoints=http"
                        - "traefik.http.routers.traefik.rule=Host(`domain.host.com`)"
                        - "traefik.http.middlewares.traefik-auth.basicauth.users=user:password"
                        - "traefik.http.middlewares.traefik-https-redirect.redirectscheme.scheme=https"
                        - "traefik.http.routers.traefik.middlewares=traefik-https-redirect"
                        - "traefik.http.routers.traefik-secure.entrypoints=https"
                        - "traefik.http.routers.traefik-secure.rule=Host(`domain.host.com`)"
                        - "traefik.http.routers.traefik-secure.middlewares=traefik-auth"
                        - "traefik.http.routers.traefik-secure.tls=true"
                        - "traefik.http.routers.traefik-secure.tls.certresolver=http"
                        - "traefik.http.routers.traefik-secure.service=api@internal"

        fail2ban:
                image: crazymax/fail2ban:latest
                container_name: fail2ban
                network_mode: "host"
                cap_add:
                        - NET_ADMIN
                        - NET_RAW
                volumes:
                        #                        - /var/log:/var/log:ro
                        - ./fail2ban/data:/data
                        - ./data/log:/var/log:ro
networks:
        t2_proxy:
                external: true

Here is my traefik.yml configuration file:

api:
        dashboard: true

entryPoints:
        http:
                address: ":80"
        https:
                address: ":443"

providers:
        docker:
                endpoint: "unix:///var/run/docker.sock"
                exposedByDefault: false

certificatesResolvers:
        http:
                acme:
                        email: [email protected]
                        storage: acme.json
                        httpChallenge:
                                entrypoint: http

log:
        filePath: "/var/log/traefik.log"
        level: DEBUG
accessLog:
        filePath: "var/log/access.log"
        filters:
                statusCodes:
                        - "400-499"
                retryAttempts: true

Here is the first proof-of-concept container I'm trying to expose. It's just portainer in a separate docker-compose:

version: '3'

services:
        portainer:
                image: portainer/portainer-ce:latest
                container_name: portainer
                restart: unless-stopped
                security_opt:
                        - no-new-privileges:true
                networks:
                        - t2_proxy
                ports:
                        - "9000:9000"
                volumes:
                        - /etc/localtime:/etc/localtime:ro
                        - /var/run/docker.sock:/var/run/docker.sock:ro
                        - ./data:/data
                labels:
                        - "traefik.enable=true"

                          #web routers
                        - "traefik.http.routers.portainer.entrypoints=http"
                        - "traefik.http.routers.portainer.rule=Host(`domain.host.com`) && PathPrefix(`/portainer`)"
                          #- "traefik.http.routers.portainer.rule=Host(`domain.host.com`) && PathPrefix(`/portainer{regex:$$|/.*}`)"
                          #- "traefik.http.routers.portainer.rule=Path(`/portainer`)"
                          #- "traefik.http.routers.portainer.rule=PathPrefix(`/portainer{regex:$$|/.*}`)"

                          #middlewares
                          #- "traefik.http.routers.portainer.middlewares=portainer-stripprefix"
                          #- "traefik.http.middlewares.portainer-stripprefix.stripprefix.prefixes=/portainer"
                        - "traefik.http.middlewares.portainer-https-redirect.redirectscheme.scheme=https"
                        - "traefik.http.routers.portainer.middlewares=portainer-https-redirect"

                          #web secure rpiters
                        - "traefik.http.routers.portainer-secure.entrypoints=https"
                        - "traefik.http.routers.portainer-secure.rule=Host(`domain.host.com`) && PathPrefix(`/portainer`)"
                          #- "traefik.http.routers.portainer-secure.rule=Host(`domain.host.com`) && PathPrefix(`/portainer{regex:$$|/.*}`)"
                          #- "traefik.http.routers.portainer-secure.rule=Path(`/portainer`)"
                          #- "traefik.http.routers.portainer-secure.rule=PathPrefix(`/portainer{regex:$$|/.*}`)"
                          #- "traefik.http.routers.portainer-secure.middlewares=chain-basic-auth@users"
                        - "traefik.http.routers.portainer-secure.tls=true"
                        - "traefik.http.routers.portainer-secure.tls.certresolver=http"
                        - "traefik.http.routers.portainer-secure.service=portainer"
                        - "traefik.http.services.portainer.loadbalancer.server.port=9000"
                        - "traefik.docker.network=t2_proxy"
networks:
        t2_proxy:
                external: true

In summary, I navigate to domain.host.com, and it behaves properly by redirecting me to domain.host.com/dashboard. However, when I go to domain.host.com/portainer it gives a 404 error.

Please let me know if I should post any other details. I sense I am missing a very obvious bit of configuration, as this is my first time using Traefik. Thanks again for any help!

Upvotes: 1

Views: 1873

Answers (1)

transistor
transistor

Reputation: 31

For future googlers

Alright, I figured it out tonight. Thank you, reddit.com/traefik user /u/Quafeinum for trying to help! I actually read the guide here: https://spad.uk/practical-configuration-of-traefik-as-a-reverse-proxy-for-docker/ by spad on linuxserver.io which helped me understand the labels better. The crux of the problem was

traefik.http.services.whoami-whoami.loadbalancer.server.scheme=https

Whatever that does, it was in all the examples, and I mindlessly copied it (there's a cautionary tale here). After removing it, the containers are properly exposed on HTTPS now. Verified with portainer and whoami.

Here is a link to a pastebin of the relevant docker-composes and yamls. This will get a functioning traefik that dynamically loads docker container whoami over HTTPS.

https://pastebin.com/AfBdz6Qm

Upvotes: 2

Related Questions