Kubernetes Nginx Ingress pod subpath

10/16/2021

I have a pod that exposes an HTTP service.
This pod has some HTTP endpoints which are available under /accounts.
My goal is to access this sub-path via accounts.example.com.

For example if the url accounts.example.com/test is requested, the nginx ingress should route the request to /accounts/test on my pod.

In the nginx ingress documentation I cannot find a use-case with examples like this.

-- Markus
kubernetes
nginx

1 Answer

10/16/2021

You should use rewrite to accomplish your request.

https://kubernetes.github.io/ingress-nginx/examples/rewrite/

  • Here is an example:
    • Focus on this line: path: /something(/|$)(.*)
apiVersion: networking.k8s.io/v1
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(/|$)(.*)

  • Look on the different annotations for the re-write:

enter image description here enter image description here

-- CodeWizard
Source: StackOverflow