Matías Costa
1 min readFeb 16, 2021

--

Hi Jason,

That’s a very good question actually. As mentioned in the article, nginx.ingress.kubernetes.io/upstream-vhost controls the value for $host, that then is used in the nginx config so set the Host value:

proxy_set_header Host $host

and it works very well when all the requests go to the same backend.

If you have multiple paths to different backends, you can set the value of $host depending on the request path using the following annotation:

nginx.ingress.kubernetes.io/configuration-snippet

To simplify, let’s say we have the following entries, and that the default backend configured through nginx.ingress.kubernetes.io/upstream-vhost is hi-service1-service.<namespace>.svc.cluster.local.

annotations:
nginx.ingress.kubernetes.io/upstream-vhost: hi-service1-service.<namespace>.svc.cluster.local
nginx.ingress.kubernetes.io/service-upstream: "true"
...
spec:
rules:
- host: entry.host.test.com
http:
paths:
- backend:
serviceName: hi-service1-service
servicePort: 8080
path: /
- backend:
serviceName: hi-service2-service
servicePort: 8080
path: /api/tnt

To set Host to a different value for requests going to /api/tnt:

annotations:
nginx.ingress.kubernetes.io/configuration-snippet: set $ingress_host ""; if ($uri ~ "/api/tnt"){ set $ingress_host hi-service2-service.<namespace>.svc.cluster.local;} proxy_set_header Host $ingress_host;

That way requests go /api/tnt will be sent to hi-service2.

Hope that helps!

--

--

Matías Costa
Matías Costa

Written by Matías Costa

SRE engineer | Technology enthusiast | Learning & Sharing

Responses (1)