Rewrite path for nginx Ingress

7/4/2018

I need to point Ingress to images so that my Pods gets the URL, in full. I have the below config:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: solar-demo
  annotations:
    nginx.org/server-snippet: "proxy_ssl_verify off;"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: shmukler.example.com
    http:
      paths:
      - path: /city/*
        backend:
          serviceName: solar-demo
          servicePort: 3000
      - path: /solar/*
        backend:
          serviceName: solar-demo
          servicePort: 3001

If I keep the line: nginx.ingress.kubernetes.io/rewrite-target: /, my services inside the Pods get the rewritten paths, so /city/dublin becomes /dublin.

If I comment out the line nginx.ingress.kubernetes.io/rewrite-target: /, I just get 503 errors on the client side, and nothing in the logs. With rewrite, my services give me 404 because there is no route /dublin.

What am I doing wrong? How could I just pass the path on and have the Pods respond?

$ kubectl describe svc solar-demo
Name:              solar-demo
Namespace:         default
Labels:            <none>
Annotations:       kubectl.kubernetes.io/last-applied-configuration= 
{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"solar-demo","namespace":"default"},"spec":{"ports":[{"name":"city","port":3000...
Selector:          app=testapp
Type:              ClusterIP
IP:                10.107.221.76
Port:              city  3000/TCP
TargetPort:        3000/TCP
Endpoints:         172.17.0.3:3000,172.17.0.8:3000
Port:              solar  3001/TCP
TargetPort:        3001/TCP
Endpoints:         172.17.0.3:3001,172.17.0.8:3001
Session Affinity:  None
Events:            <none>

Suggestions?

-- Moshe Shmukler
kubernetes
kubernetes-ingress
minikube
nginx

1 Answer

7/5/2018

Here should be a working config:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: solar-demo
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: shmukler.example.com
    http:
      paths:
      - path: /city
        backend:
          serviceName: solar-demo
          servicePort: 3000
      - path: /solar
        backend:
          serviceName: solar-demo
          servicePort: 3001

What changed:

  • Removed * from paths
  • Specified ingress in the annotation
  • Removed re-write annotation

The path on an ingress (when using an nginx ingress) is like specifying the nginx location block. nginx does not use the * character in location blocks.

-- AWippler
Source: StackOverflow