'Kubernetes Ingress Controller Redirect if contains characters like "?"
I have an ingress controller working for UI container service and backend container service. my ingress configuration is as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: testapp
annotations:
rules:
- host: test1.example.com
http:
paths:
- path: /static
backend:
serviceName: ui-service
servicePort: 80
- path: /apicall
backend:
serviceName: backend-service
servicePort: 8080
Which is working fine. Now I need to forward this ingress URL if it contains ?.
For eg, if url is test1.example.com/?productid=10001 I need to add static before this ? and forward it to test1.example.com/static?productid=10001.
Is this behavior possible through below annotations?
nginx.ingress.kubernetes.io/rewrite-target: /
If yes, how to have that regex kind of url where if ? is present in the url followed by any string/characters, add static keyword before it?
Solution 1:[1]
First of all look at the YAML below to understand how works rewrite rule:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
ingressClassName: nginx
rules:
- host: rewrite.bar.com
http:
paths:
- path: /something(/|$)(.*)
pathType: Prefix
backend:
service:
name: http-svc
port:
number: 80
In this ingress definition, any characters captured by
(.*)will be assigned to the placeholder$2, which is then used as a parameter in therewrite-targetannotation.For example, the ingress definition above will result in the following rewrites:
rewrite.bar.com/somethingrewrites torewrite.bar.com/rewrite.bar.com/something/rewrites torewrite.bar.com/rewrite.bar.com/something/newrewrites torewrite.bar.com/new
In your case, you will need to follow a similar procedure. But first, I suggest you do a separate ingress for each path. This will help you keep order later and additionally you will have an independent ingress for each path. Look at this question to see why and how you should create a separate ingress.
Now I need to forward this ingress URL if it contains ?. for eg if URL is test1.example.com/?productid=10001 i need to add static before this ? and forward it to test1.example.com/static?productid=10001.
You will need to create an appropriate capture group. For the data you presented it will look like this:
- path: /(/|$)(.*)
and the annotation should be like this:
nginx.ingress.kubernetes.io/rewrite-target: /static/$2
In this case, everything that gets captured from the URL after the / character will be rewritten to the new address, which will look like /static and the address that was before. There should be no problem with the question mark.
If you need to create a regex that will act on a question mark, you will need to prefix it with a \ character, which is described here.
Also, consider that you are currently targeting root / path and want to create 2 ingresses. I also suggest creating appropriate endpoints depending on the situation, so that you can easily create 2 different ingresses responsible for directing different traffic.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harsh Manvar |
