traefik/docs/content/middlewares/overview.md

118 lines
3.1 KiB
Markdown
Raw Normal View History

---
title: "Traefik Proxy Middleware Overview"
description: "There are several available middleware in Traefik Proxy used to modify requests or headers, take charge of redirections, add authentication, and so on."
---
# Middlewares
Tweaking the Request
{: .subtitle }
![Overview](../assets/img/middleware/overview.png)
2020-02-04 20:20:04 +00:00
Attached to the routers, pieces of middleware are a means of tweaking the requests before they are sent to your [service](../routing/services/index.md) (or before the answer from the services are sent to the clients).
2020-02-04 20:20:04 +00:00
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.
2021-06-11 13:30:05 +00:00
Middlewares that use the same protocol can be combined into chains to fit every scenario.
!!! warning "Provider Namespace"
2021-02-11 13:34:04 +00:00
Be aware of the concept of Providers Namespace described in the [Configuration Discovery](../providers/overview.md#provider-namespace) section.
It also applies to Middlewares.
## Configuration Example
2023-05-10 13:28:05 +00:00
```yaml tab="Docker & Swarm"
2019-03-29 11:34:05 +00:00
# As a Docker Label
whoami:
2019-06-24 04:04:03 +00:00
# A container that exposes an API to show its IP address
image: traefik/whoami
2019-03-29 11:34:05 +00:00
labels:
2019-06-24 04:04:03 +00:00
# Create a middleware named `foo-add-prefix`
2019-04-01 15:56:04 +00:00
- "traefik.http.middlewares.foo-add-prefix.addprefix.prefix=/foo"
2019-06-24 04:04:03 +00:00
# Apply the middleware named `foo-add-prefix` to the router named `router1`
2019-07-19 15:00:05 +00:00
- "traefik.http.routers.router1.middlewares=foo-add-prefix@docker"
2019-03-29 11:34:05 +00:00
```
```yaml tab="Kubernetes IngressRoute"
2019-03-29 11:34:05 +00:00
---
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-03-29 11:34:05 +00:00
kind: Middleware
metadata:
name: stripprefix
spec:
2019-07-01 09:30:05 +00:00
stripPrefix:
2019-03-29 11:34:05 +00:00
prefixes:
- /stripit
---
2023-03-20 14:38:08 +00:00
apiVersion: traefik.io/v1alpha1
2019-03-29 11:34:05 +00:00
kind: IngressRoute
metadata:
2019-05-27 08:24:04 +00:00
name: ingressroute
2019-03-29 11:34:05 +00:00
spec:
# more fields...
routes:
# more fields...
2019-04-17 07:34:04 +00:00
middlewares:
2019-09-23 15:00:06 +00:00
- name: stripprefix
2019-03-29 11:34:05 +00:00
```
2019-10-15 15:34:08 +00:00
```yaml tab="Consul Catalog"
# Create a middleware named `foo-add-prefix`
- "traefik.http.middlewares.foo-add-prefix.addprefix.prefix=/foo"
# Apply the middleware named `foo-add-prefix` to the router named `router1`
- "traefik.http.routers.router1.middlewares=foo-add-prefix@consulcatalog"
```
2019-07-22 07:58:04 +00:00
```yaml tab="File (YAML)"
# As YAML Configuration File
http:
routers:
router1:
service: myService
middlewares:
2019-09-23 15:00:06 +00:00
- "foo-add-prefix"
2019-07-22 07:58:04 +00:00
rule: "Host(`example.com`)"
middlewares:
foo-add-prefix:
addPrefix:
prefix: "/foo"
services:
service1:
loadBalancer:
servers:
2019-09-23 15:00:06 +00:00
- url: "http://127.0.0.1:80"
2019-07-22 07:58:04 +00:00
```
```toml tab="File (TOML)"
# As TOML Configuration File
[http.routers]
[http.routers.router1]
service = "myService"
middlewares = ["foo-add-prefix"]
rule = "Host(`example.com`)"
[http.middlewares]
[http.middlewares.foo-add-prefix.addPrefix]
prefix = "/foo"
[http.services]
[http.services.service1]
[http.services.service1.loadBalancer]
[[http.services.service1.loadBalancer.servers]]
url = "http://127.0.0.1:80"
```
## Available Middlewares
2021-06-11 13:30:05 +00:00
A list of HTTP middlewares can be found [here](http/overview.md).
A list of TCP middlewares can be found [here](tcp/overview.md).
2022-09-09 15:17:53 +00:00
{!traefik-for-business-applications.md!}