Search code examples
dockerdocker-composetraefik

Traefik middleware redirection from www to non-www throws 404 error


I created a Traefik (v2.9.8) middleware, seems to be correctly detected by Traefik (showing up in the Dashboard and it is applied to the site's HTTP Router), but when I go to www.example.com Traefik throws a 404 error.

Docker Compose:

version: "3.1"

services:
  traefik:
    restart: unless-stopped
    image: traefik:v2.9.8
    security_opt:
      - no-new-privileges:true
    ports:
      - "80:80"
      - "443:443"
    volumes:
      # Allow Traefik to use the system's Docker socket
      # MUST be the same as in the config file "traefik.yml"
      - /var/run/docker.sock:/var/run/docker.sock:ro
      # Map the static conf into the container
      - ./config/traefik/traefik.yml:/etc/traefik/traefik.yml:ro
      # Map the dynamic conf into the container
      - ./config/traefik/config.yml:/etc/traefik/config.yml:ro
      - ./logs/traefik:/traefik-logs/
      # Map the certificates into the container
      - ./config/local-certs:/etc/certs:ro
    networks:
      - my_website
    environment:
      # Some env config, this works OK
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.tls=true"
      - "traefik.http.routers.traefik.tls.certresolver=myresolver"
      - "traefik.http.routers.traefik.entrypoints=https"
      - "traefik.http.routers.traefik.rule=Host(`${TRAEFIK_DASHBOARD_SUBDOMAIN}.${ENV_HOST}`)"

# Other services here...

  wordpress:
    build:
      context: ./wordpress
    image: wordpress:v1
    restart: unless-stopped
    volumes:
      # My volumes, working OK
    networks:
      - my_website
    environment:
      # Some env config, this works OK too
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.wordpress.tls=true"
      - "traefik.http.routers.wordpress.entrypoints=https"
      - "traefik.http.routers.wordpress.middlewares=redirect-www@file"
      - "traefik.http.routers.wordpress.rule=Host(`${ENV_HOST}`)"

Traefik main file (middleware at the EOF):

global:
  sendAnonymousUsage: false

api:
  dashboard: true
  insecure: false

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

  file:
    # We use the same file as in the proxy volume
    directory: /etc/traefik
    watch: true

log:
 # Path inside the container
 filePath: "/traefik-logs/traefik-service.log"
 # Alternative logging levels are: "DEBUG", "PANIC", "FATAL", "ERROR", "WARN", and "INFO".
 level: WARN
 format: common

entryPoints:
  http:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: https
          scheme: https
  https:
    address: ":443"

# Redirect www to non-www
http:
   middlewares:
     redirect-www:
       redirectRegex:
         regex: "^(https:\\/\\/)?www\\.(.+)"
         replacement: "https://${2}"
         permanent: true

Am I missing something here?


Solution

  • Yes, I was missing something: add a www into my website's service Host label.

    - "traefik.http.routers.wordpress.rule=Host(`${ENV_HOST}`, `www.${ENV_HOST}`)"
    

    This fixed the issue and is now working and redirecting properly from www to non-www.