22

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
  • 944
  • 2
  • 9
  • 23

1 Answers1

16

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!

clxoid
  • 2,577
  • 12
  • 21
  • 1
    Thats exactly what I had to do. I didnt understand that each ingress was it's own proxy_pass. When I learned that it all fit together very simply. – Pablo Marti Cordero May 15 '19 at 15:41
  • 3
    @coolinuxoid When an ingress rule automatically creates proxy_pass directive to relevant upstream/backend service, it is http. For my service, it creates `proxy_pass http://default-my-ingress-test.myapp.com-myapp-88;` directive. But instead, i need it to be https like this: `proxy_pass https://default-my-ingress-test.myapp.com-myapp-88;`. Then only my application will work as that backend accepts https requests only. Is there any way to modify that `proxy_pass` directive for a service for this purpose? – AnjK Mar 17 '21 at 14:21
  • @AnjanaDyna check this https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#server-side-https-enforcement-through-redirect – clxoid Mar 18 '21 at 22:52