Kubernetes Nginx Reverse Proxy Return URL

5/10/2018

I'm running k8s with nginx ingress. It's pointing https://www.example.com/app at some pod with rewrite "/app" to "/", app listen 80 and serves "/":

kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
  labels:
    app: example
  name: example
  namespace: default
spec:
  rules:
  - host: www.example.com
    http:
      paths:
      - backend:
          serviceName: app
          servicePort: 80
        path: /app
      - backend:
          serviceName: app
          servicePort: 80
        path: /app/
  tls:
  - hosts:
    - www.example.com
    secretName: example-tls

If i'm going to https://www.example.com/app request passed to pod and pod responded with another URL (/content for example), and my browser shows me https://www.example.com/content - it's 404, because there is no ingress rule for "/content". AFAIK nginx can rewrite response with sub_filter. Another way is to develop app with some configurable path prefix. Also i can use base_url, but only with html. But is there simple way to pass prefix in return, so if pod return "/content" nginx rewrites it to https://www.example.com/app/content

Thank you.

-- Andrey Okhotnikov
kubernetes
nginx

1 Answer

5/11/2018

But is there simple way to pass prefix in return, so if pod return "/content" nginx rewrites it to https://www.example.com/app/content

No, there is no simple way to make it by Ingress rules.

It is possible by Nginx itself, but not implemented in Nginx-Ingress.

I highly recommend you to return the right path from the application.

-- Anton Kostenko
Source: StackOverflow