What is the Best way to Setup Proxy Pass in an Nginx Ingress object for Kubernetes

5/1/2019

Currently I am trying to Migrate a site that was living on an Apache Load balanced Server to my k8s cluster. However the application was set up strangely with a proxypass and proxyreversepass like so:

ProxyPass /something http://example.com/something
ProxyPassReverse /something http://example.com/something

And I would like to mimic this in an Nginx Ingress

First I tried using the rewrite-target annotation however that does not keep the Location header which is necessary to get the application running again.

Then I tried to get the proxy-redirect-to/from annotation in place inside a specific location block like so:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gpg-app-ingress
  annotations:
    nginx.ingress.kubernetes.io/proxy-redirect-from: http://originalapp.com/something
    nginx.ingress.kubernetes.io/proxy-redirect-to: http://example.com/something
spec:
  rules:
  - host: example.com
    http:
      paths:
        - path: /something
          backend:
            serviceName: example-com
            servicePort: 80

I would like to be able to instead use a custom proxy_pass variable but it doesn't seem like its possible.

What would be the best way to mimic this proxy pass?

-- Pablo Marti Cordero
kubernetes
kubernetes-ingress
nginx
nginx-ingress
proxy

1 Answer

5/15/2019

Firstly you can use custom configuration for your nginx ingress controller, documentation can be found here

Also, if you just want to use nginx ingress controller as a reverse proxy, each ingress rule already creates proxy_pass directive to relevant upstream/backend service.

And if paths are same with your rule and backend service, then you don't have to specify rewrite rule, only just path for backend service. But if paths are different, then take consider using nginx.ingress.kubernetes.io/rewrite-target annotation, otherwise you will get 404 backend error

So to redirect request from which is coming to frontend http://example.com/something to backend example-com/something, your ingress rule should be similar to below

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: gpg-app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
   #nginx.ingress.kubernetes.io/rewrite-target: /different-path
spec:
  rules:
  - host: example.com
    http:
      paths:
        - path: /something
          backend:
            serviceName: example-com
            servicePort: 80

For more explanation about annotations, check Nginx Ingress Annotations

Also, consider checking logs of nginx-ingress-controller pod via if something wrong

kubectl logs nginx-ingress-controller-xxxxx

Hope it helps!

-- coolinuxoid
Source: StackOverflow