k8s nginx ingress rewrite-target annotation not working as expected

12/13/2017

Running in Minikube, I have the following ingress, with the necessary backend:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
        - path: /config/
          backend:
            serviceName: api-gateway
            servicePort: web

The intention is to serve the gateway's web UI, which expects the path to be /, otherwise it assumes the it is a logical path and attempts to forward the request to the correct microservice.

The rewrite of the path is not working as I expect it. It doesn't replace /config/ with /. Am I misunderstanding the example?

In the logs of the nginx controller I see:

192.168.99.1 - [192.168.99.1] - - [13/Dec/2017:08:54:11 +0000] "GET /config HTTP/1.1" 301 185 "-" "curl/7.55.1" 78 0.000 [-] - - - -
192.168.99.1 - [192.168.99.1] - - [13/Dec/2017:08:54:11 +0000] "GET /config/ HTTP/1.1" 404 154 "-" "curl/7.55.1" 79 0.010 [dev-api-gateway-web] 172.17.0.10:8080 143 0.010 404

That's telling me that a request for /config got rewritten to /config/ and resulted in a 404.

How do I rewrite /config to /?

-- Niel de Wet
kubernetes
mod-rewrite
nginx

2 Answers

12/14/2017

You are misunderstanding the example. With your configuration requests to the rewrite target / will be rewritten to /config/, not the other way around. So you need to switch paths: use /config as rewrite target, and set the / path in the rule.

So the rewrite target is always where the external requests go to, the rule path where the requests to the service/pod go to.

-- slintes
Source: StackOverflow

7/18/2019
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
        - path: /config/(.+)
          backend:
            serviceName: api-gateway
            servicePort: web
-- Alexey Dmitriev
Source: StackOverflow