简体   繁体   中英

Another nginx ingress rewrite-target problem

I have one service and a single ingress resource with kubenetes nginx ingress controller. I want the /student path of my url to go to the root of the application and match any other url segments which follow the student .

For example: http://example.com/student/ver should match the /ver route of my application.

However, my ingress always hit the application with the /student url path prefixing the other url segments. If I call http://example.com/student/ver , my application is hit with the same url ( student/ver ).

My ingress:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
  name: ingress-resource
spec:
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          serviceName: lesson-streaming
          servicePort: 80
        path: /student

I spent days with this and was not successful once.


Edit:

The ingress is changed to the following - not my requests say http 404

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ingress.kubernetes.io/rewrite-target: /$2
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
  name: ingress-resource
  namespace: default
spec:
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          serviceName: lesson-streaming
          servicePort: 80
        path: /student(/|$)(.*)

You can follow the link to use the rewrite-target annotation correctly and keep the right key nginx.ingress.kubernetes.io/rewrite-target .

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)

I have tried many times to execute properly path based routing in nginx ingress controller but I have found the main reason behind the path based routing not working.

replace the (nginx.ingress.kubernetes.io/rewrite-target: /) to (nginx.org/rewrites: "serviceName=servicename rewrite=/")

must add ingressClassName in spec

in my case I have kubernetes 1.23.0 & nginx/1.21.6

I hope this will resolve the above problem. Best of luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM