Search code examples
varnishtraefik

Traefik - how to add custom header "Cache-Control"


I use Traefik 2.4 + Docker + Varnish.

I want for my dev environment to disable the Varnish cache, I can do it using headers:

Cache-control: no-cache
Pragma: no-cache

So in my docker-compose proxy file, I used:

  varnish:
    labels:
      - "traefik.http.routers.api_store_varnish.rule=Host(`${APP_ENV:-dev}.${APP_NAME:-api-store}.${DOMAIN_EXTENSION:-local}`, `api_store`)"
      - "traefik.http.routers.api_store_varnish.tls=true"
      - "traefik.http.services.api_store_varnish.loadbalancer.server.port=80"
      - "traefik.http.middlewares.no-cache.headers.Cache-Control=no-cache"
      - "traefik.http.middlewares.no-cache.headers.Pragma=no-cache"
      - "traefik.http.routers.api_store_varnish.middlewares=no-cache@docker"

I looked at the doc https://doc.traefik.io/traefik/middlewares/overview/ and https://doc.traefik.io/traefik/middlewares/headers/, but for now, my container is in 404 and doesn't show in my Traefik dashboard.

What is the correct way to deal with the headers ?


Solution

  • I think there's an issue with your labels. Checking the docs, they would get you to do something like this instead:

    labels:
    ...
    - "traefik.http.middlewares.testHeader.headers.customrequestheaders.Cache-Control=no-cache"
    - "traefik.http.middlewares.testHeader.headers.customrequestheaders.Pragma=no-cache"
    

    Any change, adding those customrequestheaders in between the middleware name and your header name?