How to add slash at the end of path automatically based on Traefik?
This article was last updated on: July 24, 2024 am
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, a very common need, the URL entered by the user is e-whisper.com/alert-manager, if nothing is done will return 404, need to automatically add slashes into e-whisper.com/alert-manager/, how to implement based on Traefik on K8S?
The answer is: redirectRegex MiddleWare + regularity.
Actual combat
Create MiddleWare directly as follows:
1 |
|
📝 The instructions are as follows:
The regular match is:
^(https?
: Content that begins with https or http;?
Indicates that the previous character is matched 0 or 1 times[^/]+/
: matches the URL first/
Previous content[-a-z0-9_]+
: Matches the first one/
The content that follows is often alphanumeric and hyphenated and underlined
ultimately^(https?://[^/]+/[-a-z0-9_]+)$
Examples of what the group matches are:https://e-whisper.com/monitor-alertmanager, and replace it with:${1}/
, i.e. the matching group is followed by it /
, examples such as:https://e-whisper.com/monitor-alertmanager
🐾 note:
The above MiddleWare may not be able to adapt to all situations, readers can grasp its main points and make appropriate adjustments according to their needs.
IngressRoute uses
Use it directly like this:
1 |
|
🎉🎉🎉 Finish!
EOF