Middleware issue: "does not exist"

Hi, I'm struggling with an issue related to middlewares.

I have created a middleware named secure-headers in my traefik.yml file, and I'm trying to reference it my docker-compose.yml file, but it keeps telling me the middleware does not exist.

I've defined the following in my traefik.yml file:

# ... other configuration here (let me know if you need to see it)

middlewares:
  secure-headers:
    headers:
      stsSeconds: 31536000                # Strict-Transport-Security
      stsIncludeSubdomains: true
      stsPreload: true
      browserXssFilter: true              # X-XSS-Protection
      frameDeny: true                     # X-Frame-Options
      featurePolicy: "vibrate 'none';"    # Feature-Policy header
      contentTypeNosniff: true            # X-Content-Type-Options
      referrerPolicy: "strict-origin-when-cross-origin"

and in my docker-compose.yml:

# ...other configuration here (let me know if you need to see it)
  traefik:
    image: "traefik:v2.1"
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./traefik.yml:/traefik.yml:ro"
      - "./certs:/certs:ro"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.http-catchall.rule=hostregexp(`{host:[a-z-.]+}`)"
      - "traefik.http.routers.http-catchall.entrypoints=web"
      - "traefik.http.routers.http-catchall.middlewares=redirect-to-https"
      - "traefik.http.routers.http-catchall.middlewares=secure-headers@file"
      - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"

and finally, in my logs:

traefik_1  | time="2020-03-11T16:31:45Z" level=error msg="middleware \"secure-headers@file\" does not exist" entryPointName=web routerName=http-catchall@docker

Hello,

Labels are map, so the key must be unique.

traefik.http.routers.http-catchall.middlewares is a key.

-  "traefik.http.routers.http-catchall.middlewares=redirect-to-https,secure-headers@file"

Also put the secure-headers is useless because you are using a redirect.

Ok, I understand now; thanks! I think I'll try setting entry-point defaults, which I see were introduced in v2.2-rc1.

Would you recommend that as an alternative solution?

I recommend to use the v2.2 :smile:

The default router configuration on entry point is the best solution to apply an middleware on all the routers related to an entry point.

1 Like