skip to Main Content

I am struggling with making a rewrite/redirect rule on nginx-ingress on Kubernetes.

According to the doc https://kubernetes.github.io/ingress-nginx/examples/rewrite/ it says it is possible with the annotation "nginx.ingress.kubernetes.io/rewrite-target", but unable to implement on my ingress. Maybe I am doing it wrong.

An example, portion of nginx-ingress yaml file.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    example.com/deployment-name: frontend-page
    example.com/ingress-hostnames: www.example.com
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: www.example.com
    http:
      paths:
      - backend:
          serviceName: frontend-page
          servicePort: 8080
        path: /front(/|$)(.*)

Tried another way around as well but no luck:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    example.com/deployment-name: frontend-page
    example.com/ingress-hostnames: www.example.com
    nginx.ingress.kubernetes.io/rewrite-target: /shop
spec:
  rules:
  - host: www.example.com
    http:
      paths:
      - backend:
          serviceName: frontend-page
          servicePort: 8080
        path: /

I want to redirect any traffic to the page www.example.com/front/checkdomain to www.example.com/checkdomain. I want to remove that "front" path comes in between.

2

Answers


  1. Chosen as BEST ANSWER

    The solution was

    Changing from:

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

    To:

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

    So the yaml looks like:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      annotations:
        kubernetes.io/ingress.class: nginx
        example.com/deployment-name: frontend-page
        example.com/ingress-hostnames: www.example.com
        nginx.ingress.kubernetes.io/rewrite-target: /$2/
    spec:
      rules:
      - host: www.example.com
        http:
          paths:
          - backend:
              serviceName: frontend-page
              servicePort: 8080
            path: /front(/|$)(.*)
    

  2. try this with an updated API version, hope your ingress supports networking.k8s.io/v1beta1

    apiVersion: networking.k8s.io/v1beta1
    kind: Ingress
    metadata:
      annotations:
        kubernetes.io/ingress.class: nginx
        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(/|$)(.*)
    

    Example : https://kubernetes.github.io/ingress-nginx/examples/rewrite/#examples

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search