I have nginx-ingress setup for both frontend and backend using below yaml file.
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: polls-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/service-upstream: "true"
cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
tls:
- hosts:
- example.com
secretName: polls-tls
rules:
- host: example.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 3000
- path: /graphql/(.*)
backend:
serviceName: polls
servicePort: 8000
Currently, my rewrite-target reroutes as below
example.com => / of frontend app
example.com/ => / of frontend app
example.com/graphql => / of frontend app
example.com/graphql/post => / of frontend app
example.com/graphql/ => / of backend app
example.com/graphql/post/ => /post of backend app
But I want the backend app to be used for both graphql/post and graphql/post/ as below.
example.com => / of frontend app
example.com/ => / of frontend app
example.com/hello => /hello of frontend app
example.com/test => /test of frontend app
example.com/graphql => / of backend app
example.com/graphql/ => / of backend app
example.com/grpahql/post => /post of backend app
example.com/graphql/post/ => /post of backend app
Could anyone tell me how to achieve it?
You need to change the regex for graphql path like the following:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: polls-ingress
annotations:
kubernetes.io/ingress.class: "nginx"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/service-upstream: "true"
cert-manager.io/cluster-issuer: "letsencrypt-prod"
spec:
tls:
- hosts:
- example.com
secretName: polls-tls
rules:
- host: example.com
http:
paths:
- path: /(.*)
backend:
serviceName: frontend
servicePort: 3000
- path: /graphql/?(.*) # <- HERE add "?"
backend:
serviceName: polls
servicePort: 8000
From docs on regex:
The question mark indicates zero or one occurrences of the preceding element. For example, colou?r matches both "color" and "colour".