traefik/docs/content/middlewares/stripprefixregex.md

82 lines
2.3 KiB
Markdown
Raw Normal View History

2019-06-18 10:20:04 +00:00
# StripPrefixRegex
Removing Prefixes From the Path Before Forwarding the Request (Using a Regex)
{: .subtitle }
2019-04-08 15:14:08 +00:00
Remove the matching prefixes from the URL path.
## Configuration Examples
```yaml tab="Docker"
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=/foo/[a-z0-9]+/[0-9]+/"
2019-04-08 15:14:08 +00:00
```
```yaml tab="Kubernetes"
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: test-stripprefixregex
spec:
2019-06-18 07:50:05 +00:00
stripPrefixRegex:
2019-09-02 08:52:04 +00:00
regex:
2019-09-23 15:00:06 +00:00
- "/foo/[a-z0-9]+/[0-9]+/"
2019-04-08 15:14:08 +00:00
```
```json tab="Marathon"
"labels": {
2019-09-03 18:32:03 +00:00
"traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex": "/foo/[a-z0-9]+/[0-9]+/"
}
```
2019-04-08 15:14:08 +00:00
```yaml tab="Rancher"
labels:
2019-09-23 15:00:06 +00:00
- "traefik.http.middlewares.test-stripprefixregex.stripprefixregex.regex=/foo/[a-z0-9]+/[0-9]+/"
2019-04-08 15:14:08 +00:00
```
2019-07-22 07:58:04 +00:00
```toml tab="File (TOML)"
2019-04-08 15:14:08 +00:00
[http.middlewares]
2019-07-01 09:30:05 +00:00
[http.middlewares.test-stripprefixregex.stripPrefixRegex]
2019-09-03 18:32:03 +00:00
regex = ["/foo/[a-z0-9]+/[0-9]+/"]
2019-07-22 07:58:04 +00:00
```
```yaml tab="File (YAML)"
http:
middlewares:
test-stripprefixregex:
stripPrefixRegex:
2019-09-02 08:52:04 +00:00
regex:
2019-09-23 15:00:06 +00:00
- "/foo/[a-z0-9]+/[0-9]+/"
2019-04-08 15:14:08 +00:00
```
## Configuration Options
### General
The StripPrefixRegex middleware will:
- strip the matching path prefix.
- store the matching path prefix in a `X-Forwarded-Prefix` header.
!!! tip
2019-07-01 09:30:05 +00:00
Use a `stripPrefixRegex` middleware if your backend listens on the root path (`/`) but should be routeable on a specific prefix.
2019-04-08 15:14:08 +00:00
### `regex`
The `regex` option is the regular expression to match the path prefix from the request URL.
!!! tip
Regular expressions can be tested using online tools such as [Go Playground](https://play.golang.org/p/mWU9p-wk2ru) or the [Regex101](https://regex101.com/r/58sIgx/2).
For instance, `/products` would match `/products` but also `/products/shoes` and `/products/shirts`.
Since the path is stripped prior to forwarding, your backend is expected to listen on `/`.
2019-04-08 15:14:08 +00:00
If your backend is serving assets (e.g., images or Javascript files), chances are it must return properly constructed relative URLs.
2019-04-08 15:14:08 +00:00
Continuing on the example, the backend should return `/products/shoes/image.png` (and not `/images.png` which Traefik would likely not be able to associate with the same backend).
2019-04-08 15:14:08 +00:00
The `X-Forwarded-Prefix` header can be queried to build such URLs dynamically.