Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to rewrite HOST header in k8s Ingress Controller?

Due to some legacy application that relies on Host header to function correctly, I need to have an Ingress (proxy, etc) that capable of rewrite Host header and pass that to downstream (backend). Is there any Ingress Controller that supports this functionality?

Example:

End user access our website through foo.com/a for backend a and foo.com/b for backend b. But since a and b are legacy app, it only accept:

  • a accepts connection when Host: a.foo.com
  • b accepts connection when Host: b.foo.com
like image 338
Agung Pratama Avatar asked Feb 11 '19 05:02

Agung Pratama


People also ask

What is rewrite target in ingress?

In this ingress definition, any characters captured by (. *) will be assigned to the placeholder $2 , which is then used as a parameter in the rewrite-target annotation. For example, the ingress definition above will result in the following rewrites: rewrite.bar.com/something rewrites to rewrite.bar.com/

Is it possible to have multiple ingress controllers in the cluster?

You may deploy any number of ingress controllers using ingress class within a cluster. Note the . metadata.name of your ingress class resource.

Can you have multiple ingress controllers in Kubernetes?

Starting with NGINX IC v1. 8.0, one can install multiple NGINX ingress controllers in a Kubernetes cluster. The optional NGINX Ingress Controller can be installed as an App on your cluster. NGINX IC v2.


3 Answers

This can be done using this annotation: nginx.ingress.kubernetes.io/upstream-vhost: host.example.com

like image 196
Camil Avatar answered Oct 19 '22 04:10

Camil


I'm not sure whether you can find appropriate annotation within NGINX Ingress Controller for Host header modification to match your requirement as well. However, you can consider using nginx.ingress.kubernetes.io/configuration-snippet annotation in order to append configuration snippet to the location block inside nginx.conf of the particular Nginx controller pod:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Host www.example-host.com;
  name: my-app
spec:
  rules:
  - host: my-app.example.com
    http:
      paths:
      - backend:
        path: /app
          serviceName: my-app
          servicePort: http

We set here Host header www.example-host.com for target URL my-app.example.com.

like image 41
Nick_Kh Avatar answered Oct 19 '22 05:10

Nick_Kh


I want to add my finding to this question of mine.

Although my solution is not using k8s Ingress Controller, our cluster is using Istio and Istio's VirtualService supports rewrite the uri and authority (Host header) as documented in this link: https://istio.io/docs/reference/config/istio.networking.v1alpha3/#HTTPRewrite

To know how I implement that in my case, you can take a look at this link: https://github.com/istio/istio/issues/11668

like image 23
Agung Pratama Avatar answered Oct 19 '22 03:10

Agung Pratama