Modify the Requests and the Responses
Traefik Hub API Gateway allows you to modify the requests and the responses according to your needs. It is possible to rewrite the path (adding or stripping the prefix or replacing the values), to compress the response, or to manipulate the headers.
Add Prefix
Adds the prefix /foo
to every request:
- Middleware AddPrefix
- IngressRoute
- Service & Deployment
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: add-foo
namespace: apps
spec:
addPrefix:
prefix: /foo
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-prefix
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/addprefix`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: add-foo
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Compress
Compress the response according to the Accept-Encoding
header value:
- Middleware Compress
- IngressRoute
- Service & Deployment
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: compress
namespace: apps
spec:
compress: {}
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-compress
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/compress`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: compress
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Manipulate Headers
Adding Headers to the Request and the Response
The following example adds the X-Script-Name
header to the proxied request to the upstream and the X-Custom-Response-Header
header to the response from the upstream.
- Middleware Headers
- IngressRoute
- Service & Deployment
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: middleware-header
namespace: apps
spec:
headers:
customRequestHeaders:
X-Script-Name: "test"
customResponseHeaders:
X-Custom-Response-Header: "value"
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-headers
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/headers`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: middleware-header
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Adding and Removing Headers
In the following example, requests are proxied to the upstream with an extra X-Script-Name
header while their X-Custom-Request-Header
header gets stripped,
and upstream responses are stripped of their X-Custom-Response-Header
header.
- Middleware Headers
- IngressRoute
- Service & Deployment
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: middleware-header
spec:
headers:
customRequestHeaders:
X-Script-Name: "test" # Adds
X-Custom-Request-Header: "" # Empty value removes the header
customResponseHeaders:
X-Custom-Response-Header: "" # Empty value removes the header
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-headers
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/headers`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: middleware-header
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Replace Path
Replace the request path with /foo
:
- Middleware ReplacePath
- IngressRoute
- Service & Deployment
---
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: middleware-replacepath
spec:
replacePath:
path: /foo
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-headers
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/replacepath`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: middleware-replacepath
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Strip Prefix
It exists two middlewares to strip the prefix:
- StripPrefix: Strip prefixes that are equals to the value given in parameter.
- StripPrefixRegex: Strip prefixes that match the regular expression given in parameter.
In the example below, the middleware strips the prefix /foo/$value
with $value
being any alphabetic value:
- Middleware StripPrefixRegex
- IngressRoute
- Service & Deployment
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: middleware-stripprefixregex
spec:
stripPrefixRegex:
regex:
- "/foo/[a-z]+"
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: applications-apigateway-headers
namespace: apps
spec:
entryPoints:
- websecure
routes:
- match: Path(`/strip`)
kind: Rule
services:
- name: whoami
port: 80
middlewares:
- name: middleware-stripprefixregex
kind: Deployment
apiVersion: apps/v1
metadata:
name: whoami
namespace: apps
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: traefik/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami
namespace: apps
labels:
app: whoami
spec:
type: ClusterIP
ports:
- port: 80
name: whoami
selector:
app: whoami
Related Content
- See the AddPrefix middleware options in the dedicated section.
- See the Compress middleware options in the dedicated section.
- See the Headers middleware options in the dedicated section.
- See how to secure your APIs using headers.
- See the ReplacePath middleware options in the dedicated section.
- See the ReplacePathRegex middleware options in the dedicated section.
- See the StripPrefix middleware options in the dedicated section.
- See the StripPrefixRegex middleware options in the dedicated section.