I am learning Kubernetes and experimenting a bit I have the following ingress file:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: testhost-ingress
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: testhost-ingress.info
http:
paths:
- path: /api/keycloak(/|$)(.*)
pathType: Prefix
backend:
service:
name: keycloak
port:
number: 8080
- path: /api/departments(/|$)(.*)
pathType: Prefix
backend:
service:
name: department
port:
number: 8080
- path: /api/employees(/|$)(.*)
pathType: Prefix
backend:
service:
name: employee
port:
number: 8080
- path: /v2(/|$)(.*)
pathType: Prefix
backend:
service:
name: web2
port:
number: 8080
If I use the file above I can access department, services and web2 services as I desired meaning if I call http://testhost-ingress.info/api/employees/add/John it will correctly call the employee service with http://employee:8080/api/employees/add/John
While this is desired for the 3 services for Keycloak I don't want the api/keycloak part being repeated http://testhost-ingress.info/api/keycloak/auth should call http://keycloak:8080/auth If I add nginx.ingress.kubernetes.io/rewrite-target: /$2 it will work for keycloak but now the other services won t work as expected.
How can I have different rewrite-target rules for different services?
I found the answer, one can uses nginx.ingress.kubernetes.io/server-snippet to specify some specific rules for some path. In my case for keycloak :
nginx.ingress.kubernetes.io/server-snippet: |
location ~* ^/api/keycloak(/|$)(.*) {
rewrite ^/api/keycloak(/|$)(.*) /$2 break;
proxy_pass http://keycloak.default:8080;
}