Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 726
Pablo Marti Cordero Avatar asked May 01 '19 18:05

Pablo Marti Cordero


People also ask

What is nginx ingress Kubernetes IO proxy connect timeout?

proxy-connect-timeout : this defines the timeout for establishing a connection with a proxied server. The default value is 60 seconds, and the timeout typically cannot exceed 75 seconds. Check here for more information. proxy-send-timeout : this will set a timeout for transmitting a request to the proxied server.

Is Kubernetes Ingress a reverse proxy?

An ingress controller acts as a reverse proxy and load balancer.

Which protocol does nginx ingress controller handle?

NGINX Ingress resources support additional protocols (TCP, UDP, and TLS Passthrough) – You can now deliver complex, non-HTTP-based services from Kubernetes using custom resources, in a simple and intuitive manner.


1 Answers

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!

like image 84
coolinuxoid Avatar answered Oct 23 '22 14:10

coolinuxoid