0

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?

TomLott
  • 41
  • 6

2 Answers2

0

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

TomLott
  • 41
  • 6
0

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

quoc9x
  • 1,423
  • 2
  • 9
  • 26
  • http://localhost/api/v1/users and what about this one? "http://localhost/api/api/v1/users" wasn't correct – TomLott Sep 13 '22 at 20:15