Minikube with nginx ingress path rewrite issue

6/24/2019

I'm using minikube with nginx ingress.

I'm trying to use url rewriting like this

Here is my ingress definition:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: config-reader-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  backend:
    serviceName: default-http-backend
    servicePort: 80
  rules:
  - host: microservices.info
    http:
      paths:
      - path: /reload(/|$)(.*)
        backend:
          serviceName: spring-cloud-reload
          servicePort: 8080
      - path: /upload(/|$)(.*)
        backend:
          serviceName: spring-cloud-upload
          servicePort: 8080

The following urls rewrites like this:

  • /reload to /

  • /reload/xyx to /xyz

which is fine. But

  • /reload/x/y rewrites to /x// instead of /x/y

also

  • /reload/x/y/ rewrites to /x/y// instead of /x/y/

minikube version: v0.35.0

How can be fixed the multiple path elements case?

UPDATE

-------------------------------------------------------------------------------
NGINX Ingress controller
  Release:    0.21.0
  Build:      git-b65b85cd9
  Repository: https://github.com/aledbf/ingress-nginx
-------------------------------------------------------------------------------
-- Eugen Covaci
kubernetes
kubernetes-ingress
minikube
nginx-ingress

1 Answer

6/25/2019

Prior nginx-ingress v0.22.0 there was a known issue with trailing slashes.

To detect which version of the ingress controller is running, exec into the pod and run nginx-ingress-controller version command.

kubectl exec -it $POD_NAME -- /nginx-ingress-controller --version

-------------------------------------------------------------------------------
NGINX Ingress controller
  Release:    0.24.1
  Build:      git-ce418168f
  Repository: https://github.com/kubernetes/ingress-nginx
-------------------------------------------------------------------------------

Rewrite target annotation is very sensitive for trailing slashes. If it's not present, the request will not be rewritten.

Thus, you should always supply url as /reload/x/y/

Another problem with duplicated trailing slashes (fixed in v 0.22.0 and higher).

In order to understand, what's is going on there, you can exec to ingress-controller pod, find /etc/nginx/nginx.conf and search for

set $location_path  "/reload(/|${literal_dollar})(.*)";

or

rewrite "(?i)/reload(/|$)(.*)" /$2 break;

blocks

To fix it, either update ingress-controller version to the latest or you can also use configuration-snippet annotation:

nginx.ingress.kubernetes.io/configuration-snippet: |
    rewrite (?i)/reload/x/y/ /x/y break;
-- A_Suh
Source: StackOverflow