1

The "nginx.ingress.kubernetes.io/rewrite-target" annotation on my ingress resource does not seem to be doing anything. Everything works just fine when I change path: /helloworld to path: /. I have tried putting the annotation's value in double quotes and changing the order of annotations to no effect. What am I missing? It seems like this should be fairly straightforward.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: helloworld-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: <NGINX_CONTROLLER_EXTERNAL_IP>.xip.io
    http:
      paths:
      - backend:
          serviceName: helloworld-svc
          servicePort: 80
        path: /helloworld
  • So do you reach the back-end service, but the path is still `/helloworld`? – Christian Fosli Jan 03 '21 at 21:18
  • 1
    I have mostly seen the rewrite-target used with regex. I.e. `nginx.ingress.kubernetes.io/rewrite-target: /$2` and `path: /helloworld(/|$)(.*)`. This way you would rewrite such that `/helloworld` is removed, but the rest of the of the path is kept. However, if you always want to rewrite to root path, I would think it should work like you've done.. :thinking: – Christian Fosli Jan 03 '21 at 21:22
  • @ChristianFosli You are right. It should work and it would in most cases. It turns out the problem was related to the service the path was pointing too. It's a Nginx server serving a React.js app. I added an answer with a link to a more detailed post on the issue. Thank you anyway for your efforts. – 2um9YJ6haZ7wKP4m Jan 04 '21 at 10:26
  • No worries :-) Yes, since React.js works on the client side this kind of rewrite won't make any difference. – Christian Fosli Jan 04 '21 at 20:20

2 Answers2

2

I solved my problem. It was not related to the "rewrite-target" annotation itself. I was trying to point it to a Nginx server serving a React.js app. I needed to set the base URL within the React app and copy the build to the corresponding subdirectory of /usr/share/nginx/html in the Dockerfile. After taking those steps there was no need for using "rewrite-target" anymore. I posted a separate question detailing the setup with React and Nginx and answered it too.

1

You will need to use app-root if you want to rewrite in the / context. https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#rewrite

VBoi
  • 349
  • 5
  • 21
  • Thanks for your help. The problem was unrelated to the "rewrite-target" annotation. I added an answer with a link to a more detailed post in case someone runs into the same problem. – 2um9YJ6haZ7wKP4m Jan 04 '21 at 10:28