nginxinc/nginx-ingress-helm-operator

Can't make nginx rewrite-target to work using nginx-ingress-controller operator

juangon opened this issue · 7 comments

Describe the bug
I have a system that needs to dynamically create nginx-controllers. Hence I am using this operator. To achieve this, I am using java-operator-sdk

To Reproduce
Steps to reproduce the behavior:

  1. Install this nginx-ingress-helm-operator
  2. Create a simple nginxingress Kubernetes resource
  3. Create an ingress with nginx.ingress.kubernetes.io/rewrite-target: /$1 or $2
  4. Nginx works if adding paths, but previous annotation for rewriting doesn't work. It seems location parts are added in nginx config files but no rewriting in those files.

Expected behavior
Rewrites should work, and some "rewrite" should be added in any of nginx controller config files.

Your environment

  • Version of the NGINX Ingress Operator - release version or a specific commit: 3.0.1
  • Version of the Ingress Controller - release version or a specific commit: 3.0.1
  • Version of Kubernetes: 1.23
  • Kubernetes platform (e.g. Mini-kube or GCP): AKS
  • Using NGINX or NGINX Plus: NGINX

Additional context
Add any other context about the problem here. Any log files you want to share.

Could you please share an example of your desired Ingress resource?
So that we can fully understand the desired configuration.

Ingress annotations are not common between all ingress controller implementations (the ingress object is standard but the annotations are not). But I want to make sure we are more helpful and get you to your desired outcome.

Sure! Here it is (I changed data, so not real names):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: custom-ingress-sample-ingress
  namespace: custom-ingress-sample-namespace
spec:
  ingressClassName: custom-nginx-sample
  rules:
  - host: example.com
    http:
      paths:
      - backend:
          service:
            name: service1
            port:
              number: 80
        path: /service1/(.+)
        pathType: Prefix
      - backend:
          service:
            name: service2
            port:
              number: 80
        path: /service2/(.+)
        pathType: Prefix
      - backend:
          service:
            name: service3
            port:
              number: 80
        path: /service3/(.+)
        pathType: Prefix

If I change some of those services to have those paths (/service1, /service2 etc) it works, but I would like to let them just have the root path, and make this change through ingress. It seems it doesn't rewrite anything.

Thanks very much!

I mean, if I remove the rewrite annotation and remove the regex strings, it works if services' root paths are the same as ingress ones.

I mean, if I remove the rewrite annotation and remove the regex strings, it works if services' root paths are the same as ingress ones.

Hi @juangon
It looks like you are using annotations that are for a different NGINX Ingress controller project.

This annotation:
annotations: nginx.ingress.kubernetes.io/rewrite-target: /$1

Works for this NGINX Ingress project:
https://github.com/kubernetes/ingress-nginx

For this github repo project, you will want to use our specific annotations for Ingress located here:
https://docs.nginx.com/nginx-ingress-controller/configuration/ingress-resources/advanced-configuration-with-annotations/

Specifically, you will want to use your rewrite annotation:

nginx.org/rewrites
We have an example as well on how to use your rewrites:

https://github.com/nginxinc/kubernetes-ingress/tree/v3.0.1/examples/ingress-resources/rewrites

Let us know if that helps and if you have any other questions.

Thanks! I read somewhere that this annotation was for- NGINX Plus, is it true?

@juangon Hello.
This annotation will work with NGINX open source Ingress controller as well as NGINX Plus Ingress controller.

It works like a charm. Thanks very much!