This plugin allows to change, on the fly, the header's value of a request.
pilot:
token: [REDACTED]
experimental:
devPlugin:
goPath: /home/tm/go
moduleName: github.com/adyanth/header-transform
entryPoints:
http:
address: ":8000"
forwardedHeaders:
insecure: true
api:
dashboard: true
insecure: true
providers:
file:
filename: rules-header-transform.yaml
$ docker run -d --network host containous/whoami -port 5000
# traefik --config-file traefik.yml
To choose a Rule you have to fill the Type
field with one of the following:
- 'Rename' : to rename a header
- 'Set' : to Set a header
- 'Del' : to Delete a header
Each Rule can be named with the Name
field
Each Rule can be set to run only if the previous hop IP address belongs to a trusted range of IPs. Set this using the optional TrustedCIDR
.
# Example
- Rule:
Name: 'Test'
...
TrustedCIDR:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
A Rule Rename needs two arguments and optionally the third.
Header
, the regex of the header you want to replaceValue
, the new headerHeaderPrefix
, the prefix to denote the new Header name is to be taken from another header value
# Example Rename
- Rule:
Name: 'Header rename'
Header: 'Cache-Control'
Value: 'NewHeader'
Type: 'Rename'
# Old header:
Cache-Control: gzip, deflate
# New header:
NewHeader: gzip, deflate
- Rule:
Name: 'Header Renaming'
Header: 'X-Traefik-*'
Value: 'X-Traefik-merged'
Type: 'Rename'
# Old header:
X-Traefik-uuid: 0
X-Traefik-date: mer. 21 oct. 2020 11:57:39 CEST
# New header:
X-Traefik-merged: 0 # A value from old headers
A Set rule will either create or replace the header and value (if it already exists), appending multiple values with the separator if specified.
A Rule Set needs the first two arguments, and optionally the next three.
Value
can be skipped if specifying Values
.
Header
, the header you want to createValue
, the value of the new headerValues
, a list of values to addSep
, the separator you want to useHeaderPrefix
, the prefix to denote the Value is to be taken from another header
# Example Set
- Rule:
Name: 'Set Cache-Control'
Header: 'Cache-Control'
Value: 'Foo'
Type: 'Set'
# New header:
Cache-Control: Foo
# Example Usage
- Rule:
Name: 'Header set'
Header: 'X-Forwarded-For'
Value: '^CF-Connecting-IP'
HeaderPrefix: "^"
Type: 'Set'
# Old header:
CF-Connecting-IP: 1.1.1.1
# New headers:
CF-Connecting-IP: 1.1.1.1
X-Forwarded-For: 1.1.1.1
# Example Usage
- Rule:
Name: 'Header XFF'
Header: 'X-Forwarded-For'
Value: '^CF-Connecting-IP'
Values:
- '^X-Forwarded-For'
- '192.168.0.1'
Sep: ', '
HeaderPrefix: "^"
Type: 'Set'
# Old header:
CF-Connecting-IP: 1.1.1.1
X-Forwarded-For: 10.0.0.1, 10.10.10.1
# New headers:
CF-Connecting-IP: 1.1.1.1
X-Forwarded-For: 1.1.1.1, 10.0.0.1, 10.10.10.1, 192.168.0.1
# Example Join
- Rule:
Name: 'Header join'
Header: 'Cache-Control'
Sep: ','
HeaderPrefix: "^"
Values:
- '^Cache-Control'
- 'Foo'
- 'Bar'
Type: 'Set'
# Old header:
Cache-Control: gzip, deflate
# Joined header:
Cache-Control: gzip, deflate,Foo,Bar
A Rule Delete needs only one argument
Header
, the header you want to delete
# Example Del
- Rule:
Name: 'Delete Cache-Control'
Header: 'Cache-Control'
Type: 'Del'
The rules will be evaluated in the order of definition
#Example
- Rule:
Name: 'Header addition'
Header: 'X-Custom-2'
Value: 'True'
Type: 'Set'
- Rule:
Name: 'Header deletion'
Header: 'X-Custom-2'
Type: 'Del'
- Rule:
Name: 'Header join'
Header: 'X-Custom-2'
Value: 'False'
Type: 'Set'
This will firstly set the header X-Custom-2
to 'True', then delete it and finally set it again but with False