Middlewares

Tweaking the Request

Overview

Attached to the routers, pieces of middleware are a means of tweaking the requests before they are sent to your service (or before the answer from the services are sent to the clients).

There are several available middleware in Traefik, some can modify the request, the headers, some are in charge of redirections, some add authentication, and so on.

Middlewares that use the same protocol can be combined into chains to fit every scenario.

Provider Namespace

Be aware of the concept of Providers Namespace described in the Configuration Discovery section. It also applies to Middlewares.

Configuration Example

Docker

  1. # As a Docker Label
  2. whoami:
  3. # A container that exposes an API to show its IP address
  4. image: traefik/whoami
  5. labels:
  6. # Create a middleware named `foo-add-prefix`
  7. - "traefik.http.middlewares.foo-add-prefix.addprefix.prefix=/foo"
  8. # Apply the middleware named `foo-add-prefix` to the router named `router1`
  9. - "traefik.http.routers.router1.middlewares=foo-add-prefix@docker"

Kubernetes IngressRoute

  1. ---
  2. apiVersion: traefik.io/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: stripprefix
  6. spec:
  7. stripPrefix:
  8. prefixes:
  9. - /stripit
  10. ---
  11. apiVersion: traefik.io/v1alpha1
  12. kind: IngressRoute
  13. metadata:
  14. name: ingressroute
  15. spec:
  16. # more fields...
  17. routes:
  18. # more fields...
  19. middlewares:
  20. - name: stripprefix

Consul Catalog

  1. # Create a middleware named `foo-add-prefix`
  2. - "traefik.http.middlewares.foo-add-prefix.addprefix.prefix=/foo"
  3. # Apply the middleware named `foo-add-prefix` to the router named `router1`
  4. - "traefik.http.routers.router1.middlewares=foo-add-prefix@consulcatalog"

Marathon

  1. "labels": {
  2. "traefik.http.middlewares.foo-add-prefix.addprefix.prefix": "/foo",
  3. "traefik.http.routers.router1.middlewares": "foo-add-prefix@marathon"
  4. }

Rancher

  1. # As a Rancher Label
  2. labels:
  3. # Create a middleware named `foo-add-prefix`
  4. - "traefik.http.middlewares.foo-add-prefix.addprefix.prefix=/foo"
  5. # Apply the middleware named `foo-add-prefix` to the router named `router1`
  6. - "traefik.http.routers.router1.middlewares=foo-add-prefix@rancher"

File (YAML)

  1. # As YAML Configuration File
  2. http:
  3. routers:
  4. router1:
  5. service: myService
  6. middlewares:
  7. - "foo-add-prefix"
  8. rule: "Host(`example.com`)"
  9. middlewares:
  10. foo-add-prefix:
  11. addPrefix:
  12. prefix: "/foo"
  13. services:
  14. service1:
  15. loadBalancer:
  16. servers:
  17. - url: "http://127.0.0.1:80"

File (TOML)

  1. # As TOML Configuration File
  2. [http.routers]
  3. [http.routers.router1]
  4. service = "myService"
  5. middlewares = ["foo-add-prefix"]
  6. rule = "Host(`example.com`)"
  7. [http.middlewares]
  8. [http.middlewares.foo-add-prefix.addPrefix]
  9. prefix = "/foo"
  10. [http.services]
  11. [http.services.service1]
  12. [http.services.service1.loadBalancer]
  13. [[http.services.service1.loadBalancer.servers]]
  14. url = "http://127.0.0.1:80"

Available Middlewares

A list of HTTP middlewares can be found here.

A list of TCP middlewares can be found here.


Using Traefik for Business Applications?

If you are using Traefik in your organization, consider our enterprise-grade solutions:

These tools help businesses discover, deploy, secure, and manage microservices and APIs easily, at scale, across any environment.