HTTP Middlewares

Controlling connections

Overview

Configuration Example

Docker & Swarm

  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. # As a Kubernetes Traefik IngressRoute
  2. ---
  3. apiVersion: traefik.io/v1alpha1
  4. kind: Middleware
  5. metadata:
  6. name: stripprefix
  7. spec:
  8. stripPrefix:
  9. prefixes:
  10. - /stripit
  11. ---
  12. apiVersion: traefik.io/v1alpha1
  13. kind: IngressRoute
  14. metadata:
  15. name: ingressroute
  16. spec:
  17. # more fields...
  18. routes:
  19. # more fields...
  20. middlewares:
  21. - 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"

File (TOML)

  1. # As TOML Configuration File
  2. [http.routers]
  3. [http.routers.router1]
  4. service = "service1"
  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"

File (YAML)

  1. # As YAML Configuration File
  2. http:
  3. routers:
  4. router1:
  5. service: service1
  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"

Available HTTP Middlewares

MiddlewarePurposeArea
AddPrefixAdds a Path PrefixPath Modifier
BasicAuthAdds Basic AuthenticationSecurity, Authentication
BufferingBuffers the request/responseRequest Lifecycle
ChainCombines multiple pieces of middlewareMisc
CircuitBreakerPrevents calling unhealthy servicesRequest Lifecycle
CompressCompresses the responseContent Modifier
ContentTypeHandles Content-Type auto-detectionMisc
DigestAuthAdds Digest AuthenticationSecurity, Authentication
ErrorsDefines custom error pagesRequest Lifecycle
ForwardAuthDelegates AuthenticationSecurity, Authentication
HeadersAdds / Updates headersSecurity
IPAllowListLimits the allowed client IPsSecurity, Request lifecycle
InFlightReqLimits the number of simultaneous connectionsSecurity, Request lifecycle
PassTLSClientCertAdds Client Certificates in a HeaderSecurity
RateLimitLimits the call frequencySecurity, Request lifecycle
RedirectSchemeRedirects based on schemeRequest lifecycle
RedirectRegexRedirects based on regexRequest lifecycle
ReplacePathChanges the path of the requestPath Modifier
ReplacePathRegexChanges the path of the requestPath Modifier
RetryAutomatically retries in case of errorRequest lifecycle
StripPrefixChanges the path of the requestPath Modifier
StripPrefixRegexChanges the path of the requestPath Modifier

Community Middlewares

Please take a look at the community-contributed plugins in the plugin catalog.


Using Traefik OSS in Production? Consider Adding Advanced Capabilities.

Add API Gateway or API Management capabilities seamlessly to your existing Traefik deployments. No rip and replace. No learning curve.