An open API service indexing awesome lists of open source software.

https://github.com/kodeart/traefik-plugin-setheaderfrom

A traefik middleware plugin that creates or overwrites a request header using a value from another header.
https://github.com/kodeart/traefik-plugin-setheaderfrom

traefik traefik-plugin

Last synced: 13 days ago
JSON representation

A traefik middleware plugin that creates or overwrites a request header using a value from another header.

Awesome Lists containing this project

README

          

# Set Header From Header

A Traefik **middleware** plugin that **creates (or overwrites)
a request header** using a value extracted from another request
header via a regular expression capture group.

Typical use case: derive `X-Subdomain` from `Host`
(e.g., `foo.example.com` → `X-Subdomain: foo`).

---

## What it does

Given:

- `regex`: a regular expression applied to the source header value
- `source`: the **source** request header name (e.g., `Host`)
- `destination`: the **destination** request header name to set (e.g., `X-Subdomain`)
- `prefix`: optional string to prepend to the captured value
- `suffix`: optional string to append to the captured value

If the source header exists and `regex` matches with at least one capture group, the middleware sets:

- `create = prefix + + suffix`

If there is **no match**, the destination header is **not created**.

---

## Configuration

### Fields

| Field | Type | Required | Description |
|---------------|--------|----------|------------------------------------------------------------------------------------------------------|
| `regex` | string | yes | Regex used to match the source header value. Must include a capture group (the plugin uses group 1). |
| `source` | string | yes | Source request header to read from. |
| `destination` | string | yes | Destination request header name to set. |
| `prefix` | string | no | Prepended to the captured value before setting the destination header. |
| `suffix` | string | no | Appended to the captured value after setting the destination header. |

### Notes

- Only the **first** capture group is used (group 1).
- If the regex cannot be compiled, the plugin initialization fails and Traefik will refuse to load the middleware.
- Header names are treated as HTTP header keys (e.g., `X-Foo`, `Host`).

---
## Example Configuration

For each plugin, the Traefik static configuration must define the module name.

The following declaration defines a plugin:

### Static configuration
```yaml
experimental:
plugins:
setheaderfrom:
moduleName: github.com/kodeart/traefik-plugin-setheaderfrom
version: v1.0.0-beta.2
```
> Use the latest **version** (v1.0.0-beta.1 is an example).

### Dynamic configuration
```yaml
http:
middlewares:
set-header-from:
plugin:
setheaderfrom:
regex: '^(.+)\.example\.com$'
source: Host
destination: X-Subdomain
prefix: '' # optional
suffix: '' # optional
```

Attach it to a router/service as you normally would:
```yaml
http:
routers:
app:
rule: Host(`test.example.com`)
service: app-svc
middlewares:
- set-header-from

services:
app-svc:
loadBalancer:
servers:
- url: http://127.0.0.1
```
---

### Docker labels
```yaml
services:
whoami:
image: traefik/whoami
labels:
- traefik.enable=true
- traefik.http.routers.whoami.rule=Host(`test.example.com`)
- traefik.http.routers.whoami.middlewares=set-header-from@file
```
(Using `@file` assumes you configured the middleware
via the file provider. You can also configure plugins
via other providers depending on your setup.)