HTTP to HTTPS redirection with Terraform and Docker - Middleware not recognized in Traefik Configuration

Hello everybody !

I've been encountering an issue with Traefik where a defined middleware is not being recognized. I would greatly appreciate any insights or suggestions.

The https:// works well but if I go on http:// I get an 404 page.

Here's my traefik.toml configuration:

# EntryPoints definition
[entryPoints]
  [entryPoints.web]
  address = ":80"

  [entryPoints.websecure]
  address = ":443"

# Providers configuration
[providers]
  [providers.docker]
  endpoint = "unix:///var/run/docker.sock"
  exposedByDefault = false

# Let's Encrypt Certificate Resolver
[certificatesResolvers.letsencrypt.acme]
  email = "my@email.fr"
  storage = "/etc/traefik/acme.json"
  [certificatesResolvers.letsencrypt.acme.httpChallenge]
  entryPoint = "web"

# Middleware
[http.middlewares]
[http.middlewares.https-redirect.redirectScheme]
  scheme = "https"

here is my Traefik Docker Container configuration in Terraform:

// Ressource pour l'image Docker Traefik
resource "docker_image" "traefik_image" {
  name = "traefik:v2.10" // nom de l'image Docker
}

resource "docker_container" "traefik" {
  name  = "traefik"
  image = docker_image.traefik_image.image_id
  command = [
    "--api.insecure=true --providers.docker",
    "--api.dashboard=true",
  "--providers.docker.exposedbydefault=false"]

  ports {
    internal = 443
    external = 443
  }
  ports {
    internal = 80
    external = 80
  }
  volumes {
    container_path = "/etc/traefik/traefik.toml"
    host_path      = "/etc/traefik/traefik.toml"
  }
  volumes {
    container_path = "/var/run/docker.sock"
    host_path      = "/var/run/docker.sock"
    read_only      = true
  }
}

And here's the relevant part of my Terraform configuration for the WordPress Docker container:

resource "docker_container" "fnsf_wordpress_container" {
  ...
  labels {
    label = "traefik.enable"
    value = "true"
  }
  labels {
    label = "traefik.http.routers.fnsf_wordpress.rule"
    value = "Host(`my.domain.fr`)"
  }
  labels {
    label = "traefik.http.services.fnsf_wordpress.loadbalancer.server.port"
    value = "80"
  }
  labels {
    label = "traefik.http.routers.fnsf_wordpress.entrypoints"
    value = "websecure"
  }
  labels {
    label = "traefik.http.routers.fnsf_wordpress.tls.certresolver"
    value = "letsencrypt"
  }
  labels {
    label = "traefik.http.routers.fnsf_wordpress.middlewares"
    value = "https-redirect"
  }
  ...
}

The problem is that I keep getting the following error in the Traefik logs:

level=info msg="Configuration loaded from file: /etc/traefik/traefik.toml"
level=error msg="middleware \"https-redirect@docker\" does not exist" routerName=fnsf_wordpress@docker entryPointName=websecure
level=error msg="middleware \"https-redirect@docker\" does not exist" entryPointName=websecure routerName=fnsf_wordpress@docker
level=error msg="middleware \"https-redirect@docker\" does not exist" routerName=fnsf_wordpress@docker entryPointName=websecure
level=error msg="middleware \"https-redirect@docker\" does not exist" entryPointName=websecure routerName=fnsf_wordpress@docker
2023/08/25 17:21:38 reverseproxy.go:667: httputil: ReverseProxy read error during body copy: unexpected EOF
2023/08/25 17:35:37 reverseproxy.go:667: httputil: ReverseProxy read error during body copy: unexpected EOF

Even though I've clearly defined the middleware in the traefik.toml file. Does anyone have any idea why the middleware might not be recognized? Any help would be greatly appreciated!

Thank you in advance.

Maybe try with "@file", if the middleware is defined in a dynamic config file read with provider.file:

  labels {
    label = "traefik.http.routers.fnsf_wordpress.middlewares"
    value = "https-redirect@file"
  }

Note that you can declare a redirect globally in the entrypoint directly, see simple Traefik example.

In the logs we can see "https-redirect@docker\" but I don't know why.

I don't use provider.file, the middleware is define in my traefik.tolm

Traefik middleware (doc) is dynamic configuration, which needs to be provided by a provider. When using a file for dynamic config, it needs to be loaded with providers.file in static config (like traefik.toml).

Thanks for your reply.

I did not manage to fix by using providers.file but by

declare a redirect globally in the entrypoint directly

as you said in your first reply. Thanks for that !

For people who want to know how I done this, in my .toml file I add:

[entryPoints.web.http.redirections.entrypoint]
  to = "websecure"
  scheme = "https"

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