Stupid question time about routing

Time for some stupid questions. I was playing around with https and ended up ditching it because I couldn't get the basics working. I haven't played around with web development in quite a few years and think I have forgotten more than I ever learned. The questions I'm about to ask may not even relate to Traefik, in which case please let me know and I'll go looking elsewhere (domain related). I suppose that's the big question, which of these issues are handled by Traefik and which ones are handled by something else.

Setup:

  • Domain names are with NameCheap and DNS point to DigitalOcean (ns1.digitalocean.com, etc).
  • DNS records handled through DigitalOcean:
    • A record points to server IP
    • CNAME *.mydomain.com is an alias of mydomain.com

What works:

  • going to subdomain.mydomain.com works and shows appropriate content such as traefik dashboard, flask site, etc.

What doesn't work:

  • Putting www in front, such as www.subdomain.mydomain.com shows "404 page not found" but works for traefik dashboard so I'm wondering if this get's handled in flask or in Traefik? I tried searching and came up empty handed, maybe I just searched the wrong thing.
  • How do you do redirects in Traefik 2.0 to go from http to https automatically so your really only running one website, can you do that? I spent ages trying to do this when I was first trying to learn Traefik and gave up.

Thanks in advance for any assistance.

1 Like

Hi, have you found any solution to this problem?

Hello @mindgonemad,

This is because Traefik treats subdomains as separate hosts, as it should. The Traefik dashboard is specifically written to not require a host, and therefore works as it should.

As for redirects, you should look at the RedirectScheme Middleware:

https://docs.traefik.io/v2.0/middlewares/redirectscheme/#redirectscheme

An example of this in the docker provider can be found here: https://github.com/containous/traefik/issues/4688#issuecomment-477800500

Thanks for responding Daniel.

Another question regarding redirects; I stumbled on an Containous article that uses a global https redirect (https://blog.containo.us/traefik-2-0-docker-101-fc2893944b9d) but could never get it working. Is this the way we should be heading or should we be putting a redirect on every service as per the example that was posted?

I should clarify: It redirects to https but shows 404 on both services, maybe the TLS isn't working properly???

version: "3.7"

services:

  traefik:
    image: "traefik:v2.0.2"
    command:
      - --entrypoints.web.address=:80
      - --entrypoints.websecure.address=:443
      - --providers.docker=true
      - --api=true
      #- --certificatesresolvers.leresolver.acme.caserver=https://acme-staging-v02.api.letsencrypt.org/directory
      - --certificatesresolvers.leresolver.acme.email=<email>
      - --certificatesresolvers.leresolver.acme.storage=/letsencrypt/acme.json
      - --certificatesresolvers.leresolver.acme.tlschallenge=true
      # Logging
      - "--log.level=ERROR" # DEBUG, ERROR, INFO???
      - "--log.filePath=/traefik.log"
      - "--log.format=json"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./logs/traefik.log:/traefik.log"
      - "./letsencrypt:/letsencrypt"
    labels:
      # Dashboard
      - "traefik.http.routers.traefik.rule=Host(`traefik.creativesandbox.dev`)"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.tls.certresolver=leresolver"
      - "traefik.http.routers.traefik.entrypoints=websecure"
      - "traefik.http.routers.traefik.middlewares=authtraefik"
      - "traefik.http.middlewares.authtraefik.basicauth.users=<basic_auth_stuff>"
      
      # global redirect to https
      - "traefik.http.routers.http-catchall.rule=hostregexp(`{host:.+}`)"
      - "traefik.http.routers.http-catchall.entrypoints=web"
      - "traefik.http.routers.http-catchall.middlewares=redirect-to-https"

      # middleware redirect
      - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
    networks:
      - traefik-public

  my-app:
    image: containous/whoami:v1.3.0
    labels:
      - "traefik.http.routers.my-app.rule=Host(`traefik-whoami-tls.creativesandbox.dev`)"
      - "traefik.http.routers.my-app.middlewares=auth"
      - "traefik.http.routers.my-app.entrypoints=web"
      - "traefik.http.routers.my-app.tls=true"
      - "traefik.http.routers.my-app.tls.certresolver=leresolver"
      - "traefik.http.middlewares.auth.basicauth.users=<basic_auth_stuff>"

networks:
  traefik-public:
    driver: overlay
    name: traefik-public

Thanks