How does Traefik automatically remove the prefix for backward forwarding?

This article was last updated on: February 7, 2024 pm

preface

Traefik is a modern HTTP reverse proxy and load balancer that makes it easy to deploy microservices.

Traefik works with multiple existing infrastructure components (Docker, Swarm patterns, Kubernetes, Marathon, Consul, Etcd, Rancher, Amazon ECS…). Integrate and configure yourself automatically and dynamically.

Series:

In practice, there is often such a demand, the URL entered by the user ishttps://e-whisper.com/alert-manager/#/alerts, but forwarding to the backend to becomehttp://alertmanager:9093/#/alerts, How to implement based on Traefik on K8S?

The answer is: use StripPrefixRegex MiddleWare.

Actual combat

Create a MiddleWare directly as follows:

1
2
3
4
5
6
7
8
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-prefix-1
spec:
stripPrefixRegex:
regex:
- /[^/]+/

The function implemented above is to remove one layer/<xxx>/ Prefix.

Remove the two layers of prefix

Removing the two layers of prefix is also simple:

1
2
3
4
5
6
7
8
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-prefix-2
spec:
stripPrefixRegex:
regex:
- /[^/]+/[^/]+/

IngressRoute uses

Use it directly like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: alertmanager
spec:
routes:
- kind: Rule
match: Host(`e-whisper.com`) && PathPrefix(`/alertmanager/`)
middlewares:
- name: strip-prefix-1
services:
- name: alertmanager
port: 9093

🎉🎉🎉 Finish!

EOF


How does Traefik automatically remove the prefix for backward forwarding?
https://e-whisper.com/posts/1051/
Author
east4ming
Posted on
July 21, 2022
Licensed under