Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Nginx Ingress removing part of URL

I'm deploying a simple app in Kubernetes (on AKS) which is sat behind an Ingress using Nginx, deployed using the Nginx helm chart. I have a problem that for some reason Nginx doesn't seem to be passing on the full URL to the backend service.

For example, my Ingress is setup with the URL of http://app.client.com and a path of /app1g going http://app.client.com/app1 works fine. However if I try to go to http://app.client.com/app1/service1 I just end up at http://app.client.com/app1, it seems to be stripping everything after the path.

My Ingress looks like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /
  creationTimestamp: "2019-04-03T12:44:22Z"
  generation: 1
  labels:
    chart: app-1.1
    component: app
    hostName: app.client.com
    release: app
  name: app-ingress
  namespace: default
  resourceVersion: "1789269"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/app-ingress
  uid: 34bb1a1d-560e-11e9-bd46-9a03420914b9
spec:
  rules:
  - host: app.client.com
    http:
      paths:
      - backend:
          serviceName: app-service
          servicePort: 8080
        path: /app1
  tls:
  - hosts:
    - app.client.com
    secretName: app-prod
status:
  loadBalancer:
    ingress:
    - {}

If I port forward to the service and hit that directly it works.

like image 229
Sam Cogan Avatar asked Dec 17 '22 17:12

Sam Cogan


1 Answers

So I found the answer to this. It seems that as of Nginx v0.22.0 you are required to use capture groups to capture any substrings in the request URI. Prior to 0.22.0 using just nginx.ingress.kubernetes.io/rewrite-target: / worked for any substring. Now it does not. I needed to ammend my ingress to use this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
  creationTimestamp: "2019-04-03T12:44:22Z"
  generation: 1
  labels:
    chart: app-1.1
    component: app
    hostName: app.client.com
    release: app
  name: app-ingress
  namespace: default
  resourceVersion: "1789269"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/app-ingress
  uid: 34bb1a1d-560e-11e9-bd46-9a03420914b9
spec:
  rules:
  - host: app.client.com
    http:
      paths:
      - backend:
          serviceName: app-service
          servicePort: 8080
        path: /app1/?(.*)
  tls:
  - hosts:
    - app.client.com
    secretName: app-prod
status:
  loadBalancer:
    ingress:
    - {}
like image 159
Sam Cogan Avatar answered Jan 04 '23 01:01

Sam Cogan