Is there a best practice to implement ingress rule with rewrite to root

10/25/2019

For example, I have a rule (https://kubernetes.github.io/ingress-nginx/examples/rewrite/):

apiVersion: extensions/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(/|$)(.*)

So anything that goes to rewrite.bar.com/something will go to http-svc/. And if http-svc will answer another location, e.g. /static there will be redirect to rewrite.bar.com/static and here we go - 404. I wonder is there any simple and clear solution to fix such situations without asking the developer to implement proxy_path variable or something like that?

Tried Make links in response relative to new path

-- Andrey Okhotnikov
kubernetes
nginx-ingress

1 Answer

10/26/2019

Change the rewrite annotation to $1. Note I have also changed the path regex. That should do it.

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something/(.*)
-- Rodrigo Loza
Source: StackOverflow