Home > Software design >  ingress controller correct path
ingress controller correct path

Time:09-14

I have this yaml file and it works fine:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: some-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: "http://localhost"
    nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS, DELETE"
    nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,X-CustomHeader,X-LANG,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Api-Key,X-Device-Id,Access-Control-Allow-Origin"
spec:
  rules:
  - http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: backend-svc
              port:
                number: 80

for example, "http://localhost/api/v1/users" gives me correct answer.

but I need to change path to "- path: /api/" and everything stop working. Following requests give an error: "http://localhost/api/v1/users" "http://localhost/api/api/v1/users".

What am I doing wrong?

CodePudding user response:

"nginx.ingress.kubernetes.io/rewrite-target: /" was excess

CodePudding user response:

If you want custorm path, you need to add

nginx.ingress.kubernetes.io/rewrite-target: /$2

and path: /api(/|$)(.*)

YAML file will look like this:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: some-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/service-upstream: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: "http://localhost"
    nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS, DELETE"
    nginx.ingress.kubernetes.io/cors-allow-headers: "DNT,X-CustomHeader,X-LANG,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Api-Key,X-Device-Id,Access-Control-Allow-Origin"
spec:
  rules:
  - http:
      paths:
        - path: /api(/|$)(.*) 
          pathType: Prefix
          backend:
            service:
              name: backend-svc
              port:
                number: 80

Then, you can access http://localhost/api/api/v1/users

  • Related