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

25,395

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!

Share:
25,395

Related videos on Youtube

Pablo Marti Cordero
Author by

Pablo Marti Cordero

Updated on July 09, 2022

Comments

  • Pablo Marti Cordero
    Pablo Marti Cordero almost 2 years

    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
    Pablo Marti Cordero almost 5 years
    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.
  • AnjanaAK
    AnjanaAK about 3 years
    @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?
  • clxoid
    clxoid about 3 years