Kubernetes Ingress redirect to a directory

5/21/2019

using ingress example in kubernetes. I've been trying to browse but couldn't understand how to rewrite ingress to take a file which needs from the website with the ingress object in kubernetes from a folder

Here is the example:

 apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /$1
      name: tomcat-ingress
      namespace: default 
    spec:
      rules:
      - host: rewrite.bar.com
        http:
          paths:
          - backend:
              serviceName: tomcat-deployment-service
              servicePort: 8080
            path: /tomcat/?(.*)
          - backend:
              serviceName: nginx-deployment-service
              servicePort: 80
            path: /nginx/?(.*)

AND here how is done in our local nginx server:

  location ~* /company/logo/[0-9]*/.*\.(jpg|jpeg|gif|png)$ {
                root /opt/comapny/docs-branch/;
                rewrite /company/logo/([0-9]*)/(.*) /$1/$2 break;

I'm trying to figure out how when accessing this endpoint to redirect to a folder so it can take its file when needed. Also shall it try to access them from the nginx controller pod or the pod that has the service with the ingress

-- Danny
kubernetes
kubernetes-ingress
nginx-ingress

1 Answer

5/22/2019

It's possible to add custom config snippets to an Ingress via annotations:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/server-snippet: |
      location ~* /company/logo/[0-9]*/.*\.(jpg|jpeg|gif|png)$ {
          rewrite /company/logo/([0-9]*)/(.*) /$1/$2 break;
      }

You can easily break things here, so to view the eventual config file in the ingress-controller pod:

kubectl get pods
kubectl exec mingress-nginx-ingress-controller-8f57f66d-mm9j7 cat /etc/nginx/nginx.conf | less
-- Matt
Source: StackOverflow