Traefik middleware is a component, that allows to modify http requests before passing it to the service. There are different usecases for the middleware:
- redirect
- rewrite
- security, like basic auth
- rate limiting
Let me show how to implement basic traefik middleware.
redirect
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: redirect
namespace: home
spec:
redirectRegex:
regex: "^https://piasecki\\.it/$"
replacement: "https://piasecki.it/?effect=mirror&camera=true"
permanent: false
This resource will redirect from https://piasecki.it/ to https://piasecki.it/?effect=mirror&camera=true . To use it, you need to modify ingress, by adding home-redirect@kubernetescrd to traefik.ingress.kubernetes.io/router.middlewares annotation.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod
spec.ingressClassName: traefik
traefik.ingress.kubernetes.io/router.middlewares: cert-manager-redirect-https@kubernetescrd,home-redirect@kubernetescrd
In the effect, ?effect=mirror&camera=true will be added to user request, whenever he will visit https://piasecki.it/ . It is possible to do the same thing, but transparently for the user by using rewrite.
Rewrite
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: rewrite
namespace: home
spec:
replacePathRegex:
regex: "^/$"
replacement: "/?effect=mirror&camera=true"
It should work. But it did not. What the backend received was
10.42.0.36 - - [16/Nov/2024 12:30:03] "GET /%3Feffect=mirror&camera=true HTTP/1.1" 404 -
? was encoded to %3F and it resulted with 404 error.
There are a few solutions, but they require working with backend.
- correct path parsing
- parsing custom headers
I guess I will stick with the redirect then.
Leave a Reply