Nginx ingress routing issue

39 views Asked by At

I have issue with url paths.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: backend
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /v1/$1
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  tls:
  - hosts:
    - backend.example.com
    secretName: backend-example-com
  rules:
  - host: backend.example.com
    http:
      paths:
      - path: /v9/anonymous/set-data/?(.*)
        backend:
          serviceName: backend2
          servicePort: 80
      - path: /v9/?(.*)
        backend:
          serviceName: backend1
          servicePort: 80

In service I'm getting

/v1/

but I should get

/v1/anonymous/set-data/foo/bar/

I need to rewrite all versions in path to /v1/ And some request should go to backend1, but some specific paths to backend2

1

There are 1 answers

0
ivanovUA On

Found solution

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: backend
  annotations:
#    nginx.ingress.kubernetes.io/rewrite-target: /v1/$1
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite ^/v9/?(.*)$ /v1/$1 break;
spec:
  tls:
  - hosts:
    - backend.example.com
    secretName: backend-example-com
  rules:
  - host: backend.example.com
    http:
      paths:
      - path: /v9/anonymous/set-data/?(.*)
        backend:
          serviceName: backend2
          servicePort: 80
      - path: /v9/?(.*)
        backend:
          serviceName: backend1
          servicePort: 80

Everything rewrites to /v1/ and working as should