Scenario / Questions

I’m trying to rewrite the path to the service while preserving the browser url with nginx ingress on kubernetes.

What I need is this:

  • => http://service
  • => http://service/bar
  • => http://service/file.css

That is, make the /path be the root (or some different url) in the upstream server.

From what I see here, this is done using a trailing slash on the proxy_pass, but nginx ingress doesn’t seem to have this option.

Is this possible nginx ingress?

Find below all possible solutions or suggestions for the above questions..

Suggestion: 1

Is it Possible on Nginx Ingress?

  • Yes, In order to achieve it you can use rewrite-target.

  • It will create a capture group and send it to the appropriate service. Here is an Ingress example:

kind: Ingress
  name: "rewrite"
  annotations: nginx /$2
    - http:
          - path: /foo(/|$)(.*)
              serviceName: echo-svc
              servicePort: 80

In this ingress definition, any characters captured by (.*) will be assigned to the placeholder $2, which is then used as a parameter in the rewrite-target annotation.

This ingress will do the following:

  • Requests to /foo will be delivered to echo1-svc as /
  • Requests to /foo/bar will be delivered to echo1-svc as /bar
  • Requests to /foo/file.css will be delivered to echo-svc as /file.css
  • Nginx Ingress uses Path Priority:

In NGINX, regular expressions follow a first match policy. In order to enable more accurate path matching, ingress-nginx first orders the paths by descending length before writing them to the NGINX template as location blocks.


$ kubectl apply -f echo-ingress.yaml created

$ kubectl get ingress
NAME           HOSTS          ADDRESS        PORTS   AGE
echo-ingress   80      48s

$ tail -n 1 /etc/hosts

$ curl
{"path": "/bar",
...suppressed output...
  "os": {"hostname": "echo-deploy-764d5df7cf-6m5nz"}

$ curl
{"path": "/",
  "os": {"hostname": "echo-deploy-764d5df7cf-6m5nz"}

If you have any question let me know in the comments.