traefik: entryPoints redirect not working

I want redirect in the following case http://mysite.com => https://www.mysite.com and https://mysite.com => https://www.mysite.com. Why is not working?

traefik config

defaultEntryPoints = ["https"]

[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
      regex = "^http://mysite.com/(.*)"
      replacement = "https://www.mysite.com/$1"
  [entryPoints.https]
    address = ":443"
    [entryPoints.https.tls]
    [entryPoints.https.redirect]
      regex = "^https://mysite.com/(.*)"
      replacement = "https://www.mysite.com/$1"


[acme]
email = "..."
storageFile = "/acmestorage/acme.json"
entryPoint = "https"
[[acme.domains]]
   main = "mysite.com"
   sans = ["www.mysite.com"]

docker-compose for traefik

version: "2"
services:
  proxy:
    image: traefik:v1.1.1
    command: --web --docker --docker.domain=mysite.com --logLevel=ERROR
    volumes:
      - ./config.toml:/traefik.toml
      - ./storage:/acmestorage
      - /var/run/docker.sock:/var/run/docker.sock:rw
    ports:
      - "80:80"
      - "443:443"
      - "8089:8080"
networks:
  default:
    external:
      name: internal_network

docker-compose for app

version: "2"
services:
  app:
        image: golang:onbuild
        command: app.bin -config=config.json -stderrthreshold=INFO -v=0
        volumes:
          - ./state/app:/go/src/app
          - ./state/app/bin/app.bin:/usr/local/bin/app.bin
        ports:
          - 8080:80
        links:
          - postgres
          - elasticsearch
        labels:
            - "traefik.backend=mysite"
            - "traefik.port=80"
            - "traefik.frontend.rule=Host:www.mysite.com"
#
#
#
networks:
  default:
    external:
      name: internal_network

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

I found a work around based on @jibingeo 's comments, the following is a stripped down version of my configurations:

services:
  static-files:
    image: mysite/mysite-static-files
    labels:
      - "traefik.backend=static-files"
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:www.mysite.com,mysite.com"
  chat-server:
    image: mysite/mysite-chat
    labels:
      - "traefik.backend=chat-server"
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:www.mysite.com,mysite.com;PathPrefixStrip: /chat"
  auth-server:
    image: mysite/mysite-auth-server
    labels:
      - "traefik.backend=auth-server"
      - "traefik.port=80"
      - "traefik.frontend.rule=Host:www.mysite.com,mysite.com;PathPrefixStrip: /auth"
  traefik:
    image: traefik
    command: --web --docker --docker.domain=mysite.com --logLevel=ERROR
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - /home/centos/mysite-bundle.pem:/ca.crt
      - /home/centos/ca.key:/ca.key

and my toml

# traefik.toml
defaultEntryPoints = ["http", "https"]
[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
    regex = "^http://mysite.com/(.*)"
    replacement = "https://www.mysite.com/$1"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]
      [[entryPoints.https.tls.certificates]]
      certFile = "/ca.crt"
      keyFile = "/ca.key"
    [entryPoints.https.redirect]
      regex = "^https://mysite.com/(.*)"
      replacement = "https://www.mysite.com/$1"

It looks like You need to include mysite.com and www.mysite.com as a front-end rule. Traefik appears to re-route correctly, but for whatever reason, it’s needed as a front-end rule.

No. We just stopped using traefik

After spending some time on this issue, I found that http to https redirection won’t work if you don’t have matching front-end rule.

ie. If https://hello.com return 404, then http://hello.com will also return 404 instead of redirecting to https://hello.com