Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(/|$)(.*)
like image 870
Charlie Avatar asked Oct 24 '25 10:10

Charlie


1 Answers

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(/|$)(.*)
like image 163
kitt Avatar answered Oct 26 '25 23:10

kitt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!